How to Backup a Folder with a Batch file

The following batch script below, copies a folder to a folder. The folder is created with a name according to date that the script is run. The script will output a log file of the operation – files which were copied will be logged.

This script is intended only for WINDOWS machines. (XP and above)

Step 1

Click on Start > Run.

Enter cmd and press the enter key

Step 2

In the black window that opened write:

# EDIT BACKUP.BAT

The backup.bat is the name of the file that we created .

Step 3

In the blue screen that we just opened copy and paste the following (each line without the # mark):

# FOR /F “TOKENS=1 DELIMS= ” %%A IN (‘date/T’) DO SET CDATE=%%B
# FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘date/T’) DO SET mm=%%B
# FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
# FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
# SET date=%dd%-%mm%-%yyyy%
# md C:\%date% 
# copy “c:\folder_name\
.” c:\%date%>>c:\LogFile.txt 
echo %date% %time%>>c:\LogFile.txt

Let’s explain the code above:

EXPLANATION 1

# FOR /F “TOKENS=1 DELIMS= ” %%A IN (‘date/T’) DO SET CDATE=%%B
# FOR /F “TOKENS=1,2 eol=/ DELIMS=/ ” %%A IN (‘date/T’) DO SET mm=%%B
# FOR /F “TOKENS=1,2 DELIMS=/ eol=/” %%A IN (‘echo %CDATE%’) DO SET dd=%%B
# FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘echo %CDATE%’) DO SET yyyy=%%B
# SET date=%dd%-%mm%-%yyyy%
# md c:\%date%

All of this sequence is actually a command to open a library if 
the correct date for the day .

EXPLANATION 2

# copy “c:\folder_name*.*” c:\%date%

This is the command that copy the files in to the date folder (the backup folder).

EXPLANATION 2-A

folder_name is the folder that you want to copy from him

%date% is the folder with the correct day to copy the files that we want to backup .

EXPLANATION 3

# >>c:\logFile.txt 
# echo %date% %time%>>c:\LogFile.txt

This is a LOG file that contain the names of all the files that we copy and also the time and date that the backup was runs.

I wrote this script for a friend of mine for backup of folders in his father’s business.