Home > Blockchain >  How can I use CMD to create a subfolder in a folder and move every 10 files created there?
How can I use CMD to create a subfolder in a folder and move every 10 files created there?

Time:05-22

There is a cmd code (see code below). You can't do it in Powershell for I need it in cmd. This code with ffmpeg software slices the video into pieces. The point is, before ffmpeg works I create command MD folder result_%%i and put there for each processed file ALL results (video files) of ffmpeg processing: result_%%i/%%id.mp4

Question: How to create a subfolder in the folder for each of 10 output slices and put them there? Respectively so, there will be several subfolders in the result_%%i folder.

The subfolder should be numbered as result_1_%%i , for example. Thus, the full path to any file will look like this: result_%%i/result_1_%%i/%%id.mp4

@echo off
for %%i in (*.mov) do (
  MD result_%%i
  ffmpeg -i "%%i" -filter_complex "[0:v]boxblur=40,scale=1080x1920,setsar=1[bg];[0:v]scale=1080:1920:force_original_aspect_ratio=decrease[fg];[bg][fg]overlay=y=(H-h)/2" -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72 -c:a aac -preset slow -crf 23 -r 30 -sn -force_key_frames "expr: gte(t, n_forced * 0.5)" -f segment -segment_time 14.5 -reset_timestamps 1 result_%%i/%%i%d.mp4

)

pause

CodePudding user response:

Here is a batch file for this task.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
(for %%i in (*.mov) do (
  md "result_%%~ni"
  ffmpeg.exe -i "%%i" -filter_complex "[0:v]boxblur=40,scale=1080x1920,setsar=1[bg];[0:v]scale=1080:1920:force_original_aspect_ratio=decrease[fg];[bg][fg]overlay=y=(H-h)/2" -c:v libx264 -profile:v main -level:v 3.0 -x264-params scenecut=0:open_gop=0:min-keyint=72:keyint=72 -c:a aac -preset slow -crf 23 -r 30 -sn -force_key_frames "expr: gte(t, n_forced * 0.5)" -f segment -segment_time 14.5 -reset_timestamps 1 "result_%%~ni\%%~ni%d.mp4"
  if not errorlevel 1 set "FolderName=result_%%~ni" & call :MoveFiles
)) & exit /B
:MoveFiles
set "FolderCount=0"
:FolderLoop
set /A FolderCount =1
set "SlicesFolder=%FolderName%\Slices_%FolderCount%"
md "%SlicesFolder%"
set "FileCount=0"
for %%j in ("%FolderName%\*.mp4") do (
    setlocal EnableDelayedExpansion
    if !FileCount! == 10 endlocal & goto FolderLoop
    endlocal
    move "%%j" "%SlicesFolder%\" >nul
    set /A FileCount =1
)
goto :EOF

Let us assume the current directory on starting the batch file contains the movie file Development & Test(!) 100%.mov.

The batch file creates the subdirectory result_Development & Test(!) 100% in the current directory.

Then the batch file runs ffmpeg.exe which creates in this directory the files:

Development & Test(!) 100%_001.mp4
Development & Test(!) 100%_002.mp4
Development & Test(!) 100%_003.mp4
Development & Test(!) 100%_004.mp4
Development & Test(!) 100%_005.mp4
Development & Test(!) 100%_006.mp4
Development & Test(!) 100%_007.mp4
Development & Test(!) 100%_008.mp4
Development & Test(!) 100%_009.mp4
Development & Test(!) 100%_010.mp4
Development & Test(!) 100%_011.mp4
Development & Test(!) 100%_012.mp4

The subroutine MoveFiles creates in result_Development & Test(!) 100% two more subdirectories and moves the files into them. So the final directory structure is:

  • result_Development & Test(!) 100%
    • Slices_1
      • Development & Test(!) 100%_001.mp4
      • Development & Test(!) 100%_002.mp4
      • Development & Test(!) 100%_003.mp4
      • Development & Test(!) 100%_004.mp4
      • Development & Test(!) 100%_005.mp4
      • Development & Test(!) 100%_006.mp4
      • Development & Test(!) 100%_007.mp4
      • Development & Test(!) 100%_008.mp4
      • Development & Test(!) 100%_009.mp4
      • Development & Test(!) 100%_010.mp4
    • Slices_2
      • Development & Test(!) 100%_011.mp4
      • Development & Test(!) 100%_012.mp4

The command exit /B can be replaced by goto SliceDone with the label :SliceDone below the posted code and more command lines with endlocal as last command line.

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 /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • set /?
  • setlocal /?

See also:

  • Related