Avatar of jonlake
jonlake
Flag for Guernsey asked on

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?
Microsoft DOS

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
Rediers

/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

pritamdutt

Have u tried using /X parameter ?
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Knight

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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
jonlake

ASKER
Thank you to everyone who contributed. Billprew's solution matched precisely what I was after.
Bill Prew

Glad that helped, thanks.

~bp