简体   繁体   中英

Copying with batch scriptfile in Windows 2008 R2

I have a .cmd file that i want to use in automating copying a folder from a mapped location on a windows 2008 server to an external HDD connected to the windows server box:

@ECHO OFF
ECHO Backup process started.
set TARGET_FOLDER=Z:\"WindowsImageBackup - Copy"
ECHO TARGET_FOLDER "%TARGET_FOLDER%"
SET DESTINATION_FOLDER=H:\SERVER_BACKUP_IMAGE
ECHO DESTINATION_FOLDER "%DESTINATION_FOLDER%"
For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set mydate=%%d%%c%%b%%a)
For /f "tokens=1-2 delims=: " %%a in ('time /t') do (set mytime=%%a%%b)
SET BACKUP_FOLDER=BACKUP_%mydate%_%mytime%
ECHO BACKUP_FOLDER "%BACKUP_FOLDER%"
SET FINAL_FOLDER=%DESTINATION_FOLDER%\%BACKUP_FOLDER%
MKDIR "%FINAL_FOLDER%"
ECHO DIRECTORY CREATED "%FINAL_FOLDER%"
ECHO Backup process started ...
XCOPY "%TARGET_FOLDER%" "%FINAL_FOLDER%" >> "%FINAL_FOLDER%\%BACKUP_FOLDER%.log" /Y
ECHO Backup process ended successfully.
@ECHO ON
pause

After running the script, the destination folder is created and the log file created says 0 files copied but there are files in the target folder. This is the output:

Backup process started. TARGET_FOLDER "Z:\\"WindowsImageBackup - Copy"" DESTINATION_FOLDER "H:\\SERVER_BACKUP_IMAGE" BACKUP_FOLDER "BACKUP_20131409Sat_0333" DIRECTORY CREATED "H:\\SERVER_BACKUP_IMAGE\\BACKUP_20131409Sat_0333" Backup process started ... Invalid number of parameters Backup process ended successfully.

C:\\Control>pause Press any key to continue . . .

The 7th line is of interest but I do not know how to go around it. What parameters am i supposed to use?

Funny thing is that if i run this same script on my windows 7 laptop (i have the same directory structure), it runs and copies accordingly.

Thanks for your help.

您的报价需要改进:

set "TARGET_FOLDER=Z:\WindowsImageBackup - Copy"

The /Y was in the wrong spot, the xcopy had no filespec, and your filepath quoting needed some cleaning up as Endoro mentioned. This is how I would go about it, using your style of code (and wmic to get a robust date/time stamp).

@ECHO OFF
ECHO Backup process started.
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

set "TARGET_FOLDER=Z:\WindowsImageBackup - Copy"
SET "DESTINATION_FOLDER=H:\SERVER_BACKUP_IMAGE"
SET "BACKUP_FOLDER=BACKUP_%fullstamp%"
SET "FINAL_FOLDER=%DESTINATION_FOLDER%\%BACKUP_FOLDER%"

ECHO TARGET_FOLDER "%TARGET_FOLDER%"
ECHO DESTINATION_FOLDER "%DESTINATION_FOLDER%"
ECHO BACKUP_FOLDER "%BACKUP_FOLDER%"
MKDIR "%FINAL_FOLDER%" 2>nul
ECHO DIRECTORY CREATED "%FINAL_FOLDER%"
ECHO Backup process started ...
XCOPY "%TARGET_FOLDER%\*.*" "%FINAL_FOLDER%\" /Y >>"%FINAL_FOLDER%\%BACKUP_FOLDER%.log" 
ECHO Backup process ended
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM