Sumit
Sumit

Reputation: 83

Rename file based on file Content batch file

I need a batch file that reads the description name present in the XYZ.txt file and rename that file name based on description name.

For example i have a file name called "nest.txt" and when we open the text file(nest.txt) the second line of the file name has Description(Description=Man) then the batch file should rename my XYZ.txt file as Man.txt

i have 1000 files to rename based on the above condition. Please help me

Upvotes: 2

Views: 3234

Answers (3)

Stephan
Stephan

Reputation: 56155

I assume, you have 1000 files with 1000 different names.

@echo off
for /f %%i in ('dir /b *.txt') do (
  for /f "skip=1 tokens=1* delims==" %%j in ( %%i ) do ( 
    if "%%j"=="Description" echo ren "%%i" "%%k.txt" 
  )
)

This will not work on files which have spaces in their (original) filename, but I don't understand, why. (%%i contains only the part before the first space). Maybe someone other can help with this (I'm sure, it's only a minor change, but I can't find it)

Remove the "echo", if the output fits your needs.

(are you sure that all 1000 files have 1000 different Descriptions?)

Upvotes: 1

Endoro
Endoro

Reputation: 37569

try this:

for /f "skip=1 tokens=1* delims==" %%i in (nest.txt) do ren XYZ.txt "%%~j.txt"

Upvotes: 2

David Ruhmann
David Ruhmann

Reputation: 11367

This will get you started.

for /f "tokens=1,* delims==" %%A in ('find /i "Description" "nest.txt"') do echo %%B

Upvotes: 1

Related Questions