Question

I've been trying to put together a robocopy CMD script to be able to ask the user for a path (copy paste or just entering it manually) but I seem to be stumped...

Here's the current code I have;

@ECHO OFF
SETLOCAL

:Input
SET /P "source=Please enter or paste the location you want backed up and press ^<Enter^>."
IF "%source%"=="" GOTO Error
GOTO :DoTask
:Error
ECHO You did not specify a location to be backed up! Please try again. & goto :Input

::SET source="":: Obsolete for now, since user input is possible.

:DoTask
REM SET YEAR
set YEAR=%date:~6,4%

REM SET MONTH
set MONTH=%date:~3,2%
if %MONTH% LSS 10 set MONTH=%MONTH:~1,2%
if %MONTH% LSS 10 set MONTH=0%MONTH%

REM SET DAY
set DAY=%date:~0,2%
if %DAY% LSS 10 set DAY=%DAY:~1,2%
if %DAY% LSS 10 set DAY=0%DAY%

REM SET HOUR
set HOUR=%time:~0,2%
if %HOUR% LSS 10 set HOUR=%HOUR:~1,2%
if %HOUR% LSS 10 set HOUR=0%HOUR%

REM SET MINUTE
set MINUTE=%time:~3,2%
if %MINUTE% LSS 10 set MINUTE=%MINUTE:~1,2%
if %MINUTE% LSS 10 set MINUTE=0%MINUTE%

REM SET SECOND
set SECOND=%time:~6,2%
if %SECOND% LSS 10 set SECOND=%SECOND:~1,2%
if %SECOND% LSS 10 set SECOND=0%SECOND%

SET destination="Backups"\%date%
SET logdir="Backups\Logs"\%date%
SET log="Backups\Logs"\%date%\%HOUR%_%MINUTE%_%SECOND%.log

mkdir "%logdir%" 2>NUL

SET copyoptions=/COPYALL /E /ZB /SEC /MIR
:: /COPYALL :: COPY ALL file info.
:: /E       :: Copy Subfolders, including Empty Subfolders.
:: /ZB      :: Use restartable mode; if access denied use Backup mode.
:: /SEC     :: Copy files with SECurity.
:: /MIR     :: MIRror a directory tree.

SET logoptions=/R:0 /W:0 /LOG:%log% /TS /NP /V /ETA /TIMFIX /SECFIX /TEE
:: /R:n:: Number of Retries.
:: /W:n:: Wait time between retries.
:: /LOG:: Output log file.
:: /TS :: Include Source file Time Stamps in the output.
:: /NP :: No Progress - don’t display % copied.
:: /V  :: Produce Verbose output log, showing skipped files.
:: /ETA:: Show Estimated Time of Arrival of copied files.
:: /TIMFIX :: FIX file TIMes on all files, even skipped files.
:: /SECFIX :: FIX file SECurity on all files, even skipped files.
:: /TEE:: Output to console window, as well as the log file.
:: /NFL:: No file logging.
:: /NDL:: No dir logging.

ROBOCOPY %source% %destination% %copyoptions% %logoptions%

:End

If this is too long, please feel free to edit this post and just link to it: http://pastebin.com/Np8wBF5b

So basically my issue with the above is as follows;

If I try to enter the path:

C:\Users\Public\Pictures\Sample Pictures

I get the error:

ERROR: Invalid Parameter #3 : "Backups\2013/10/22"
Source: C:\Users\Public\Pictures\Sample\
Destination: G:\Pictures

Notice how the destination in the first line of the error has forward slashes instead of backslashes? But what I find break it is the space between "Sample" and "Pictures".

For if I use a single word, it works perfectly, but brings me to my second problem;

Source: C:\Users\User\Pictures\BlackBerry\
Destination: G:\Backups\2013\10\22\

The source is correct, however in the destination folder it doesn't recreate the "Blackberry" folder, only the contents therein, and that is my issue...

For the user gets to choose multiple destinations to XCOPY, yet all that will happen is all the files get bunched together, with no folder structure etc...

My head is blown with trying to figure this all out, so I am REALLY hoping so kind soul will be able to help me out with this! :)

Was it helpful?

Solution

Enclose %source% in quotes, either there or where it is set.

Robocopy "%source%" ...

Edit: extra code after further the comments

Change this line as shown below: SET destination="Backups"\%date%

for %%a in ("%source%") do SET destination="Backups\%date%\%%~nxa"

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher. Run the batch file below to see the variables, and you can modify them too.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') 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 "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top