REM Capture the date/time(right down to the second) and then assign it to a variable
set yy=%date:~-4%
set dd=%date:~-7,2%
set mm=%date:~-10,2%
set newdate=%dd%%mm%%yy%_%Time:~0,8%
set newdate=%newdate::=%
SET foldername="svetlana_backup_%newdate%"
SET drive=T:
REM Source directories
SET documents=%drive%\Documents
REM Destination directories
SET destinationDocuments=%backupDir%\Documents
call:makedirandcopy %documents% %destinationDocuments%
:makedirandcopy
ECHO Making and Copying %~2 Directory
MKDIR %~2
XCOPY %~1 %~2 /E /F
I have the following batch file on my desktop, when I run the batch file, it supposed to make a directory on my destination drive and copy all the documents, however, it makes the directory, and the documents subdirectory, but on my desktop, where the batch file resides, and never copies the files in Documents. The file name is also incorrect, it just assigns the time to the directory instead of the date_time.
Passing T:\Documents "T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents
Making and Copying T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents Directory
Making and Copying Directory
0 File(s) copied
If I were to do all of this without the label it works.
call :makedirandcopy %documents% %destinationDocuments%
goto :EOF
:makedirandcopy
ECHO Making and Copying %~2 Directory
MKDIR "%~2"
XCOPY "%~1" "%~2" /E /F
goto :EOF
From your run report, you have a line
Passing T:\Documents "T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents
There is nothing in the code you've posted that will generate that line.
You then have
Making and Copying T:\Backup"\"svetlana_backup_23022016_ 91300"\Documents Directory
Making and Copying Directory
The cause of this is that you are call
ing :makedirandcopy
(which displays the first line) and when the call
ed routine ends (presumably it reaches end-of-file) control is returned to the statement following the call
- which is the routine again, this time with no parameters, hence the second line.
Try
call :makedirandcopy "%documents:"=%" "%destinationDocuments:"=%"
goto :EOF
which will remove the excess quotes in each of the variables and quote the result.
Note that since there are spaces in the parameters being passed to :makedirandcopy
, xcopy
will require those parameters quoted.
Perhaps you'd also need
...
set newdate=%newdate::=%
set newdate=%newdate: =0%
...
to replace the suppressed-leading-zero in the time with a real, genuine 0
.