Use-Batch-Files-in-Windows

You can use batch files to work behind the scenes in Windows and automate repetitive processes. Create a few strings of code and place them in a file to be run by the Task Scheduler, and you can actually make your very own backup program without downloading anything. You can create a backup using a batch file or program it to perform other repetitious operations.

Not only this, you can have this run on a schedule and even output a history of results every time the task runs. Follow our guide step-by-step to learn how this is done. Be warned – The process looks complex, but if you follow it step by step you will be able to master it in no time.

SEE ALSO: Backup Windows 8 Apps using Windows 8 Data backup 

How to backup using a batch file

What commands are used?

There are three commands to be used for copying or moving, and your selection depends on what it is you need to move or copy:

XCOPY: Copies files and folders to a different location.

Copy: Copies one or more files to a different location, but not folders.

Move: Moves and renames files and folders.

Say we have a mapped network drive or external device we want to use for a backup location. Something I’d like to set up for myself is a backup of all my downloaded program files.

The software I download goes to my “%userprofile%\Downloads\Software” folder, so I want to use this as the source and then my external drive, “G:\DownloadsBackup” as the destination.

It’s at this point you need to choose how you want to do the transition. Though we could use any command listed above, we’re going to choose “move” under personal preference because I don’t want to keep all these executable and zip files on my local computer.

Move objects with a batch file

Options, or switches, are used in batch files for additional command functions. There are two we can use here.

/Y: Don’t ask to overwrite files of the same name

/-Y: Confirm overwrite operations

After you find a good place for a destination, make a string in notepad similar to mine below, using the correct structure.

Structure: move switch source destination

move /Y %userprofile%\Downloads\Software\*.* G:\DownloadsBackup

Note: If there is a space in the folder structure, cover the whole location in quotes (e.g.: “G:\Downloads Backup”).

Save this as a batch file (“.bat”) under a recognizable name and location.

Save-the-file-with-a-bat-extension

If this file is run it should yield the correct display of results to indicate a successful move, removing the source files in the process.

View-the-backup-results-in-the-command-prompt

Automate a move operation to backup files

We’ve written about using the Windows Task Scheduler to automate reminder prompts, and we can also use it for running a batch file on a schedule.

Search for “Task Scheduler” on the Start menu and choose to start a new task from the “Action” menu.

Create-a-new-task-in-Task-Scheduler

Enter a name for the task and choose “Run whether user is logged on or not” with “Run with highest privileges” checked.

Check-the-security-settings-for-the-new-task

Enter the schedule information from the “Triggers” tab to whatever suits you best, ensuring it’s set to “Enabled” at the very bottom.

Manage-the-schedule-trigger-for-the-new-task

Create a new action from the “Actions” tab to “Start a program” with your own batch file selected.

Choose-a-batch-file-to-start-on-a-schedule

Make optional changes to the “Settings” tab for how the scheduler should respond to missed tasks. Because we’re backing up files, which is important to complete in some cases, fiddle with these options to ensure it will run again after a defined time.

Ensure-the-task-will-try-again-if-it-fails-from-the-Settings-tab

When finishing the scheduler you may be prompted for a password to your account because we selected to run the task whether we’re logged on or not – so it needs the user password.

Because we’re making this entire process automated, it’d be nice to know if the operations actually succeed. We can add some more commands to the end of the initial one to output the result to a text file.

The section below explain how to do that.

Output backup results to a text file

Begin by making a folder where the results should reside, like ours of “Downloads\Reports.” Add the locations full path after the batch file you made above, like this, ensuring the “>” symbol is used: 

move %userprofile%\Downloads\Software\*.* G:\DownloadsBackup\ > %userprofile%\Downloads\Reports\Result.txt

This will make a file called “Result.txt” with information about the operation. It will state which file(s) were moved, if any, just like in the command prompt result above.

View-batch-results-in-an-exported-text-file

While this will work fine, after the second operation comes in and overwrites this file, you will no longer have access to the earlier days results.

We can avoid this by adding yet another string that changes the date of the “Result.txt” file to show the current date. If you only run this backup once a day, this will work just fine.

Add the following two new lines in the batch file:

set Today=%date:~10,4%-%date:~4,2%-%date:~7,2%
ren %userprofile%\Downloads\Reports\Result.txt %Today%.txt

This sets the variable “Today” up with the current day and then the second line renames the “Result.txt” file to that which the variable defines: the current date. The result for today will make a file called “2013-06-20.”

The final result for our entire batch file is as follows:

move %userprofile%\Downloads\Software\*.* G:\DownloadsBackup\ > %userprofile%\Downloads\Reports\Result.txt
set Today=%date:~10,4%-%date:~4,2%-%date:~7,2%
ren %userprofile%\Downloads\Reports\Result.txt %Today%.txt

Remember, each of these paths and commands for reports and backup source/destination locations are all customizable and will change according to your setup.

Conclusion

This may seem daunting at first, but if you read carefully through this guide you’ll notice it’s really not all that difficult. Yes, you can use other  programs that do this for you with more options and an easy-to-use interface, but getting a hold of the processes of Windows can be fun and rewarding.

If you’re looking for dedicated backup programs, consider reading about the Windows 8 File History feature or MoveBOT.