Writing script to backup selected directories from my hard drive (C:) onto an external hard drive using XCOPY.
All went well when I used the same directory name on both source and destination. But when I tried to get a bit clever and
make the destination a subdirectory of a dated main directory called "Archive", I keep getting an "Invalid path" error. Here's an exerpt
of what I'm trying to do:
set/P Drive= Enter destination drive (with colon):
::
set BACKUPCMD= xcopy /S /D /I /Y /Q /C /T
::
set hour=%time:~0,2%
if "%hour:~0,1%"==" " set hour=0%time:~1,1%
SET dateNtime=%date:~10,4%-%date:~4,2%-%date:~7,2 %__%hour%:%time:~3,2%
::
set directory=TEST
set source=%directory%
set destination=%Drive%\ARCHIVE_%dateNtime%\%directory%
%BACKUPCMD% "C:\%source%\*.*" "%destination%"
Could somebody help me with my syntax?
Best Answer
Could somebody help me with my syntax?
There are several problems with your batch file:
The options to xcopy go after the source and destination
Your
SET dateNtime...
command is broken.It contains spaces and trailing spaces.
Using
%date%
to provide a solution is dependent on the OS Locale, Regional, and Language settings.Rather than try and fix your code I've provided working code using
wmic
(see getdate)This code works independently of OS Locale, Language or the user's chosen date format (Control Panel/Regional) and it is very easy to change the output format.
The following batch file fixes both of these issues:
Notes:
set _date=%_year%%_month%%_day%%_hh%%_mm%%_ss%
as appropriate to get your required date format.echo
when you are happy with thexcopy
command.Further Reading