-2

I am trying to create a which date stamps text files monthly.

I tried the following code, but this says file not found, 0 files copied.

set FileDate=%date:/=%
set FileDateYYYY=%FileDate:~0,4%
set FileDateMM=%FileDate:~4,2%
set /a FileDateMM=%FileDateMM%
if %FileDateMM% EQU 0 ( SET FileDateMM=12
    SET /a FileDateYYYY=%FileDateYYYY%-1)
set FileDateOut=%FileDateYYYY%_%FileDateMM%
xcopy "L:\Capital Management\SAM Market Risk\OB lapses and surrenders\RawData\OB_PERSISTENCY.txt"  "L:\Capital Management\SAM Market Risk\OB lapses and surrenders\RawData\%FileDate%\" /C /D /Y /I

File not found - OB_PERSISTENCY.txt
0 File(s) copied

Does anyone know why that is?

If I replace the name of the file OB_PERSISTENCY with *, it works, but I need it to work with the filename.

Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

0

The /D switch is used for copying files, based on a modification date, which you didn't fill in. As you create the destination folder using the xcopy command, the modification date of that folder will be later than the modification date of the file, so the file won't be copied.
Please retry without this switch, and it should work:

xcopy "L:\Capital Management\SAM Market Risk\OB lapses and surrenders\RawData\OB_PERSISTENCY.txt"  "L:\Capital Management\SAM Market Risk\OB lapses and surrenders\RawData\%FileDate%\" /C /Y /I
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • The `/D` option does no harm whatsoever. If the destination directory doesn't exist, there will be no existing file with the source filename inside it, so the file will be copied. If the destination directory exists, the source file will only be copied if it is newer than the existing destination file. Additionally the OP wishes to copy all files, _or use a wildcard because they don't know its name in advance_, they don't wish to copy a single known file name. – Compo Jun 28 '19 at 16:14