dropbox1349

Script: riordinare le serie tv scaricate da emule ognuna nella propria cartella

Jan 15th, 2021
1,037
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2. setlocal EnableExtensions DisableDelayedExpansion
  3.  
  4. rem // Define constants here:
  5. set "_ROOT=%~1"    & rem /* (target directory; `.` is current working directory, `%~dp0.` is
  6.                     rem     parent of this script, `%~1` is first command line argument) */
  7. set _MASKS="*.rar" "*.mkv" "*.avi" "*.mp4" & rem // (space-separated list of quoted file patterns)
  8. set _SEPS=" " "."  & rem // (space-separated list of quoted separators)
  9. rem /* Specify multiple `findstr` search strings, including the prefix `/C:`, as you would
  10. rem    directly state them at the `findstr` command, which are used to match the particular
  11. rem    sub-strings of the file names that are used to find the part where to split them at
  12. rem    and to derive the name of the sub-directory where to move the respective file to: */
  13. set _FILTERS=/C:"^S[0123456789][0123456789]*E[0123456789][0123456789]*$" ^
  14.               /C:"^[0123456789][0123456789]*x[0123456789][0123456789]*$" ^
  15.                /C:"^S[0123456789][0123456789]*$" ^
  16.                 /C:"^S[0123456789][0123456789]*E[0123456789][0123456789]-[0123456789][0123456789]*$" ^
  17.                  /C:"^1[0123456789]*x[0123456789][0123456789]*$" ^
  18.                   /C:"^S[0123456789]*E[0123456789]-[0123456789][0123456789]*$" ^
  19.                    /C:"^Stagione*[0123456789]*$" ^
  20.                      /C:"^S[0123456789][0123456789]-[0123456789][0123456789]*$" ^
  21.                       /C:"^E[0123456789][0123456789]-[0123456789][0123456789]*$" ^
  22.                        /C:"^S[0123456789][0123456789]-[0123456789][0123456789]*E[0123456789][0123456789]-[0123456789][0123456789]*$"
  23.                    
  24.  
  25. rem // Change into root directory:
  26. pushd "%_ROOT%" && (
  27.    rem // Loop through all matching files:
  28.     for /F "delims= eol=|" %%F in ('dir /B /A:-D-H-S %%_MASKS%%') do (
  29.        rem // Store current file name and extension, initialise some auxiliary variables:
  30.         set "NAME=%%~nF" & set "EXT=%%~xF" & set "SDIR= " & set "FLAG=#"
  31.        rem // Toggle delayed expansion to avoid trouble with `!` (also later on):
  32.         setlocal EnableDelayedExpansion
  33.        rem // Replace all predefined separators by spaces:
  34.         for %%S in (!_SEPS!) do set "NAME=!NAME:%%~S= !"
  35.        rem // Loop through all space-separated (quoted) items of the file name:
  36.         for %%I in ("!NAME: =" "!") do (
  37.            rem // Skip the loop body when a sub-string has already been found before:
  38.             if defined FLAG (
  39.                rem // Store current portion of the file name:
  40.                 endlocal & set "ITEM=%%~I"
  41.                rem // Use `findstr` to match against the predefined sub-strings:
  42.                 cmd /V /C echo(!ITEM!| > nul findstr /R /I %_FILTERS% && (
  43.                    rem // Match encountered, hence skip this and the remaining items:
  44.                     set "FLAG="
  45.                 ) || (
  46.                    rem /* No match found, so append the current item to the name of the
  47.                    rem    sub-directory where the file is supposed to be moved then: */
  48.                     setlocal EnableDelayedExpansion
  49.                     for /F "delims=" %%E in ("!SDIR!!ITEM! ") do (
  50.                         endlocal & set "SDIR=%%E"
  51.                     )
  52.                 )
  53.                 setlocal EnableDelayedExpansion
  54.             )
  55.         )
  56.        rem // Process only file naes where sub-directory names could be derived from:
  57.         if not defined FLAG if not "!SDIR:~1,-1!"=="" (
  58.            rem // Create sub-directory, if not yet existing:
  59.              2> nul mkdir "!SDIR:~1,-1!"
  60.            rem // Move current file into the sub-directory (but do not overwrite in case):
  61.              if not exist "!SDIR:~1,-1!\!NAME!!EXT!" > nul move "!NAME!!EXT!" "!SDIR:~1,-1!\"
  62.         )
  63.         endlocal
  64.     )
  65.     popd
  66. )
  67.  
  68. endlocal
  69. exit /B
  70.  
  71.  
RAW Paste Data