2

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

Sumit
  • 83
  • 1
  • 3
  • 7
  • What did you try ? We will not do your homework. – nouney Jun 28 '13 at 13:40
  • show an example of TXT file. – Endoro Jun 28 '13 at 13:53
  • according to what you say, you have a thousand files named "nest.txt" with different content. Really?? – Stephan Jun 28 '13 at 18:15
  • Hi Stephan, hope you are doing well. yes, we have 1000 file and each of the description name is different. based on that only i would like to rename the file. Please help me. – Sumit Jun 28 '13 at 18:22
  • possible duplicate of [Renaming file based on its content using Batch file](http://stackoverflow.com/questions/17370879/renaming-file-based-on-its-content-using-batch-file) – TrevorPeyton Jun 28 '13 at 19:14
  • Another variant duplicate: http://stackoverflow.com/questions/17370879/renaming-file-based-on-its-content-using-batch-file – James L. Jun 28 '13 at 20:40

3 Answers3

2

try this:

for /f "skip=1 tokens=1* delims==" %%i in (nest.txt) do ren XYZ.txt "%%~j.txt"
Endoro
  • 37,015
  • 8
  • 50
  • 63
1

This will get you started.

for /f "tokens=1,* delims==" %%A in ('find /i "Description" "nest.txt"') do echo %%B
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • Hi All a small correction here..i apologies for the same. 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 nest.txt file as Man.txt – Sumit Jun 28 '13 at 14:10
1

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?)

Stephan
  • 53,940
  • 10
  • 58
  • 91