Alright scripters,

Im still trying to wrap my head around the FOR command. What Id like to do is rename all files in all subfolders to the folder name they reside in, retaining their extensions. Id imagine it could be done with something like:

   For /R %X in (*.*) do REN %X <argument for parent folder name>

Maybe Im wrong. Whats the correct way to do this? And it must be a script. Im not asking about Bulk Rename Utility or File Renamer Deluxe, etc.

Thanks for your time!

  • 1
    I think heat stroke must be interfering with you ability to think. Obviously you cannot do what you ask, unless you never have more than one file of any given type in all your folders. – dbenham Jul 24 '16 at 13:19

You can only accomplish your stated goal if each folder never has more than one file of any given type. That is not a reasonable expectation unless there are unusual circumstances.

But it is possible to insert the name of the parent folder in front of every file - something like
"fileName.ext" --> "parentFolderName_fileName.ext"

The FOR /R command can iterate the folder paths of all folders within a root tree.

The DIR /B /A-D "folderPath" command can list all files within a folder. But you don't want to rename files that aready begin with the parent folder name, so that can be piped to FINDSTR to exclude the already renamed files.

The results of the piped command can be iterated with FOR /F. The EOL and DELIMS options are used to make sure that the full name of every file is preserved.

The %%~nxD returns just the folder name and extension of the parent folder.

Putting it all together, the following long one liner can be used directly on the command line. It will process the tree rooted at the current directory.

for /r %D in (.) do @for /f "eol=: delims=" %F in ('dir /b /a-d "%D" 2^>nul^|findstr /vbic:"%~nxD_"') do ren "%D\%F" "%~nxD_%F"

The command can be put in a script, and then the root folder to process can be passed in as an argument. If no argument is given, then the script processes starting at the current directory. If an argument is given, then that value is used as the root folder.

@for /r %1 %%D in (.) do @for /f "eol=: delims=" %%F in (
  'dir /b /a-d "%%D" 2^>nul^|findstr /vbic:"%%~nxD_"'
) do ren "%%D\%%F" "%%~nxD_%%F"

The code is simpler if you use my JREN.BAT regular expression renaming utility. It is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe files are needed.

Full documentation is embedded within the utility - accessed via jren /?, or jren /?? if you want paged output.

The following simple command will process the tree rooted at the current directory:

for /r %F in (.) do @call jren "^" "%~nxF_" /p "%F" /fx "%~nxF_*"

The first two JREN arguments are the find/replace strings - The search matches the beginning of the file name, and the replace simply insert the parent folder name (with a trailing underscore). The /P "%F" option specifies the root folder. And the /FX "%~nxF_* option excludes files that already begin with the name of the parent folder, followed by an underscore.

The command can be put in a batch script that accepts an optional root folder as an argument (process current directory if no value given):

@echo off
for /r %1 %%F in (.) do call jren "^" "%%~nxF_" /p "%%F" /fx "%%~nxF_*"
  • Wow. Great utility and lots of good information too, thank you Mr. Benham. This works quite well. – Heatseeker86 Jul 24 '16 at 20:14

Save as *.bat

@echo off
for /r %%D in (.) do (
  for /f "delims=" %%F in ('dir /b /A:-D "%%D"') do echo "%%D\%%F" "%%~nxD%%~xF"
@echo off
for /r %%F in (*) do (
  for /f "delims=" %%D in ("%%~dpF.") do echo "%%F" "%%~nxD%%~xF"

Verify that this is what you want before replacing echo with ren.

For - Looping commands
Parameter Extensions

  • Even though dbenham's suggestion works too, this also does exactly what I want. Thank you very much. – Heatseeker86 Jul 24 '16 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.