-1

I need a batch file that reads the description name present in the nest.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(say Description=Man) then the batch file should rename my nest.txt file as Man.txt

Open a file nest.txt Copy the description name present in file nest.txt

Header
Description=MAN
Menu KeyWords=MAN_ABC

Now close this file and rename file nest.txt to MAN.txt

James L.
  • 9,384
  • 5
  • 38
  • 77
Sumit
  • 83
  • 1
  • 3
  • 7
  • How do you identify which files need to be opened/renamed? For example, could it be more than one file (e.g., *.txt)? – James L. Jun 28 '13 at 18:25
  • yes we have multiple file with different file name and each file has different description. so each file should be renamed based on its description name. Please help me. – Sumit Jun 28 '13 at 18:27
  • What you are asking for is not difficult, but it is problematic because using a file mask like *.txt will find files again after they are renamed. I'll type an answer that shows you how to do what you are asking, and avoid the file mask problems. – James L. Jun 28 '13 at 18:33
  • Please provide some tips. – Sumit Jun 28 '13 at 18:35
  • Would it just be those three lines or would it be more? – TrevorPeyton Jun 28 '13 at 18:39
  • it has more lines, however only second line has Description name that is fixed. PLease make sure that the file name should be the same as Description name as explained in above example. nest.txt to MAN.txt please revert for any questions – Sumit Jun 28 '13 at 18:42
  • The answer isolates the `Description=` line in the file, ignoring the other lines. It does not do any error checking however, so I'm not sure what will happen if a file doesn't have `Description=` in it or if the value after the `=` is blank. – James L. Jun 28 '13 at 18:48
  • 1
    possible duplicate of [Rename file based on file Content batch file](http://stackoverflow.com/questions/17366009/rename-file-based-on-file-content-batch-file) – Endoro Jun 28 '13 at 19:17
  • 1
    @Endoro More like exact duplicate. – TrevorPeyton Jun 28 '13 at 19:25
  • 1
    Another duplicate: http://stackoverflow.com/questions/17372805/modify-file-content-using-batch-file – James L. Jun 28 '13 at 20:39

1 Answers1

0

The following batch file takes one command line parameter and must be run from the folder where the files to be renamed exist. You could easily add more code to the batch file to make it more intelligent (e.g., change to the folder, hard code *.txt in place of %1, etc.).

So, if the batch file is called fixfilenames.bat and is in the same folder with the TXT files, from a command prompt, type fixfilenames *.txt and it will rename the files first to have the extension temp_txt so that the for loop won't pick up the files again after they are renamed. Then when it is done, it renames all temp_txt files to txt files.

@echo off

for %%i in (%1) do (
  for /f "tokens=2 delims==" %%j in ('findstr /B /I "Description=" "%%i"') do (
    ren "%%i" "%%j.temp_txt"
  )
)

ren *.temp_txt *.txt
James L.
  • 9,384
  • 5
  • 38
  • 77
  • James it is working fine for normal text description however when we have a description name with the special character such as linéaire, it fails. Can you please modify and help me? – Sumit Jun 28 '13 at 19:15
  • Sorry. I don't know much about extended characters and filenames. Try searching for extended characters, batch file, and code page. – James L. Jun 28 '13 at 20:30
  • Hey @JamesL. I came across this ans of yours while searching for a similar requirement, I am completely new to batch scripting so would you be kind enough to tell me what `for /f "tokens=2 delims==" %%j` does ? – Wilfred Clement May 31 '20 at 04:56
  • @WilfredClement : `delims==` tells the parser to take the output from `findstr /B /I "Description="` and split it on the equals sign. The first part will therefore be "Description" and the second part will be the value after the equals sign. `tokens=2` makes the parser assign the second value (the part after the equal sign) to `%%j`. You can use `%%j` in the `do` section of the `for` statement. In this case, the OP wanted to rename the file (`%%i.txt`) to be a new filename (`%%j.txt`). – James L. Jun 01 '20 at 17:44