Change filenames with command line in Windows 7

Posted on 2012-08-20
Last Modified: 2012-08-21
I have a bunch of files that I need to change the names, and I want to be able to do this through a command line,

What I have are

01 - Filename 1.txt
08 - This file.txt
15 - That filename.txt
27 - Randome file.txt
32 - This name is.txt

and so on.  I want to replace the leading #'s in front of the "hyphen" with the words "Folder Name" then "hyphen" and the rest stay as is.  The command I've used

REN "* - *.*" ""Folder Name" - *.*"

but what it did was putting the "Folder Name" and "hyphen" in place, and removed the rest of the text.  How could I achive what I want with this simple command?  Please advise.  Thanks.
Question by:swgit
    LVL 9

    Expert Comment

    by:Evan Cutler
    I use this...
    has preview window and everything.
    It's perfect for me.
    Hope it's good for you too:'s free...I like free ;)
    LVL 95

    Expert Comment

    by:Lee W, MVP
    If I understand you, then this command should do the trick:

    for /f "tokens=1* delims=-" %a in ('dir /b /a-d') do @ren "%a -%b" "Folder Name -%b"

    Test in a sample directory first and if it doesn't work, post a screen shot.
    LVL 1

    Author Comment

    @arcee123 - i'm still testing this, will let you know soon
    @leew - please see attached screenshot screenshot.
    LVL 95

    Accepted Solution

    Silly mistake on my part - take out the space after %a in the ren command:

    for /f "tokens=1* delims=-" %a in ('dir /a-d /b') do @ren "%a-%b" "Folder name -%b"

    And in the future (I didn't mention this before), but when executing a command in a for loop and you have problems, echo it back - example, in debugging this, I simply added "echo " to see what command WOULD have been run and from there it was obvious what the problem was:

    for /f "tokens=1* delims=-" %a in ('dir /a-d /b') do @echo ren "%a-%b" "Folder name -%b"
    LVL 1

    Author Closing Comment

    @leew - that works perfectly.  thank you.

    @arcee123 - thank you for your help on this.  your utility works okay.  however, i needed something that i could include in a batch.  and the utility doesn't work so well if the leading numbers, in front of the hyphen, are not the same total group, i.e. file 1 has 1 # before hyphen, file 2 has 3 #'s, file 3 has 6 #'s, etc., there's not a way to remove them correctly without trimming other parts of the name.
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Reminder - in a batch file, you need to double the % -
    for /f "tokens=1* delims=-" %%a in ('dir /a-d /b') do @ren "%%a-%%b" "Folder name -%%b"
    LVL 1

    Author Comment

    yes, i did do that from noting the instructions through "for /?" :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    The Display applet of Windows 7 Control Panel has changed a great deal since Windows XP  (it was missing and more or less replaced in Windows Vista by the Personalization applet.)  Below is a screenshot of what the Display applet of Windows XP, whic…
    So many times I have seen the words written in a question "if only I could show you" or " I know how hard it is for you since you can't see it" in any zone. That has inspired me to write about this tool in windows 7 called "Problem Steps Recorder…
    This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
    This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now