procs.cmd 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. @echo off
  2. setlocal EnableExtensions
  3. setlocal EnableDelayedExpansion
  4. if "%~1" neq "" (
  5. 2>nul >nul findstr /rc:"^ *:%~1\>" "%~f0" && (
  6. shift /1
  7. goto %1
  8. ) || (
  9. >&2 echo ERROR: routine %~1 not found
  10. )
  11. ) else >&2 echo ERROR: missing routine
  12. exit /b
  13. :PROCDIR
  14. if [%1] == [] call :ERROR_ARG %*
  15. setlocal DISABLEDELAYEDEXPANSION
  16. set SRCDIR=%~1
  17. set FILEMASKS=%~2
  18. if not defined FILEMASKS set FILEMASKS="\.*"
  19. set PROC=%~3
  20. set TOTAL=0
  21. set COUNTER=0
  22. for /f "usebackq tokens=* delims=" %%I in ( `dir /b /s /a:-d "%SRCDIR%\*" 2^>NUL ^| findstr /i "%FILEMASKS%" 2^>NUL` ) do (
  23. set /a TOTAL+=1 >nul
  24. )
  25. for /f "usebackq tokens=* delims=" %%I in ( `dir /b /s /a:-d "%SRCDIR%\*" 2^>NUL ^| findstr /i "%FILEMASKS%" 2^>NUL` ) do (
  26. %PROC%
  27. set PREVFILENAME=%%I
  28. set /a COUNTER+=1
  29. SETLOCAL ENABLEDELAYEDEXPANSION
  30. set /a PTS= 100*!COUNTER!/!TOTAL!
  31. if !ERRORLEVEL! NEQ 0 (
  32. @echo.Process failed. Error:!ERRORLEVEL!
  33. ) else (
  34. @echo.!COUNTER!^/!TOTAL! !PTS!^%% done.
  35. @echo.
  36. )
  37. endlocal
  38. )
  39. endlocal
  40. exit /b !ERRORLEVEL!
  41. :EXTRACT_FILE
  42. if [%1] == [] call :ERROR_ARG %*
  43. setlocal
  44. set extract_cmd="%ProgramFiles%\WinRAR\winrar.exe"
  45. set "rar_keys=-r -y -o- -ad -IBCK -INUL -x*\Thumbs.db"
  46. set SRCDIR=%~dp1
  47. set FILENAME=%~1
  48. set DEST=%~2
  49. set WORKPATH=%~3
  50. call set DESTDIR=%%DEST%%%%SRCDIR:%WORKPATH%=%%
  51. if not exist "%DESTDIR%" ( md "%DESTDIR%" || exit /b )
  52. echo.Extracting file "%FILENAME%"
  53. %extract_cmd% x %rar_keys% -- "%FILENAME%" "%DESTDIR%" 2>NUL
  54. if errorlevel 1 ( @echo.ERROR! Process %extract_cmd% failed. Code: %errorlevel% )
  55. endlocal
  56. exit /b !ERRORLEVEL!
  57. :COMPRESS_DIR
  58. if [%1] == [] call :ERROR_ARG %*
  59. if not defined PSRCDIR set "PSRCDIR="
  60. setlocal
  61. set compress_cmd="%ProgramFiles%\WinRAR\winrar.exe"
  62. set "rar_keys=-r -y -s -scul -ppassword -ep1 -m5 -ms*.bpg;*.jpg;*.png;*.rar;*.zip;*.7z -IBCK -INUL -x*\Thumbs.db"
  63. set SRCDIR=%~dp1
  64. set FILENAME="%~1"
  65. set DEST=%~2
  66. set WORKPATH=%~3
  67. call set DESTDIR=%%DEST%%%%SRCDIR:%WORKPATH%=%%
  68. set OUTPUTFILENAME=%DESTDIR:~0,-1%.rar
  69. call :GETFILEINFO dp "%DESTDIR:~0,-1%.rar" DESTDIR
  70. if not exist "%DESTDIR%" ( md "%DESTDIR%" || exit /b )
  71. if /i "%SRCDIR%" neq "%PSRCDIR%" echo.Compressing directory "%SRCDIR%" && %compress_cmd% a %rar_keys% -- "%OUTPUTFILENAME%" "%SRCDIR%" 2>NUL
  72. if errorlevel 1 ( @echo.ERROR! Process %compress_cmd% failed. Code: %errorlevel% )
  73. endlocal
  74. set PSRCDIR=%~dp1
  75. exit /b !ERRORLEVEL!
  76. :ENCODE_FILE
  77. if [%1] == [] call :ERROR_ARG %*
  78. setlocal
  79. set size=0
  80. set encode_cmd="d:\Tools\bpg-0.9.6-win64\bpgenc.exe"
  81. set "encode_keys=-q 32 -limitedrange -b 8 -m 8 -e x265 -o"
  82. set reencoder="c:\Program Files\GraphicsMagick-1.3.33-Q16\gm.exe"
  83. set FILENAME="%~1"
  84. set OUTPUTFILENAME="%~2"
  85. set TEMPFILENAME="%~3"
  86. set TEMPFILENAME1="%~dp3_.png"
  87. echo.Encoding file %FILENAME%
  88. ::%reencoder% -alpha off -strip -mattecolor white -background white -layers Dispose -format png -quality 98 %FILENAME% %TEMPFILENAME1%
  89. %reencoder% convert -define webp:method=6,target-psnr=92,pass=2,thread-level=2,image-hint=photo,filter-type=0,filter-strength=50,preprocessing=2 %FILENAME% %OUTPUTFILENAME%
  90. if errorlevel 1 ( @echo.ERROR! Process %reencoder% failed. Code: %errorlevel% )
  91. rem copy %FILENAME% %TEMPFILENAME1%
  92. rem %encode_cmd% %TEMPFILENAME1% %encode_keys% %TEMPFILENAME%
  93. rem call :GETFILEINFO z %TEMPFILENAME% size
  94. ::if errorlevel 1 (
  95. :: @echo.ERROR! Process %encode_cmd% failed. Code: %errorlevel%
  96. ::) else (
  97. :: if %size% LSS 50 (
  98. :: echo.Ouput size: %size%. Encoding failed!
  99. :: echo.%FILENAME%>>%TMPPATH%\Failed.txt
  100. :: ) else (
  101. :: echo.Ready. Ouput size: %size%. %OUTPUTFILENAME%
  102. :: copy %TEMPFILENAME% %OUTPUTFILENAME%
  103. :: del /q /s %FILENAME%
  104. :: )
  105. ::)
  106. endlocal
  107. exit /b !ERRORLEVEL!
  108. :GETFILEINFO
  109. if [%1] == [] call :ERROR_ARG %*
  110. setlocal
  111. set sw=%~1
  112. call set info=%%~%sw%2
  113. ::echo.info="%info%"
  114. ENDLOCAL & IF "%~3" NEQ "" SET %3=%info%
  115. exit /b !ERRORLEVEL!
  116. :ESCAPE_SCHARS_STR
  117. if [%1] == [] call :ERROR_ARG %*
  118. setlocal
  119. set schars="^& ^< ^> ^| ^` ^' ^. ^, ^; ^= ^( ^) ^^! [ ]"
  120. set str=%~1
  121. for %%f in (%schars%) do (
  122. set str="!str:%%f=^%%f!"
  123. ENDLOCAL & IF "%~2" NEQ "" SET %2=%str%
  124. exit /b !ERRORLEVEL!
  125. :FN_TO_FFCONCAT
  126. if [%1] == [] call :ERROR_ARG %*
  127. setlocal ENABLEDELAYEDEXPANSION
  128. set FILENAME=%~1
  129. set PREVFILENAME="%~2"
  130. set FFCONCATFILENAME="%~3"
  131. set /a nh=0 & set /a nm=0
  132. set /a ph=23 & set /a pm=50
  133. for /f "delims=_, tokens=3,4" %%I in ( "%PREVFILENAME%" ) do (
  134. set p=%%I
  135. set ph=!p:~0,2!
  136. set pm=!p:~2,4!
  137. set /a ph=100!ph! %% 100
  138. set /a pm=100!pm! %% 100
  139. )
  140. for /f "delims=_, tokens=3,4" %%I in ( "%FILENAME%" ) do (
  141. set n=%%I
  142. set nh=!n:~0,2!
  143. set nm=!n:~2,4!
  144. set /a nh=100!nh! %% 100
  145. set /a nm=100!nm! %% 100
  146. )
  147. set /a DELAY=((nh-ph)*60+(nm-pm))
  148. echo !DELAY! !nh! !ph! !nm! !pm! %PREVFILENAME% %FFCONCATFILENAME%
  149. if !DELAY!==-1430 set /a DELAY=10
  150. echo file !FILENAME:\=\\!>>%FFCONCATFILENAME%
  151. echo duration !DELAY:~0,-1!>>%FFCONCATFILENAME%
  152. ENDLOCAL
  153. exit /b !ERRORLEVEL!
  154. :NEWDATENAME
  155. @echo.%~1-%date:~6,4%-%date:~3,2%-%date:~0,2%_%time:~0,2%-%time:~3,2%-%time:~6,2%.%time:~9,2%~2
  156. exit /b !ERRORLEVEL!
  157. :DAYNUM
  158. setlocal
  159. for /F "tokens=1-3 delims=." %%a in ( "%~1" ) do (
  160. set /A "DD=1%%a-100, MM=1%%b-100, Ymod4=%%c%%4 "
  161. )
  162. for /F "tokens=%MM%" %%m in ("0 31 59 90 120 151 181 212 243 273 304 334") do (
  163. set /A DN=DD+%%m
  164. )
  165. if %Ymod4% equ 0 if %MM% gtr 2 set /A DN+=1
  166. set SDN=00%DN%
  167. @echo.%SDN:~-3%
  168. ENDLOCAL
  169. exit /b !ERRORLEVEL!
  170. :UNQ
  171. if [%1] == [] call :ERROR_ARG %*
  172. IF "%~2" NEQ "" set %1=%~2
  173. exit /b !ERRORLEVEL!
  174. :ERROR_ARG
  175. echo ERROR: Argument expected. Received: %* >&2
  176. exit /b 1
  177. :DIR_ERROR
  178. echo ERROR: Invalid source directory! %~1 >&2
  179. exit /b 1
  180. :ERROR_SELF
  181. echo ERROR: Source and destination are the same! %~1 >&2
  182. exit /b 1