1.

Solve : Batch Function Not Copying Properly?

Answer»

Code: [Select]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="syrena_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.

Code: [Select]Passing T:\Documents "T:\Backup"\"syrena_backup_23022016_ 91300"\Documents
Making and Copying T:\Backup"\"syrena_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 worksThe code you have shown and the screen output do not match.


Quote from: Syrena on February 23, 2016, 07:55:46 AM

Code: [Select]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="syrena_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

Code: [Select]Passing T:\Documents "T:\Backup"\"syrena_backup_23022016_ 91300"\Documents
Making and Copying T:\Backup"\"syrena_backup_23022016_ 91300"\Documents Directory
Making and Copying Directory
0 File(s) copied

Terribly Sorry, but could you elaborate? Are you talking about this line:

Code: [Select]Passing T:\Documents "T:\Backup"\"svetlana_backup_23022016_ 91300"\DocumentsI'm talking about these lines. There are elements there that cannot be produced with the code you have shown.
There's no point helping anyone with programming when the code they show isn't giving the RESULTS they say it is giving.

Code: [Select]Passing T:\Documents "T:\Backup"\"syrena_backup_23022016_ 91300"\Documents
Making and Copying T:\Backup"\"syrena_backup_23022016_ 91300"\Documents Directory
Making and Copying Directory
0 File(s) copied .From the first post you say it is soaking on your desktop. If so, then it is working. Just you don't like it to work that way.
Windows, like other OS can do path names at leat two ways.
1 Absolute path name.
2. Relative path name.

Quote
An absolute pathname, also referred to as an absolute path or a full path, is the location of a filesystem object (i.e., file, directory or link) relative to the root directo

If an absolute name is not given , the default is a relative path name.

Quote
. The relative pathnames are opposite to absolute pathname. A reative pathname does not begin with a slash ( / ). Generally you specifies location relative to your current working directory. This is most USEFUL to short a path name

The above is for Linux. For Windows use the \ instead.

I managed to fix it, here is the code if anyone is interested:

Code: [Select]@ECHO OFF

REM Start Backup
TITLE Backup

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="syrena_backup_%newdate%"

REM Variables
SET drive=T:
SET sevenZip=%USERPROFILE%\7z.exe

REM Source
SET documents=%drive%\Documents

REM Destination Directory
SET destination="%drive%\Backup"
SET backupDir=%destination%\%foldername%
SET destinationDocuments=%backupDir%\Documents

ECHO Welcome %USERNAME%

ECHO Please Plug in %drive% Drive and Disconnect the Internet.
pause

IF EXIST %destination% (

MKDIR %backupDir%

call :makedirandcopy "%documents:"=%" "%destinationDocuments:"=%"

DIR /S /B %backupDir% > %backupDir%\%foldername%.txt

echo Creating Zip file of %backupDir%
%sevenZip% a -tzip %backupDir%.zip %backupDir%\*

RMDIR /Q /S %destination%\%foldername%
pause
goto :EOF

) ELSE (

ECHO Error Finding Destination Drive! Aborting
pause
goto :EOF

)

:makedirandcopy
ECHO Making %~2 Directory
MKDIR "%~2"

ECHO Copying %~1 Directory
XCOPY "%~1" "%~2" /E /F
The goto:EOF was needed, because without it, makedirandcopy would execute without parameters. I also needed to properly quote my variables when passing them to the function.


Discussion

No Comment Found