so I'm writing a script to scan mkv files and downconvert any 1080p files to 720p. I'm currently running through each file using a "for /r" loop to include subfolders. Is there a way I can recreate the subfolders into the output destination?
Here's what I have so far (p.s. It's my first script):
@echo off
set mediainfo="%~dp0MediaInfo.exe"
set result="%~dp0width.txt"
set handbrake="%~dp0HandBrakeCLI.exe"
SETLOCAL ENABLEDELAYEDEXPANSION
Set /P source=Enter source path:
Set /P output=Enter destination for converted files:
if %output:~-1%==\ set output=%output:~0,-1%
if not exist %output% mkdir %output%
for /R %source% %%j in (*.mkv) do (
Echo Filepath is: %%j
%mediainfo% --Inform=Video;%%Width%% "%%j" > %result%
set /P width=<%result%
del %result%
echo Width is: !width!
if !width! == 1920 (%handbrake% -i "%%j" -o "%output%\%%~xnj" -f mkv -e x264 -q 20 --vfr -a 1,1 -E av_aac,ac3 -B 192 -w 1280 --keep-display-aspect -x level=4.1:vbv-bufsize=78125:vbv-maxrate=62500) else (echo No need to transcode.)
)
Now if I have a structure like:
TV Show\Season 1
\Season 2
\Season 3
I would like the source dir to be "TV Show" and the output to keep the files in the same folder structure. Would this be doable?
EDIT: I updated the code a bit, added an if stated to compare %%~nxq (Season X) minus the last 2 characters to the word "Season" to determine if the working folder was inside a season subfolder. Only problem is if it's "Season 10" then the if comparison will read "Season ". Haven't tested it out and I'm not sure if there is a way to use wildcards in an if statement. Here's the updated code:
@echo off
set mediainfo="%~dp0MediaInfo.exe"
set result="%~dp0width.txt"
set handbrake="%~dp0HandBrakeCLI.exe"
SETLOCAL ENABLEDELAYEDEXPANSION
Set /P source=Enter source path:
echo Source path is: %source%
Set /P a=Enter destination for converted files:
set output=%a:"=%
if not %output:~-1%==\ (set output="%output%\") else (set output="%output%")
echo Destinition path is: %output%
if not exist %output% mkdir %output%
set dest=%output%
for /R %source% %%j in (*.mkv) do (
Echo Filepath is: %%j
for %%q in ("%%~dpj.") do (
set sea=%%~nxq
if "!sea:~0,-2!"=="Season" (
set dest="%output:"=%%%~nxq\%%~xnj"
set seafold="%output:"=%%%~nxq"
if not exist !seafold! mkdir !seafold!
) else (
set dest="%output:"=%%%~xnj"
)
echo Output Path is: !dest!
)
%mediainfo% --Inform=Video;%%Width%% "%%j" > %result%
set /P width=<%result%
del %result%
echo Width is: !width!
if !width! == 1920 (%handbrake% -i "%%j" -o !dest! -f mkv -e x264 -q 20 --vfr -a 1,1 -E av_aac,ac3 -B 192 -w 1280 --keep-display-aspect -x level=4.1:vbv-bufsize=78125:vbv-maxrate=62500) else (echo No need to transcode.)
)