-2

thanks in advance for your help. I have a folder with a ton of randomly named subfolders. Within those subfolders are a number of files with numbered file names. For example..

\ParentFolder\UniqueFolderName\0001.ogg
\ParentFolder\UniqueFolderName\0002.ogg
\ParentFolder\UniqueFolderName\0003.ogg
\ParentFolder\DifferentFolderName\0001.ogg
\ParentFolder\DifferentFolderName\0002.ogg
\ParentFolder\DifferentFolderName\0003.ogg

I'd like to run a batch file from \ParentFolder\ that can rename the .ogg files to inherit the unique folder name and end up with something like this...

\ParentFolder\UniqueFolderName\UniqueFolderName - 0001.ogg
\ParentFolder\UniqueFolderName\UniqueFolderName - 0002.ogg
\ParentFolder\UniqueFolderName\UniqueFolderName - 0003.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0001.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0002.ogg
\ParentFolder\DifferentFolderName\DifferentFolderName - 0003.ogg

This is probably super simple stuff. But my tiny brain can't figure out how to do it without putting the batch file in every single subfolder.

Slynk
  • 15
  • 2

1 Answers1

0

This could be done with following commented batch code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Run in a separate command process started by FOR with cmd.exe /C the
rem command DIR to get a list of non-hidden *.ogg files in bare format
rem with just full qualified file name (drive + path + name + extension)
rem in specified directory and all its subdirectories.

rem This list is captured by FOR and processed line by line with disabling
rem the default line splitting behavior of __FOR__. The subroutine RenameFile
rem is called with each full qualified file name enclosed in double quotes.

for /F "delims=" %%I in ('dir "\ParentFolder\*.ogg" /A-D-H /B /S 2^>nul') do call :RenameFile "%%I"

rem Restore initial environment resulting in deletion of all
rem environment variables used in the subroutine below.
endlocal

rem Exit processing of this batch file with suppressing the
rem error message output on command extensions are disabled
rem in the initial environment on starting the bath file.
exit /B 2>nul


:RenameFile
rem Get just file name without drive, path and extension.
set "FileName=%~n1"

rem Get just drive and path of file name ending with a backslash.
set "FilePath=%~dp1"

rem Get name of folder containing the file.
for %%J in ("%FilePath:~0,-1%") do set "FolderName=%%~nxJ"

rem Exit the subroutine if file name contains already the folder name.
echo "%FileName%" | %SystemRoot%\System32\find.exe /I "%FolderName% - " >nul 2>nul
if not errorlevel 1 goto :EOF

rem Exit the subroutine if there is already a file or folder
rem with new file name in the folder of the current file.
if exist "%FilePath%%FolderName% - %FileName%%~x1" goto :EOF

rem Rename the file by prepending it with folder name, space, hyphen, space.
ren %1 "%FolderName% - %FileName%%~x1"
goto :EOF

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • dir /?
  • echo /?
  • endlocal /?
  • exit /?
  • find /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • ren /?
  • setlocal /?

See also: Where does GOTO :EOF return to?

Mofi
  • 46,139
  • 17
  • 80
  • 143