0

I would like to append the ddmmyy to the file name, copy the file and move to a specific location. So far this is what I have, and I am not able to get the Date Modified to work correctly.

original code:

@Echo Off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @( 
Set Day=%%A
Set Month=%%B
Set Year=%%C
Set Year=%%D
Set All=%%A%%B%%C%%D
)
@For %%a in ("*.txt") do copy %%a "import\%%~na_%All%.csv"

I tried the following:

@For %%a in ("*.txt") do copy %%a,,+ "import\%%~na_%All%.csv"

@For %%a in ("*.txt") do copy %%a "import\%%~na_%All%.csv,,+"

@For %%a in ("*.txt") do copy %%a "import\%%~na_%All%.csv",,+

The file name copies and appends the date correctly but, I can't get it to update the Date Modified correctly.

1 Answers1

0

copying a file does not modify the "last modified" date. You need to touch the file. Read this SO Question https://stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command and try

copy %%a +,,
copy %%a "import\%%~na_%All%.csv"

Apart of that, I can give you a couple of suggestions for improvement..

  • you could just use the %DATE% environment variable instead of copying/parsing the output of DATE command.

    if the format of %DATE% does not fit your requirements, take a look at this SO answer https://stackoverflow.com/a/11183896/30447.

  • you could use MOVE instead of COPY if you want to move the file (as you state in your Question).

    @echo off
    for %%a in (*.txt) do (
      copy "%%a" +,,
      move "%%a" "import\%%~na_%DATE%.csv"
    )
    
Community
  • 1
  • 1
PA.
  • 28,486
  • 9
  • 71
  • 95