Page 1 of 1

renaming files based on content

Posted: 08 Mar 2011 09:47
by cactusman252
Hey guys, totally new to batch scripting. I have tons of file that need to be renames based on the user name, which is located in the file. for example the file content is such:

Directory of Y:\Bob\My Documents

14/01/2011 01:59 PM 10,411,520 MVI_1903_1.avi
1 File(s) 10,411,520 bytes



The file is named file1 but i need to rename it bob.txt
this seems simple and would make things a lot easier. thanks in advance :D

Re: renaming files based on content

Posted: 08 Mar 2011 12:30
by aGerman
Too less informations. You've tons of files -- OK, but where? All in the same directory? All with the same file extension?

Generally (for a single file, e.g. "file1.txt"):

Code: Select all

for /f "tokens=2*" %%a in ('findstr /c:"Directory of " "file1.txt"') do (
  for /f "tokens=2 delims=\" %%c in ("%%b") do echo %%c
)

This should return "Bob" in your case.

Regards
aGerman

Re: renaming files based on content

Posted: 08 Mar 2011 14:20
by cactusman252
Directory is in the root of the Y: drive, all files are in the same directory, all with the extension of .txt
This did the trick, thanks again :)

for /f "tokens=2*" %%a in ('findstr /c:"Directory of " "*.txt"') do (
for /f "tokens=2 delims=\" %%c in ("%%b") do Ren *.txt %%c.txt
)