0

I have the code below which renames a specific file, in this case 16919.csv appending the date on the end and putting it in a folder of the same name as the original file. However I have this same batch file setup for multiple file names. What id like is for the command to grab the name of the file and use that to run the commands below. So I can have one file to rename multiple files of different names. How is this possible?

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^|find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%DD%%MM%%YYYY%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

ren "16919.csv" "/16919/16919 - %datestamp%.csv"
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 2
    Maybe if you update your question with some input and output examples that would help us answer your question. – Squashman Nov 23 '16 at 17:11
  • You are basically looking for the [`for`](http://ss64.com/nt/for.html) loop and for the [`move`](http://ss64.com/nt/move.html) command... – aschipfl Nov 23 '16 at 18:13

1 Answers1

0

The following script is limited to only renaming files of one extension (in this case, .CSV):

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^|find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

set datestamp=%DD%%MM%%YYYY%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

for /f "delims=" %%i in ('dir /b /a-d *.csv') do ren "%%~i" "%%~ni %datestamp%%%~xi"

Just run that script in the folder with the files you want to append the date to.

References this question.

Community
  • 1
  • 1
JoshuaTheMiller
  • 2,582
  • 25
  • 27
  • Thank you this is what i wanted i couldn't use the other as this is scheduled to run i dont physically run it myself so cant drag and drop. I just changed the last line a bit to: `for /f "delims=" %%i in ('dir /b /a-d *.csv') do ren "%%~i" "/%%~ni/%%~ni - %datestamp%%%~xi"` – cheesemarathon Nov 24 '16 at 12:10