how to create batch file to copy files to a shared folder with full access then logs all result & errors to a file?

eros picture eros · Feb 14, 2012 · Viewed 14k times · Source

I have created the below batch file but no success.

@ECHO OFF

@ECHO "Backup process started."

@SET TARGET_FILES = "\\pc1\temp\*.mdb"
@ECHO "TARGET_FILES:%TARGET_FILES%"

@SET DESTINATION_FOLDER = "\\pc1\temp\Backups"
@ECHO "DESTINATION_FOLDER:%DESTINATION_FOLDER%"

@SET BACKUP_FOLDER = "Backup%date:~10,4%%date:~4,2%%date:~7,2%"
@ECHO "BACKUP_FOLDER:%BACKUP_FOLDER%"

@SET FINAL_FOLDER = "%DESTINATION_FOLDER%\%BACKUP_FOLDER%"
MKDIR %FINAL_FOLDER%
@ECHO "DIRECTORY CREATED:%FINAL_FOLDER%"

XCOPY %TARGET_FILES% %FINAL_FOLDER% >> %FINAL_FOLDER%\%BACKUP_FOLDER%.log

@ECHO "Backup process ended successfully."

@ECHO ON

Environment: It will be scheduled task in C:\Windows\Tasks using Windows XP SP3

OUTPUT:

"Backup process started."
"TARGET_FILES:"
"DESTINATION_FOLDER:"
"BACKUP_FOLDER:"
The syntax of the command is incorrect.
"DIRECTORY CREATED:"
Invalid number of parameters
"Backup process ended successfully."

Any guidance on the right direction is appreciated.

UPDATES1: I've noticed that my variables were not set.

UPDATES2:

@ECHO OFF
ECHO Backup process started.

set TARGET_FILES=\\pc1\temp\*.mdb
ECHO TARGET_FILES "%TARGET_FILES%"

SET DESTINATION_FOLDER=\\pc1\temp\Backups
ECHO DESTINATION_FOLDER "%DESTINATION_FOLDER%"

SET BACKUP_FOLDER=Backup%date:~0,4%%date:~5,2%%date:~8,2%
ECHO BACKUP_FOLDER "%BACKUP_FOLDER%"

SET FINAL_FOLDER=%DESTINATION_FOLDER%\%BACKUP_FOLDER%
MKDIR "%FINAL_FOLDER%"
ECHO DIRECTORY CREATED "%FINAL_FOLDER%"

XCOPY "%TARGET_FILES%" "%FINAL_FOLDER%" >> "%FINAL_FOLDER%\%BACKUP_FOLDER%.log"

ECHO Backup process ended successfully.

@ECHO ON

OUTPUT FOR UPDATES2:

Backup process started.
TARGET_FILES "\\pc1\temp\*.mdb"
DESTINATION_FOLDER "\\pc1\temp\Backups"
BACKUP_FOLDER "Backup20120214"
DIRECTORY CREATED "\\pc1\temp\Backups\Backup20120214"
Backup process ended successfully.

LOG

\\pc1\temp\file1.mdb
\\pc1\temp\file1_be.mdb
2 File(s) copied

With the new code (UPDATES2), I have successfully copied all mdb files to target folder but all my echo messages were not logged. How can I include all my echo messages to be logged?

Answer

shenhengbin picture shenhengbin · Feb 14, 2012

I think you need add

**cd /d %~dp0\**   

("%~dp0" can replace the runtime directory to the full path of the batch file)

in your first line ..

cd /d %~dp0 means cd /d d:\currentfolder

UPDATE:

Ok , I think you just want to copy your files from A to B .

I create a sample for you base on your code.

My folder is at

D:\Run
│  main.bat  >> the copy batch
│  
└─Test
    │  new.txt
    │  
    └─Backups
        └─20120214          >> created new folder

the main.bat is

@ECHO OFF

cd /d %~dp0\

ECHO Backup process started.

SET TARGET_FILES="Test\*.txt"
ECHO TARGET_FILES:%TARGET_FILES%


SET DESTINATION_FOLDER=Test\Backups\%date:/=%
ECHO DESTINATION_FOLDER:%DESTINATION_FOLDER%

SET FINAL_FOLDER=%DESTINATION_FOLDER%
ECHO FINAL_FOLDER:%FINAL_FOLDER%
MD "%FINAL_FOLDER%"
ECHO "DIRECTORY CREATED:%FINAL_FOLDER%

XCOPY %TARGET_FILES% %FINAL_FOLDER%

ECHO Backup process ended successfully.

@ECHO ON

i hope it would help you .