Xcopy problem in batch file

Microsoft Windows 7 ultimate 32-bit
May 4, 2010 at 20:04:45
Specs: Windows 7
I am stumped with a batch file problem. I can accomplish what I want with COPY command, but it won't work when I replace it with XCOPY (which is what I need).

I am trying to back up files AND insert the DATE into the filename.
This line works perfectly:
copy c:\atemp\*.* c:\atemp\back\*-%date:~4,2%-%date:~7,2%-%date:~10,4%.*

However, when I try to run the exact same line using XCOPY, it doesn't insert the date.

Whats the difference between COPY and XCOPY regarding how it interprets the %DATE% variable?

See More: Xcopy problem in batch file

Report •

May 4, 2010 at 23:07:00
my uneducated guess would be xcopy's extrordinarily irritating habit of "politely" asking if the destination is to be a directory or a file, which i can understand to a degree..
but would have been nice to have xx\ to be interpreted as a directory no questions asked.
You can trick xcopy by creating a dummy file prior to the xcopy, and using the /y switch:
set x=%date:~5,2%
:: left off the rest cause i'm lazy, now create fake file
echo . > %x%
:: now the file exists, so xcopy won't be confused: its a FILE!
xcopy /y nnn %x%

Report •

May 5, 2010 at 00:29:28
Yep. Just when you thought it couldn't get any dummer.

Back in the day you could pipe a D or F. Recently winders added the /I option:

"If destination does not exist and copying more than one file,
assumes that destination must be a directory."

Which is meaningless. Because since xcopy won't concatenate, it MUST be a directory.

What they shoulda did was provide a /-I; but they didn't.

To the OP: you're trying to use xcopy in a way it was not meant to work.

Helping others achieve escape felicity


Report •

May 5, 2010 at 02:33:02
If you have Win7, you should be using RoboCopy, which is - once more - an extension, this time to XCOPY

Note that each command has its own parameters and behaviour. But the DATE variable works the same. It's the OS that first will parse the variable(s) if any, and THEN the command. So, the command does not know of any variables, it gets the "full" written command.

Report •

Related Solutions

Ask Question