Using xcopy for backing up files and folders

If you want to backup a specific folder and do not want to use backup software there is always the xcopy command. Xcopy is primarily used to back up files and folders across a network to a specified device. This example is how to back up a folder on the network to a thumb drive on a server.

if not exist “I:\Backups\” goto directorynonexistent >> C:\TEMP\mybat.log

xcopy  “\\dc1\d$\users\myname\*.*”  “I:\Backups\”  /C /E /Q /S /Y >> C:\TEMP\mybat.log
move  I:\Backups I:\Backups-%date:~4,2%-%date:~7,2%-%date:~10,4%
mkdir I:\Backups

:directorynonexistent >> C:\TEMP\mybat.log

Here is a breakdown of the script.

  1. The script checks to ensure the thumb drive and folder exist on the server running the script.
  2. The script logs to a file in the TEMP folder.
  3. The xcopy command runs.
  4. Since we want to maintain several versions of the backup for comparison, the “move” command is used since we have to rename the Backups folder to the date the backup was run. The “move” command is used instead of the “ren” or “rename” command since they only work against files and not folders.
  5. Once the folder is moved, the Backups folder is created since the start of the script is looking for that folder.
  6. The last part of the script is to catch the message if the folder does not exist.

This is a very simple script that can be used to back up folders that contain images, documents, and other important information when back up software is a little much for what you want to do. Leave comments on how you modified the script and this is good for all versions of Windows, XP, 7, 8, 8.1, and 10 as well as Windows Servers 2008 and 2012.

Have fun!

Comments are closed.