0

following scenario:

i have written a batch script whitch adds the datestamp to the filename. There are lots of different file-types in the folder.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') 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 "datestamp=%YYYY%-%MM%-%DD%_" 
:: naming like ISO 8601

pause

for %%i in (*.*) do ren %%i %datestamp%%%i*

is there a good way to exclude the bat-file itself from the renaming? It would be good enought if there is a way to exclude all .bat-files.

chris
  • 57
  • 1
  • 1
  • 9
  • Yes, there is. Have a look at http://stackoverflow.com/questions/8797983/can-a-dos-batch-file-determine-its-own-file-name in combination with http://stackoverflow.com/questions/16193702/batch-for-loop-exclude-file-name-that-contains. – Tim Zimmermann Aug 05 '14 at 09:30

1 Answers1

2

Your trailing * in the REN statement is not doing anything - it is not needed.

You should enclose the REN filenames in quotes in case you run into names with spaces.

Filtering out (skipping) your running batch file is easy. DIR /B can be piped to FINDSTR /V to get a list of files except for the running script. %~nx0 gives the file name and extension of the running script. FOR /F is used to capture and process the list of files.

for /f "eol=: delims=" %%F in ('dir /b /a-d ^| findstr /vixc:"%~nx0"') do ren "%%F" "%datestamp%%%F"
dbenham
  • 127,446
  • 28
  • 251
  • 390