Thursday, August 23, 2012

Backup My Docs with WINDOWS 7 cmd

I just learned how to copy / paste My Documents folder (or any other) to a new folder named Backup+date and I want to share it.

This is the code:
--------------------------------------

@echo off
echo %time%
echo ### creating backup folder
cd C:\Users\victor\desktop 
::md backup_%Date:~-4,4%_%Date:~-10,2%_%Date:~-7,2%

xcopy C:\Users\victor\Documents\* C:\Users\victor\desktop\backup_%Date:~-4,4%_%Date:~-10,2%_%Date:~-7,2% /s /i >> backup_%Date:~-4,4%_%Date:~-10,2%_%Date:~-7,2%.txt


echo Backup Complete!
echo %time%

@pause
-------------------------------------

If you want it, you need to copy this code and paste it in a notepad saving it with a name and with the extension .cmd which is pretty cool because you only need to double click this file to run it, NO need to go to the command line to execute it.

As you can see I mark the time when the script starts and finishes so I know how much time the hole process took.

then I create using "cmd" the backup folder including in its name the date, for now I am just saving the backup folder to the Desktop but really the idea would be creating it directly to an external HDD. I do this because I save a log file with all the doc that were copy from one location to another, but the reality is that xcopy by it self could create the destination folder by pointing the right location.

If you take out the date of the folder then instead of creating multiple backup folders with different dates, you will be overwriting the backup folder which is pretty useful if you only want a single image of your documents.

As you can see it is not rocket science, but I am happy that I was able to do this, now I know that I can write scripts like this for windows to move file around my system.

Vic./

No comments: