📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Utility to add prefix to all filenames in a folder

Options
There are dozens of utilities available which will perform various batch file operations, so I am hoping that someone can recommend one. It might help if I explain what I am trying to do. I want to add the prefix [CD] to all filenames in certain folders within My Music. This is to enable me to exclude these from my regular backup - I don't need to back up mp3 files which are themselves backups of actual CDs. I am running XP pro. Hope this is clear - tell me if not.
«1

Comments

  • geewhiz
    geewhiz Posts: 1,129 Forumite
    Part of the Furniture 1,000 Posts Combo Breaker
    Have a look at this, it should do what you want.
  • weegie.geek
    weegie.geek Posts: 3,432 Forumite
    'mon the Totalcommander!
    They say it's genetic, they say he can't help it, they say you can catch it - but sometimes you're born with it
  • DatabaseError
    DatabaseError Posts: 4,161 Forumite
    get a dosprompt, goto the folder, REN *.* *.cd
    done
    Utinam logica falsa tuam philosophiam totam suffodiant.
  • John_Gray
    John_Gray Posts: 5,844 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Photogenic
    You missed the word prefix in the OP.

    It would also have been good to know that you had tested it before posting.
  • DatabaseError
    DatabaseError Posts: 4,161 Forumite
    oops
    .......ren *.* cd*.*

    :)
    Utinam logica falsa tuam philosophiam totam suffodiant.
  • John_Gray
    John_Gray Posts: 5,844 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Photogenic
    Did you test it? I did. It doesn't work...

    I then tried

    for %a in (*.*) do ren "%a" "CD%a"

    and this did work. No idea why your version didn't!
  • John_Gray wrote: »
    for %a in (*.*) do ren "%a" "CD%a"

    Thanks, I tried this command and it worked. Is there an additional instruction I can add to the command which will perform the rename for every file in every sub folder if I start at "my documents\my music\CD backups"? There are hundreds of sub folders and it will take ages to type a change directory command for every sub folder before running the command .

    Alternatively, is there any way of opening a command prompt window which is set to the folder currently being viewed in windows explorer?
  • add to the end /s

    %a in (*.*) do ren "%a" "CD%a" /s

    might do it
  • John_Gray
    John_Gray Posts: 5,844 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Photogenic
    Unfortunately the REN command doesn't have a /S switch. You will need something rather more complicated.

    I really suggest you make a complete copy of your CD Backups directory and all subdirectories "somewhere safe" before you start, because it will be very messy to recover if anything goes wrong! Windows Explorer is what I'd use, myself.

    You seem quite confident with command prompts and NOTEPAD (I hope!) so cut and paste the following lines into a file called (say) PREFIXCD.BAT

    :: rename all files in a directory/subdirectory path to have CD at the front
    setlocal
    cd "%userprofile%\my documents\my music\CD backups"
    PAUSE
    set log=%~n0.log
    if exist %log% del %log%
    for /r %%a in (*.*) do ECHO ren "%%~fa" "CD%%~nxa" >> %log%
    notepad %log%
    endlocal


    This BATch file can go anywhere you like (within reason!), and the log file is put in your "CD Backups" directory.

    Run PREFIXCD.BAT once, and check that the CD command gives you the correct results (see the PAUSE command on-screen) then review the log file which results, as displayed in NOTEPAD*. If you are happy with what comes out, edit the BATch file and remove the ECHO word just in front of the ren. This will cause it to actually do the job when you run PREFIXCD.BAT again.

    Good luck!

    * I just copied a few random directories and files into a CD Backups directory on my machine, and one of the resulting renames was

    ren "C:\Documents and Settings\John\My Documents\My Music\CD Backups\SysInternals\Copy of website as at 22JUL2006\AccessChk.html" "CDAccessChk.html"

    (this single line may wrap badly!).

    LATER
    Please note that this will rename every single file in CD Backups and below to have the CD prefix. Please give more details if this is NOT what you want...
  • Thanks John for the reply posted above - I will try this! I had got to the stage where I thought I was going to place a copy of cmd.exe in every folder, so as to get the command line to show that directory. I would then have pasted in for %a in (*.*) do ren "%a" "CD%a" and run it, before closing the command prompt window and repeating the process in the next fiolder, and the next, and the next...
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 351.2K Banking & Borrowing
  • 253.2K Reduce Debt & Boost Income
  • 453.7K Spending & Discounts
  • 244.2K Work, Benefits & Business
  • 599.3K Mortgages, Homes & Bills
  • 177K Life & Family
  • 257.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.2K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.