1

I have looked in this issue in a 3 years old question but couldn't adapt the batch script to work with my case: Rename text files based on multiple strings in their contents

This is what my text files contains:

<!-- ===============================================
TEXT TEXT"
===============================================
An : ONE
Ca : ONE - AVRIL 2016 
Site : example.com -->

<!--===============================================
Insertion : example.com - Bannières Mobile 300X250 VF (300x250)

I want to rename every file with what the line Insertion : contains. In my example it would be : example.com - Bannières Mobile 300X250 VF (300x250)

I tried to use this script and since it used Date and Time it the original question but I don't know how to make it work

setlocal enabledelayedexpansion
REM for all files do:
for %%f in (*.txt) do (
  REM get desired lines and set variables:
  for /f "tokens=2,4 delims=:" %%i in (' findstr "^Insertion" %%f') do set %%i=%%j
  REM construct filename:
  set "name=!Insertion!.txt"
  echo Filename is: "!name!"
)

Can someone please help me adapting the code ?

1 Answers1

1

Perhaps something like this will do:

For /F "Tokens=1,2*Delims=:" %%A In ('FindStr/LBIC:"Insertion : " *.txt'
) Do For /F "Tokens=*" %%D In ("%%~C"
) Do If Not Exist "%%~D%%~xA" Ren "%%A" "%%~D%%~xA"

If you get cyclic issues, (loop reading the files it's just renamed), then add an extra little step:

For /F "Tokens=1,2*Delims=:" %%A In ('FindStr/LBIC:"Insertion : " *.txt'
) Do For /F "Tokens=*" %%D In ("%%~C"
) Do If Not Exist "%%~D%%~xA_" Ren "%%A" "%%~D%%~xA_"
Ren *.txt_ *.txt

Note: If you change the *.txt to include a full path, you will need to adjust the tokenising to cater for the additional delimiter.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks for your help, the second code works well, the only thing is that some characters like `é` in `example.com - Bannières Mobile 300X250 VF (300x250)` shows like this `Banni├¿res` Other than that everything is great, thank you very much for your help – penguingoldfishhorselion May 05 '17 at 21:18