Windows 7 XCOPY – Fix Invalid Path Using XCOPY

windows 7

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:

  1. The options to xcopy go after the source and destination

    XCOPY source [destination] [options]
    
  2. 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:

@echo off
setlocal enableDelayedExpansion
set/P Drive= Enter destination drive (with colon):
:: 
set BACKUPCMD=xcopy
SET BACKUPOPTIONS=/S /D /I /Y /Q /C /T
:: 
rem get date independently of OS Locale, Language or the user's chosen date format (Control Panel/Regional).
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-6" %%g in (`wmic Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"`) do (
  set _day=00%%g
  set _hours=00%%h
  set _minutes=00%%i
  set _month=00%%j
  set _seconds=00%%k
  set _year=%%l
  )
rem pad with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
set _hh=%_hours:~-2%
set _mm=%_minutes:~-2%
set _ss=%_seconds:~-2%
rem adjust _date as appropriate for your requirements
set _date=%_year%%_month%%_day%%_hh%%_mm%%_ss%
::
set directory=TEST
set source=%directory%
set destination=%Drive%\ARCHIVE_%_date%\%directory%

echo %BACKUPCMD% "C:\%source%\*.*" "%destination%" %BACKUPOPTIONS%

Notes:

  • I'm not sure from your question exactly what date format you are looking for.
  • Change set _date=%_year%%_month%%_day%%_hh%%_mm%%_ss% as appropriate to get your required date format.
  • Remove the last echo when you are happy with the xcopy command.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
  • wmic - Windows Management Instrumentation Command.
  • xcopy - Copy files and/or directory trees to another folder.
Related Question