Find Text and Rename File

I have a batch file which searches for text within files and renames the files accordingly. The problem is that it doesn't look for exact matches:

@echo off

for /f "delims=" %%a in ('findstr /m "General Ledger Summary Report" c:\Temp\*.prt') do ren "%%a" OFMREP052.prt

If the exact match isn't in the folder then any file with "General Ledger" in it will be renamed.

How can I force the batch file to search for exact matches?
jonlakeAsked:
Who is Participating?
 
Bill PrewCommented:
You will want the /C flag, as in:

@echo off
for /f "delims=" %%a in ('findstr /m /c:"General Ledger Summary Report" c:\Temp\*.prt') do ren "%%a" OFMREP052.prt

Open in new window


If you want to ignore case add the /i option as well.

~bp

0
 
RediersEUS Technology LeadCommented:
/l should do the trick...
See below

@echo off

for /f "delims=" %%a in ('findstr /m /l "General Ledger Summary Report" c:\Temp\*.prt') do ren "%%a" OFMREP052.prt

0
 
pritamduttCommented:
Have u tried using /X parameter ?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Steve KnightIT ConsultancyCommented:
you could just use FIND instead of findstr too:

FIND /I "General ledger......" c:\temp\*.prt

etc.

presumably as you are renaming to one filename there is no danger 2xsource or an existing destination file exists already?
0
 
jonlakeAuthor Commented:
Thank you to everyone who contributed. Billprew's solution matched precisely what I was after.
0
 
Bill PrewCommented:
Glad that helped, thanks.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.