With a carefully crafted batch file, it is possible to create a series of commands that will back up data to a chosen location and then shut down your PC automatically.
In Microsoft Windows 10, when moving or copying files from one folder or drive to another folder or drive, most people understandably use File Explorer and the graphical user interface (GUI). On balance, virtually dragging and dropping files is fairly intuitive and easy to do, so why not? However, the GUI is not the only option for copying your data.
Under that Windows 10 GUI lies a fairly robust command-line interface and PowerShell scripting language, including the file copying and moving application Robocopy. With Robocopy and a carefully crafted batch file, it is possible to create a series of commands that will back up data to a chosen location and then shut down your PC automatically.
This tutorial shows you how to craft a batch file that uses Robocopy to automatically back up data and then uses the proper command to shut down a Windows 10 PC completely.
SEE: Windows 10 May 2019 Update: 10 notable new features (free PDF) (TechRepublic)
How to back up files on exit automatically with Robocopy
Batch files are simple text files that the operating system interprets as executable commands. This type of file was common when the primary PC operating system was DOS, long before Windows was available.
For this example, we will have our batch file copy the contents of C:/Backup1 to Z:/Backup2, where Z: is my personal Microsoft OneDrive, mapped to a network drive in a previous article.
A batch file is a simple text file, so we can create it using any text editor. For this example, we will use Notepad. Type "notepad" in the search box on the desktop and click the appropriate result to start a blank text file.
First, we will create the Robocopy command to copy the files; then, we will order the system to shut down. Type in these lines into Notepad:
Robocopy "C:\Backup1" "Z:\Backup2" /MIR
Figure A shows the simplified version of the batch file.
Note: For our example the quotation marks in the first line are not necessary; however, if your directory names include spaces, then the quotation marks are mandatory. For the sake of completeness, we will use them regardless.
Technically, those two lines of code are all we need to accomplish our goal of backing up files and then shutting down the PC, but the commands lack elegance. For one thing, our batch file in its current form will shut down our PC immediately, without considering whether the copying procedure has completed.
We can remedy this problem by adding the -t parameter to the shutdown command. The number of seconds you wish to delay the shutdown will depend on how much data you will be backing up, but for our example, we will set it to 5 seconds.
The next potential problem is that we have not accounted for the possibility of subdirectories with files of their own that may need to be included in our Robocopy backup operation. We will add the /MIR parameter to the Robocopy command so that all files and subdirectories will be copied.
The updated commands, as shown in Figure B, would be:
Robocopy "C:\Backup1" "Z:\Backup2" /MIR
shutdown -s -t 5
With commands complete, we can save the batch file. From the File menu in Notepad click the Save As item and give the file an appropriate name and location, making sure to use the .bat extension and not the suggested .txt extension.
You can pin your batch file to the Start menu or the Taskbar or some other convenient spot like the desktop. When you double-click the batch file, it will run much like any other executable--just make sure you are ready for the shutdown process to begin because there is no stopping it once it starts.
- 20 pro tips to make Windows 10 work the way you want (free PDF) (TechRepublic)
- Windows 10 security: A guide for business leaders (TechRepublic Premium)
- Windows 10 May 2019 Update 1903 gets first major bug fixes (ZDNet)
- The best antivirus protection of 2019 for Windows 10 (CNET)
- How to activate and use the built-in Windows 10 back-up feature (TechRepublic)
- Windows 10 May 2019 Update: How to enable dark mode in any Windows app (TechRepublic)
- The 6 new features in Microsoft Universal File Explorer that pros need to know (TechRepublic)
- How to add 'Move to' and 'Copy to' to the context menu in Windows 10 (TechRepublic)
- Get more must-read Microsoft tips and news (TechRepublic on Flipboard)