0

I already have a batch file that I can drop in any SHOW_NAME directory and it will move files from a sub-folder to its SEASON parent directory. For example:

F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP1\title_episode1.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP2\title_episode2.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\TITLE_EP3\title_episode3.mkv
F:\TV_SHOWS\SHOW_NAME\SEASON1\title_episode3.mkv

When it moves all files to the parent folder (SEASON1) the "title_episode3.mkv" is a duplicate and overwrites the original. How can I automatically rename by appending a number "title_episode3 (1).mkv"?

Here is the code that I use in a batch file:

@echo off
for /d /r %%f in (*) do (
for /d %%g in ("%%f\*") do (
   for %%h in ("%%~g\*.mkv") do move "%%~h" "%%~f" >nul 2>&1
    )
)

Thanks!

  • The files aren't being moved up one level as in your title; one appears not to be moving at all! This isn't the first time anybody has needed to auto add suffixes to filenames to prevent duplicate issues, did you search this site or anywhere else before asking? – Compo Mar 19 '18 at 18:08
  • @Compo Yes, I have looked for the past few days. I cannot get any solution to work. **CORRECTION:** I place my batch file into the **SHOW_NAME** folder and it does move the files up one level to the parent folder (_SEASON1_, _SEASON2_, etc.) so that Kodi will scrape them. The problem is that it overwrites any duplicates (the `/-Y` only prompts to overwrite, not rename). I have been trying to use this solution [link](https://stackoverflow.com/questions/17387072/using-command-prompt-batch-files-to-move-and-automatically-rename-duplicate-file) but cannot get the two to work together. – user2826193 Mar 20 '18 at 11:20

1 Answers1

1

This commented batch file can be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Search for any file two directory levels below specified directory
rem and pass to subroutine MoveFile the name of the file with full path.

for /D %%A in ("F:\TV_SHOWS\SHOW_NAME\*") do (
    for /D %%B in ("%%A\*") do (
       for /F "delims=" %%I in ('dir "%%B\*" /A-D /B /S 2^>nul') do call :MoveFile "%%I"
    )
)

endlocal
goto :EOF

:MoveFile
set "FilePath=%~dp1"
set "FileNameOnly=%~n1"
set "FileNameFull=%~1"
set "FileName+Ext=%~nx1"
set "FileExtension=%~x1"

rem For files staring with a dot and not containing one more dot.
if "%FileNameOnly%" == "" set "FileNameOnly=%~x1" & set "FileExtension="

rem Get path to parent folder ending with a backslash.
for /F "delims=" %%J in ("%FilePath:~0,-1%") do set "FileParent=%%~dpJ"

rem Uncomment the line below to see the values of the six File* variables.
rem set File & echo/

rem Does a file with current file name not exist in parent folder?
if not exist "%FileParent%%FileName+Ext%" (
    rem Move the file to parent folder and if this was successful
    rem delete the folder of the moved file if being empty now.
    move "%FileNameFull%" "%FileParent%%FileName+Ext%" >nul
    if not errorlevel 1 rd "%FilePath%" 2>nul
    goto :EOF
)

set "FileNumber=1"
:NextFile
if exist "%FileParent%%FileNameOnly% (%FileNumber%)%FileExtension%" set /A "FileNumber+=1" & goto NextFile

move "%FileNameFull%" "%FileParent%%FileNameOnly% (%FileNumber%)%FileExtension%" >nul
if not errorlevel 1 rd "%FilePath%" 2>nul
goto :EOF

Running the batch file a second time on same directory with no new subdirectory and no new file does not change anything.

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

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • rd /?
  • rem /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143