2

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.)
) 
Jason
  • 21
  • 2
  • What version of windows are you running on? Have you considered PowerShell? It's a more modern scripting language. What you ask is doable, you need a couple lines before you call handbreak to parse out the relative path from %j and, then the -o param changes to "%output%\%relpath%\%%~xnj". Parsing the relative path will be a pain, maybe use SUBST to assign drive letters to your source and output paths, then %%~pj gives the relative path without the drive letter. Good luck. – jimhark Jan 29 '16 at 22:26
  • The script will be running on a windows 7 machine. I considered powershell at first, don't remember why I didn't go with it. I'm not crazy about rewriting the script I just got working, but if all else fails I'll look into it. Thanks. – Jason Jan 29 '16 at 23:34
  • No way to use wildcards in `if` (and you can't substring on a metavariable like `%%q`) - Why not use `if "!sea:~0,7!"=="Season " (...` ? Do you still have a problem with this? If so - what's the problem with the updated code?? – Magoo Jan 30 '16 at 23:32
  • I wasn't entirely sure of the syntax, but using "!sea:~0,6!" and "Season" seems to work now. Thanks for your help. – Jason Jan 31 '16 at 19:27

1 Answers1

1

Certainly.

for %%q in ("%%~dpj.") do echo %%~nxq

should provide you with a clue, given that you've worked out your first batch so far...

(~dpj gives you the drive/path of %%j; then use a quirk to extract the name and extension of the last element of the directoryname)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • I'll give that a try. I was thinking trying something like comparing %%~pj with %source% and try to remove the common paths. Or something like set season=%%~pj:!source!=%% but I can't get that to work… – Jason Jan 30 '16 at 02:53