Find Text and Rename File

Posted on 2011-10-05
Medium Priority
Last Modified: 2012-05-12
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?
Question by:jonlake

Expert Comment

ID: 36916333
/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


Expert Comment

ID: 36916343
Have u tried using /X parameter ?
LVL 59

Accepted Solution

Bill Prew earned 2000 total points
ID: 36916363
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.


NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

LVL 43

Expert Comment

by:Steve Knight
ID: 36920152
you could just use FIND instead of findstr too:

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


presumably as you are renaming to one filename there is no danger 2xsource or an existing destination file exists already?

Author Closing Comment

ID: 36920415
Thank you to everyone who contributed. Billprew's solution matched precisely what I was after.
LVL 59

Expert Comment

by:Bill Prew
ID: 36920630
Glad that helped, thanks.


Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question