1

I have a script for copying files from one folder to another. The XCOPY command looks like this:

 XCOPY /E /Y /V /D:%date% %reportsDataPath% TMP\

The date flag is supposed to make it copy only files from the date specified or later, but in the TMP I see that it copied files created earlier. However, it does not copy all the data from the files. Instead it copies the root file and subdirectories without the data they should contain.

Is there an error in my command that makes it copy those files?

0

1 Answer 1

3

%date% is giving a bad format for the command, the help says that the /D switch expects the format m-d-y, while echo %date% would give you Tue 11/26/2013. Meaning that you need to reformat the date to comply.

I think you can get that fixed with:

@echo off

:GetDate
for /f "skip=1 tokens=1-3" %%a in ('WMIC Path Win32_LocalTime Get Day^,Month^,Year') ^
do (
    set mydate=%%b-%%a-%%c
    goto CopyScript
)

:CopyScript
 xcopy /e /y /v /d:%mydate% %reportsDataPath% TMP\

Update #1

For the sake of future visitors, I believe this update will get you the correct date format regardless of region. If someone from another locale (outside US and that uses a different format than mm/dd/yyyy by default) can verify, that would be awesome.

Update #2

Using powershell, you can also get this (cmd commands still work in Powershell, but the overall functionality is massively expanded).

Get-Date -Format MM-dd-yyyy
16
  • No need for looping, use /D:%DATE:~3,2%-%DATE:~0,2%-%DATE:~6,4% or even better, use ROBOCOPY instead of XCOPY which is more reliable.
    – WhoIsRich
    Commented Nov 26, 2013 at 15:27
  • @WhoIsRich - You may want to double check your command (%DATE:~3,2%-%DATE:~0,2%-%DATE:~6,4%), that outputs 1-Tu-/26/ (I copy and pasted it, no fat fingering here). Looks like it should actually be: %DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%
    – nerdwaller
    Commented Nov 26, 2013 at 15:29
  • 2
    Ah, forgot that it is dependant on your local time format, so what I posted is correct for me, because I am in the UK and %DATE% is in DD/MM/YYYY where as in the USA %DATE% would probably give you MM/DD/YYYY.
    – WhoIsRich
    Commented Nov 26, 2013 at 15:41
  • @WhoIsRich - Sorry about that! Added it to the answer. Out of curiosity, does the xcopy /? say for you that it wants /D:m-d-y still?
    – nerdwaller
    Commented Nov 26, 2013 at 15:47
  • Yes, XCOPY still requires /D:m-d-y so atleast that is consistent :) Robocopy has /MINAGE: and wants either the number of days or YYYYMMDD just to make things interesting.
    – WhoIsRich
    Commented Nov 26, 2013 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .