New folder for every backup CMD

2019-09-05 13:28发布

I am trying to get a batch going to backup a folder on a work server. (Please see attached link) Bat Error "invalid number of parameters"

Long story short - I need the script to name a new folder for each backup, which "md new_folder" was suggested, but I cannot seem to get the context or how to place it into the code.

@echo This will now create a new backup of S:\Internal Auditor\9 - September 14

@echo off

:: variables
set SRCFOLDER="S:\Internal Auditor\9 - September 14"
set DESTFOLDER="S:\Internal Auditor\2014\9 - Sept Backup"
set folder=%date:~5,2%-%date:~8,2%-%date:~0,4%
set backupcmd=/W /E /H /V /C /Z /I /F /J /R /Y

echo ######## PLEASE WAIT SYSTEM BACKINGUP SOME DATA########
"md new_folder" xcopy %SRCFOLDER% %DESTFOLDER% %backupcmd%

echo !!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
@pause

Any help would be greatly appreciated!

----------EDIT: I tried a line of code that slowly got me closer:----------

@echo This will now create a new backup of S:\Internal Auditor\9 - September 14

@echo off

:: variables
set SRCFOLDER="S:\Internal Auditor\9 - September 14"
set DESTFOLDER="S:\Internal Auditor\2014\9 - Sept Backup"
set folder=%date:~5,2%-%date:~8,2%-%date:~0,4%
md "%DESTFOLDER%\%folder%"
set backupcmd=/W /E /H /V /C /Z /I /F /J /R /Y

echo ######## PLEASE WAIT SYSTEM BACKING UP SOME DATA########
xcopy %SRCFOLDER% "%DESTFOLDER%\%folder%" %backupcmd%

echo !!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
@pause

BUT - I am getting the following output:

This will now create a new backup of S:\Internal Auditor\9 - September 14
Access is denied.
Error occurred while processing: S:\Internal.
A subdirectory or file Auditor\2014\9 already exists.
Error occurred while processing: Auditor\2014\9.
A subdirectory or file - already exists.
Error occurred while processing: -.
A subdirectory or file Sept already exists.
Error occurred while processing: Sept.
A subdirectory or file Backup\9/-9/-Tue  already exists.
Error occurred while processing: Backup\9/-9/-Tue .
######## PLEASE WAIT SYSTEM BACKING UP SOME DATA########
Invalid number of parameters
!!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
Press any key to continue . . .

----------EDIT 2: I tried a line of code that slowly got me closer:----------

I have followed the suggestions and some things I found online, and now I am to the point where I am getting a few errors:

md "S:\Internal Auditor\~\09/12/2014"
A subdirectory or file S:\Internal Auditor\~\09/12/2014 already exists.

And:

xcopy "S:\Internal Auditor\9 - September 14 S:\Internal Auditor\2014\9 - Sept Backup\09/12/2014
/W /E /H /V /C /Z /I /F /J /R /Y /D
File not found - 09/12/2014 /W /E /H /V /C /Z /I /F /J /R /Y /D

This is my code:

@echo This will now create a new backup of S:\Internal Auditor\9 - September 14
Rem Backup 9 - September 14

@echo

:: variables
set "SRCFOLDER=S:\Internal Auditor\9 - September 14"
set "DESTFOLDER=S:\Internal Auditor\2014\9 - Sept Backup"
set folder=%date:~4%
md "%DESTFOLDER%\%folder%"
set backupcmd=/W /E /H /V /C /Z /I /F /J /R /Y /D


@echo ######## PLEASE WAIT SYSTEM BACKING UP SOME DATA########

xcopy "%SRCFOLDER% %DESTFOLDER%\%folder% %backupcmd%"

@echo !!!!!!!!BACKUP COMPLETED THANKS!!!!!!!!!!!!!!
@pause

Also, when I run the code, it makes a new directory ~\09\12\2014 instead of the desired ~\09/12/2014. Clarification: It makes 3 new subdirectories instead of one new subdirectory.

2条回答
地球回转人心会变
2楼-- · 2019-09-05 14:07

Okay let's see what you're doing. Your variable folder is being set based on your local settings for a short date, which for me shows e.g. Tue 09/09/2014 so your %date:~0,4% would be Tue. Use the echo command to be sure you're getting what you want:

echo %date:~5,2%-%date:~8,2%-%date:~0,4%

But let's assume for now you already figured that out and %folder% contains what you want it to. This is two commands on a line, which won't work at best at at worst is going to create a bunch of folders you didn't expect:

"md new_folder" xcopy %SRCFOLDER% %DESTFOLDER% %backupcmd%

You want to split that into 2 lines, and you want to use your new date-based folder name as part of the destination, I'm sure. So this will do that:

md "%DESTFOLDER%\%folder%"

and then to copy you'd need to include \%folder% in your destination:

xcopy %SRCFOLDER% %DESTFOLDER%\%folder% %backupcmd%

But I think, and I know I shouldn't ask for clarification but perhaps if I'm wrong you should clarify what you need a bit more in your question, you really want that S:\Internal Auditor\2014\9 - Sept Backup to be S:\Internal Auditor\ plus the value in %folder% and not having the date the way it is in that path.

Suggestion: Make the %folder% variable to be yyyy-mm-dd format such as 2014-09-09 so it's sortable, and use that in your destination.

查看更多
Rolldiameter
3楼-- · 2019-09-05 14:07

Ok. There are some errors:

set SRCFOLDER="S:\Internal Auditor\9 - September 14"
set DESTFOLDER="S:\Internal Auditor\2014\9 - Sept Backup"

Must be (safe method, adding first quote at start of SRCFOLDER and DESTFOLDER):

set "SRCFOLDER=S:\Internal Auditor\9 - September 14"
set "DESTFOLDER=S:\Internal Auditor\2014\9 - Sept Backup"
查看更多
登录 后发表回答