Renaming multiple files inside a folder using Windows batch commands

01
2014-07
  • user3413088

    This is taking the post located here and spinning things up a bit. As typical, I'm trying to rename several images inside a folder to be the name of the folder they're in and then add the suffix "photo1", "photo2" etc.

    In other words, I'm trying to go from here:

    Folder1
      IMG_001.jpg
      IMG_001.jpg
      IMG_003.jpg
    

    To here:

    Folder1
      Folder1_photo1.jpg
      Folder1_photo2.jpg
      Folder1_photo3.jpg
    

    Catch is there several hundred of these "Folder" folders, and each one of these will need to have the photos inside of it renamed.

    I know there is 3rd party software out there that can do this but I'm looking for a way to run this as a Windows .bat.

    If anybody has an idea please share. Thanks for your time.

  • Answers
  • dbenham

    Assuming all folders are in the same root folder, and all images match the template IMG_n.jpg, then the following one liner should work on the command line:

    for /d %A in ("yourRootPath\*") do @for %B in ("%A\img_*.jpg") do @for /f "tokens=1* delims=_0" %C in ("%~nB") do ren "%B" "%~nxA_photo%D.jpg"
    

    Don't forget to double up the percents if you use the command in a batch script.

  • nixda

    I see you asked for Batch, but others may find PowerShell useful

    Get-Childitem "C:\my\folder\" -Recurse -Include *.jpg | ForEach {             
        Rename-Item $_ "$($_.Directory.Name)_$($_.Name -ireplace "img_(0+)", "photo").jpg
    }
    
  • TessellatingHeckler

    How close do you need to get?

    This should do it, but it will keep the 0 in the numbers, e.g. folder1_photo001.jpg instead of folder1_photo1.jpg:

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    
    for /d %%d in (*) do (
    
        for %%f in ("%%d/img_*.jpg") do (
            set fname=%%f
            set fname=!fname:IMG_=photo!
            echo ren "%%d/%%f" "%%d/%%d_!fname!"
        )
    )
    

    (That will print the renaming commands it would run. Remove echo to have it actually run them. Have a backup first, verify you're happy with it, there's no undo, etc.).

    I can't think of a nice alternative if you do need 1 instead of 001. Replacing the 00 out would risk changing names where there's a number in the name as well, and counting up would break if the numbers aren't contiguous.

    [edit: pinched from another answer, I was assuming the pictures are the only things in the folders. Now looks only for img_*.jpg pattern].

  • MatthewThepc

    A much simpler approach. I'm not sure if this works, but try it out .

    cd "path of the folder that contains the photos"
    
    for /L %%n in (1 1 %random%) do (
    
        for %%f in (*.jpg) do (
    
            rename "%%f" "photo_%%n.jpg"
    
        )
    
    )
    
  • RecentCoin

    Look at some software called Irfanview. It's got a lot of sophisticated bulk rename, resize, conversion, etc. utilities built into it. I think it will do what you want.


  • Related Question

    Windows batch file: rename files (possibly in multiple folders) based on input file (of target filenames)
  • cMP

    I am a Batch-newbie...

    This "tool" is to automate the slimming down of Windows (XP) by disabling certain system driver, DLL and EXE files. Instead of outright deletion, I wish to rename-in-place, thus "removing" them from the OS, but not losing sight of where they belong (should any need to be "restored"). Renaming is accomplished by appending a new suffix to the existing filename (eg: "wdmaud.drv.group_1") The renaming suffix should be another input variable.

    The target-list is approx. 1100 files long (divided into various groups/phases), so manual renaming is out of the question. Each group will be processed in a separate run of the batch file, varying the target-list input file for each execution.

    Target-list is plain text file, one filename per line (no other data in the files). Number of entries per group varies. Target list will look like this:

        -- example start --
        netapi.dll
        netcfgx.dll 
        netdde.exe 
        netevent.dll 
        nic1394.sys
        -- example end --
    

    Filenames may be in UPPER, lower, or MiXeD case. The files may be present in more than one folder in the C:\Windows hierarchy - or may not be present at all. If a file is not found anywhere in the system, it's name should be written to a text file, one-entry-per-line.

    The specific folders of interest are:

        C:\WINDOWS\
        C:\WINDOWS\system\
        C:\WINDOWS\system32\
        C:\WINDOWS\system32\dllcache
        C:\WINDOWS\system32\drivers
    

    ...but may change as development proceeds.

    Based on a reply at stackoverflow.com, I've got started thus:

        @echo off
    
        set suffix=GROUP_1
        set targetlist=GROUP_1.txt
        set dirlist=folders.txt
    
        for /f "tokens=*" %%f in (%targetlist%) do (
          for /f "tokens=*" %%d in (%dirlist%) do (
            if exist "%%d\%%f" ren "%%d\%%f.%%suffix"
               echo %%f found in %%d >> foundlist.txt
          )
        )
    

    ==============================================================================

        :: -----------------------------------------------------------------::
        :: Batch Process to Rename-In-Place System Files from an Input List ::
        :: -----------------------------------------------------------------::
    
        @echo off
        :: >> clear files from previous run <<
        if exist RENAMED_files.txt  DEL RENAMED_files.txt
        if exist NOTFound_files.txt DEL NOTFound_files.txt
    
        ::  >> file rename-suffix reflects step name <<
        set suffix=Steppe_01
    
        ::  >> target file list to rename <<
        set targetlist=Steppe_01_files.txt
    
        ::  >> list of folders to search  <<
        set dirlist=folders.txt
    
        ::  >> PROCESS <<
    
        for /f "tokens=*" %%f in (%targetlist%) do (
          echo. >> NOTFound_files.txt
          for /f "tokens=*" %%d in (%dirlist%) do (
            if NOT exist "%%d\%%f"          echo %%f  not in %%d >> NOTFound_files.txt
            if exist     "%%d\%%f"          REN "%%d\%%f" "%%f.%suffix%"
            if exist     "%%d\%%f.%suffix%" echo renamed  %%f  in %%d >> RENAMED_files.txt
          )
        )
    
        ::  >> end of process <<
    

  • Related Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.