Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal EnableExtensions DisableDelayedExpansion
- rem // Define constants here:
- set "_ROOT=%~1" & rem /* (target directory; `.` is current working directory, `%~dp0.` is
- rem parent of this script, `%~1` is first command line argument) */
- set _MASKS="*.rar" "*.mkv" "*.avi" "*.mp4" & rem // (space-separated list of quoted file patterns)
- set _SEPS=" " "." & rem // (space-separated list of quoted separators)
- rem /* Specify multiple `findstr` search strings, including the prefix `/C:`, as you would
- rem directly state them at the `findstr` command, which are used to match the particular
- rem sub-strings of the file names that are used to find the part where to split them at
- rem and to derive the name of the sub-directory where to move the respective file to: */
- set _FILTERS=/C:"^S[0123456789][0123456789]*E[0123456789][0123456789]*$" ^
- /C:"^[0123456789][0123456789]*x[0123456789][0123456789]*$" ^
- /C:"^S[0123456789][0123456789]*$" ^
- /C:"^S[0123456789][0123456789]*E[0123456789][0123456789]-[0123456789][0123456789]*$" ^
- /C:"^1[0123456789]*x[0123456789][0123456789]*$" ^
- /C:"^S[0123456789]*E[0123456789]-[0123456789][0123456789]*$" ^
- /C:"^Stagione*[0123456789]*$" ^
- /C:"^S[0123456789][0123456789]-[0123456789][0123456789]*$" ^
- /C:"^E[0123456789][0123456789]-[0123456789][0123456789]*$" ^
- /C:"^S[0123456789][0123456789]-[0123456789][0123456789]*E[0123456789][0123456789]-[0123456789][0123456789]*$"
- rem // Change into root directory:
- pushd "%_ROOT%" && (
- rem // Loop through all matching files:
- for /F "delims= eol=|" %%F in ('dir /B /A:-D-H-S %%_MASKS%%') do (
- rem // Store current file name and extension, initialise some auxiliary variables:
- set "NAME=%%~nF" & set "EXT=%%~xF" & set "SDIR= " & set "FLAG=#"
- rem // Toggle delayed expansion to avoid trouble with `!` (also later on):
- setlocal EnableDelayedExpansion
- rem // Replace all predefined separators by spaces:
- for %%S in (!_SEPS!) do set "NAME=!NAME:%%~S= !"
- rem // Loop through all space-separated (quoted) items of the file name:
- for %%I in ("!NAME: =" "!") do (
- rem // Skip the loop body when a sub-string has already been found before:
- if defined FLAG (
- rem // Store current portion of the file name:
- endlocal & set "ITEM=%%~I"
- rem // Use `findstr` to match against the predefined sub-strings:
- cmd /V /C echo(!ITEM!| > nul findstr /R /I %_FILTERS% && (
- rem // Match encountered, hence skip this and the remaining items:
- set "FLAG="
- ) || (
- rem /* No match found, so append the current item to the name of the
- rem sub-directory where the file is supposed to be moved then: */
- setlocal EnableDelayedExpansion
- for /F "delims=" %%E in ("!SDIR!!ITEM! ") do (
- endlocal & set "SDIR=%%E"
- )
- )
- setlocal EnableDelayedExpansion
- )
- )
- rem // Process only file naes where sub-directory names could be derived from:
- if not defined FLAG if not "!SDIR:~1,-1!"=="" (
- rem // Create sub-directory, if not yet existing:
- 2> nul mkdir "!SDIR:~1,-1!"
- rem // Move current file into the sub-directory (but do not overwrite in case):
- if not exist "!SDIR:~1,-1!\!NAME!!EXT!" > nul move "!NAME!!EXT!" "!SDIR:~1,-1!\"
- )
- endlocal
- )
- popd
- )
- endlocal
- exit /B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement