Save this as a batch file in the root of your tree:
@echo off
for /r /d %%x in (*) do (
pushd "%%x"
echo %%x
for /F "delims=" %%i in ("%%x") do (
ren *.mkv "%%~ni.mkv" 2> NUL
ren *.avi "%%~ni.avi" 2> NUL
ren *.mp4 "%%~ni.mp4" 2> NUL
)
popd
)
Disclaimer: I bear no responsibility for any damage caused.
Though I've tested it on the list you gave, I highly recommend that you back up the files before running the batch file.
Okay, I finally figured it out by adapting code from yet another question. Thanks to @AFH for clarifying the parts I was unsure about in the comments to this answer, and for his general help with it - it's much appreciated.
To Extract All Files from Subfolders to their Parent Folder
for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER"
Remember to replace all instances of PARENTFOLDER
with the path to the folder that you want to extract the files to.
To Extract All Files from Subfolders and Delete Empty Subfolders
It's unlikely you'll need to keep the empty subfolders left over after extracting the files from them - I certainly didn't - so the below command automates the deletion of them, too.
for /r "PARENTFOLDER" %d in (*.*) do move "%d" "PARENTFOLDER" && cd "PARENTFOLDER" && for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"
Once again, replace all instances of PARENTFOLDER
with the path to the folder that you want to extract the files to.
By this point, however, it's no longer a one-liner, and starts to get a bit convoluted when pasted into the command line, so it's easier to just put the whole thing in a batch file. Using variables for the path to the parent folder allows you to replace just the one instance of PARENTFOLDER
at the beginning of the file, and it's also the safer option, preventing against any accidental deletion of empty folders that you might want to keep.
The Batch File
Paste into a text file, replace PARENTFOLDER
with the path to the folder you want it to work with, and save it with the .bat
extension. Run in any directory.
@ECHO OFF
SETLOCAL
SET parent="PARENTFOLDER"
CD /d %parent%
FOR /r %parent% %%d IN (*.*) DO MOVE "%%d" %parent%
FOR /f "delims=" %%d IN ('DIR /a:d /s /b ^| SORT /r') DO RD "%%d"
ECHO Done. Press any key to terminate script.
PAUSE >NUL
Best 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.
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.
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 /?
, orjren /??
if you want paged output.The following simple command will process the tree rooted at the current directory:
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):