Find Text and Rename File

Posted on 2011-10-05
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
    LVL 3

    Expert Comment

    /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

    LVL 9

    Expert Comment

    Have u tried using /X parameter ?
    LVL 51

    Accepted Solution

    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.


    LVL 43

    Expert Comment

    by:Steve Knight
    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

    Thank you to everyone who contributed. Billprew's solution matched precisely what I was after.
    LVL 51

    Expert Comment

    by:Bill Prew
    Glad that helped, thanks.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now