renaming files based on content

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

renaming files based on content

#1 Post by cactusman252 » 08 Mar 2011 09:47

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: renaming files based on content

#2 Post by aGerman » 08 Mar 2011 12:30

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

cactusman252
Posts: 10
Joined: 08 Mar 2011 08:54

Re: renaming files based on content

#3 Post by cactusman252 » 08 Mar 2011 14:20

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
)

Post Reply