Friday, December 28, 2018

batch - Windows CMD - How to rename all files in all subfolders to parent folder name


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 

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!


Answer



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_*"

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...