i am writing a script to rename files based on date.

Files are generated every day from a server outputing them like this:


My goal with my script is to auto rename them to:


Manual steps I goes trought to achieve this are :

 1- format date to AAMMJJ (i am on a french computer, windows 7)
      set date=%date:~8,2%%date:~3,2%%date:~0,2%
         echo %date%
 2- Now rename my file as expected
      set fname=fil150807102600000001.txt
      ren "%fname%" "FILE-SRV1.D%date%_%fname:~13,8%"   [strip off the first 
      13 char and leave the 8 last]

My file is correctely renamed.

now I want to script this to:

loop into the directory, 
find all file names starting by fil*
and rename them as needed

Here is my script assuming this script is run from the folder where my original files are:

set date=%date:~8,2%%date:~3,2%%date:~0,2%
for /f %%f in ('dir fil* /b') do ren %%f "FILE-SRV1.D%%date%%_%%f:~13,8%"

Result is:

Test for file existence

C:\test\bat>dir fil* /b

Running the scripts

C:\test\bat>set date=150807
C:\test\bat>for /F %f in ('dir fil* /b') do ren %f "FILE-SRV1.D%date%_%f:~13,8"
C:\test\bat>ren fil150807102600000001.txt "FILE-RV1.D%date%_fil150807102600000001.txt:~13,8"
Le nom de fichier existe déjà, ou le fichier est introuvable.

C:\test\bat>ren fil150807102600000002.txt "FILE-    SRV1.D%date%_fil150807102600000002.txt:~13,8"
Le nom de fichier existe déjà, ou le fichier
est introuvable.

C:\test\bat>ren fil150807102600000003.txt "FILE-SRV1.D%date%_fil150807102600000003.txt:~13,8"
Le nom de fichier existe déjà, ou le fichier
est introuvable.

Please your help will be well appreciated

  • Can you please add translations to the French text present? – LPChip Aug 7 '15 at 13:08
  • Le nom de fichier existe déjà, ou le fichier est introuvable ==> file name already, exist out cannot find the file – khiloc Aug 7 '15 at 13:52
  • -1 for the french, it is utterly unnecessary. You should make your demonstrations of issues, as simple as possible in order to ask your question, and in doing so you may even solve it yourself too. – barlop Aug 7 '15 at 14:06

You are using to many %signs, also it's probably not a good idea to assign to the DATEvariable, and you can't do string operations on FORvariables directly (they are like %%f and you need somethong with a %at the end):

setlocal enabledelayedexpansion
set dat=%date:~8,2%%date:~3,2%%date:~0,2% 
for %%f in (fil*) do (
  SET "FILE=%%f"
  ren "%%f" "FILE-SRV1.D%dat%_!FILE:~13,8!"

(the setlocal enabledelayedexpansion is required to use the ! notation so the FILE variable gets updated each loop iteration.)

  • running this just rename the first file in list to FILE-SRV1.D150807_~13,8 – khiloc Aug 7 '15 at 14:27
  • Yeah, there was still a % too much,corrected. – ths Aug 7 '15 at 17:10
  • Should be for %%f in (fil*) do ( or for /f "delims=" %%f in ('dir /B /A:-D fil*') do ( – JosefZ Aug 7 '15 at 20:32
  • That's super! friends!! IT's working like a charm! Thanks for all – khiloc Aug 10 '15 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.