
The code is simpler if you use my JREN.BAT regular expression renaming utility. If an argument is given, then that value is used as the root folder. If no argument is given, then the script processes starting at the current directory. The command can be put in a script, and then the root folder to process can be passed in as an argument. It will process the tree rooted at the current directory. Putting it all together, the following long one liner can be used directly on the command line.

The %%~nxD returns just the folder name and extension of the parent folder. The EOL and DELIMS options are used to make sure that the full name of every file is preserved. The results of the piped command can be iterated with FOR /F. 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 DIR /B /A-D "folderPath" command can list all files within a folder. The FOR /R command can iterate the folder paths of all folders within a root tree. "fileName.ext" -> "parentFolderName_fileName.ext"

That is not a reasonable expectation unless there are unusual circumstances.īut it is possible to insert the name of the parent folder in front of every file - something like You can only accomplish your stated goal if each folder never has more than one file of any given type.
