Using a .bat to extract text between two words - batch-file

I have a media server and I'm attempting to automate ripping my collection of movies and any future movies using MakeMKV. Ripping and moving is working without a hitch. The problem I'm running into is occasionally MakeMKV doesn't assign a title to the MKVs and I end up with title00.mkv which Media Center Master obviously cannot even begin to try to match to any metadata.
MakeMKV does offer the ability to get the information from the disc which I have print to info.txt which looks like this.
MSG:1005,0,1,"MakeMKV v1.8.10 win(x64-release) started","%1 started","MakeMKV v1.8.10 win(x64-release)"
DRV:0,2,999,1,"HD-DVD-ROM HL-DT-ST BD-RE GGW-H20L YL05","FARFROMHOME_16X9","\\Device\\CdRom0"
DRV:1,256,999,0,"","",""
DRV:2,256,999,0,"","",""
DRV:3,256,999,0,"","",""
DRV:4,256,999,0,"","",""
DRV:5,256,999,0,"","",""
DRV:6,256,999,0,"","",""
DRV:7,256,999,0,"","",""
DRV:8,256,999,0,"","",""
DRV:9,256,999,0,"","",""
DRV:10,256,999,0,"","",""
DRV:11,256,999,0,"","",""
DRV:12,256,999,0,"","",""
DRV:13,256,999,0,"","",""
DRV:14,256,999,0,"","",""
DRV:15,256,999,0,"","",""
FARFROMHOME_16X9 is the label for the disc.
How can I extract this and rename my .mkv when makemkv has finished?
Here is my BAT so far (my first attempt at a .bat):
makemkvcon64 -r info disc > info.txt
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
move C:\Users\HTPC\MakeMKV_Temp\*.mkv C:\Users\HTPC\Movies\
Renaming the file before the move would be ideal.

Although you posted an ample description of your problem and data, you have not explained what exactly you want as result, so I must guess a couple points. The lines below extract the sixth comma-separated token from the second line of info.txt file:
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
echo The label of the disk is: %discLabel%
This is the output of previous lines when they are executed on your example data:
The label of the disk is: FARFROMHOME_16X9
You also have not indicated the name of the file you want to rename (before move it). Below is a possible solution to your problem that should be adjusted when previous unclear points be defined:
makemkvcon64 -r info disc > info.txt
for /F "skip=1 tokens=6 delims=," %%a in (info.txt) do set "discLabel=%%~a" & goto continue
:continue
makemkvcon64 --minlength=3600 mkv disc:0 all C:\Users\HTPC1\MakeMKV_Temp\
START /WAIT makemkvcon64.exe
cd "c:\Program Files (x86)\FreeEject\"
FreeEject d:
ren C:\Users\HTPC1\MakeMKV_Temp\TheNameHere.mkv %discLabel%.mkv
move C:\Users\HTPC1\MakeMKV_Temp\*.mkv C:\Users\HTPC1\Movies\

I've been wrangling with the same thing and created something that works fairly well.
MakeMKV uses it's own output FileName like you indicated "title00.mkv" or something similar.
I've created two(2) batch files to perform my home movie automation.
The first is "RipWorkflow_DVD.bat", which handles all the heavy lifting to call MakeMKV first to rip the content to my drive. Also in this file is a call to Handbrake to convert the MKV to MP4 (my stupid "smart" tv won't play MKV, but likes MP4)
The second batch file "OneStepDVDRip.bat" calls the first, but with parameters I choose for
a) location of the MKV
b) the output name of my MP4
c) the minLength of a track to locate and rip to MKV
Here is the code for "OneStepDVDRip.bat"
call "C:\Users\Todd\Desktop\RipWorkflow_DVD.bat" "Z:\Video\TrueStory" "E:\My Videos\Movies\Drama\True Story (2015).mp4" 3600
Here is the code for "RipWorkflow_DVD.bat"
#echo off
cls
setlocal ENABLEDELAYEDEXPANSION
SET input=%1
SET output=%2
SET minlength=%3
echo
echo %input%
echo %output%
echo
if not exist %input% ( mkdir "%input%" )
call "C:\Program Files (x86)\MakeMKV\makemkvcon64.exe" --minlength=%minlength% --decrypt mkv disc:0 0 "%input%
timeout /t 5 /nobreak
for %%F in (%input%\*.mkv) do (
echo %%~dpnxF
echo %output%
call "C:\Program Files\Handbrake\HandbrakeCLI.exe" -v1 -i %%~dpnxF --main-feature -o %output% -f mp4 --markers -e x264 -b 2000 -a 1 -E faac -6 dpl2 -R 44.1 -B 128 -D 1.75 --subtitle-forced -x ref=2:bframes=2:subme=6:mixed-refs=0:weightb=0:8x8dct=0:trellis=0
timeout /t 5 /nobreak
DEL %%F /Q
)
endlocal

Related

downsizing multiple mpg files using ffmpeg.exe

I'm using ffmpeg.exe to process screen captures for a demo using MS Game Bar. Game Bar captures at a high frame rate at high resolution and the files are very large.
Using ffmpeg.exe I can process the files using:
ffmpeg.exe -i file.mpg file_l.mpg
And all is good but I'd like to create a bat file so I can do half a dozen or any in a folder all at once.
My Windows bat skills are poor and I've been trying to get it to work using something like this without success:
for %%f in (*.mp4) do (
ren %%~nf%%~xf !fileNum!%%~xf
set/a fileNum += 1
Can anyone help please
here's one I've used that can perhaps be adapted to meet your needs.
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename without extension
set OUTPUT=%%~nG
:: pass filename to FFmpeg output
ffmpeg -i %%G !OUTPUT!.wav
:: delete source file
del %%G
)
if your input format and output format are the same, you can do something like this:
SETLOCAL EnableDelayedExpansion
:: start loop
for %%G in (*.mp3) do (
:: get filename with extension
set MYNAME=%%~nxG
:: create temp name
ren !MYNAME! delete1.mp3
:: pass original filename to FFmpeg output
ffmpeg -i delete1.mp3 !MYNAME!
:: delete temp file
del delete1.mp3
)

Exiftool via batch file: redirecting output to original console

I'm trying to get the dimensions width/height from a webp image, using exiftool via a batch file and saving those values as variables. I can get the bat file to output the dimensions (in this example it outputs "400x222" in the exiftool window), however I'm not too sure how to redirect those values sucessfully back inside the bat file console window as variables.
Below code gives me what I want, but in the exiftool window:
#echo off
setlocal EnableDelayedExpansion
echo =============== WEBP EXIF ===============
echo.
for %%f in ("C:\ffmpegConvert\*.webp") do (
exiftool -k -s -s -s -ImageSize %%f
)
endlocal
pause
exit
Below is what I tried, but I'm not able to echo back those values in my initial bat file console window (I get "echo is off, once for each failed echo")
#echo off
setlocal EnableDelayedExpansion
echo =============== WEBP EXIF ===============
echo.
for %%f in ("C:\ffmpegConvert\*.webp") do (
for /f "tokens=1,2 delims=x" %%a in ('exiftool -s -s -s -ImageSize %%f') do (
set sW=%%a
set sH=%%b
)
#echo !sW! >CON:
#echo !sH! >CON:
)
endlocal
pause
exit
Ok, so after checking out all your feedback and doing some extra googling, I noticed that my exiftool.exe in c:\Windows had "run as admin" checked in its properties. Unticking this option now returns the variables in my original console...To be sure, I re-checked it, and it went back to not displaying the info properly. I don't understand why that is, but now it works as I wanted:) Thanks again for your responses!

Batch script to move to another section of the script if there is no input from user

I am trying to get this script to jump to another section of the script if there is no input from the user.
Down at the if %input%== area.
What I'm trying to do is skip to the section where the script checks for .mp4 files and moves them if they are there. Am I supposed to set a variable or loop for that section? Thanks for any replies
#echo off
echo Checking for youtube-dl updates.
pause
youtube-dl -U
rem Enter the url or urls that you want to download from
set /p input="Enter the url(s) you want to download:"
rem Uses the youtube-dl continue (-c) option to download multiple files if not in a playlist
youtube-dl -c "%input%"
rem pause
if %input%=="" GOTO:EOF
cls
echo Download complete, please wait while files are transfered to appropiate folder
pause
for %%o in (.mp4) do move "*%%o" "E:\Documents\scripts\videos\"
if not exist do echo .mp4 files are no longer in this directory
pause
How about following script? This script waits for 3 seconds while "file.mp4" doesn't exist. It keeps to wait until the file exists. About "filename", you can change for your script.
#echo off
set waittime=3
set filename=file.mp4
:loop
if not exist %filename% (
Timeout /t %waittime% /nobreak > nul
goto loop
)
echo Find %filename%
When doing string comparison in batch you have to make sure, that both parts are equal, which in your case will never happen! In most languages strings have double quotes around them. In batch they usually do not.
To solve your problem enclose %input% in double quotes as well.
Note that it can be useful to do something like "x%input%"=="x" to prevent certain characters like <>|to be at the beginning of the comparison string.
You can check this on your own with these few lines:
#echo off
set /p input="Input something or nothing here "
echo %input%
echo "%input%"
pause
If you are hitting Return without any input you will see that only the bottom one will output "" which is the string you are comparing to.

How to reduce the size of log file with batch script

I have two logs files that are thousands of lines long. I want to have a script that chops off everything except the last, say 51 lines (50 entries, last line is blank).
I have this code:
for %%x in (*.log) do (
for /f "skip=51 delims=*" %%l in ("%%x") do (
echo. > tmp/%%x
)
move /Y tmp/%%x %%x
)
but it keeps outputting this (twice, one for each file) and nothing else happens
for /F "skip=51 delims=*" %l in ("main.log") do (echo. 1>tmp/main.log )
move /Y tmp/main.log main.log
)
The system cannot find the path specified.
I'm even unsure about the syntax. Please help. Thanks.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\106x"
SET "filemask=*.saved"
FOR %%x IN ("%sourcedir%\%filemask%") DO (
sed -e :a -e "$q;N;52,$D;ba" "%%x" >"%temp%\x"
MOVE /y "%temp%\x" "%%x"
)
GOTO :EOF
Just a few little things.
First, directory separators are \ - / introduces switches.
Second, skip=51 will skip the first 51 lines - you want to save the last 51.
Native batch could do as you ask - but it would be very, very slow.
I've used SED in the above batch, which should be familiar to you, given the syntax you used. It's a 3rd-party utility in the DOS world - I used GNU SED - Google is your friend.
This uses a helper batch file called findrepl.bat - download from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
Place findrepl.bat in the same folder as the batch file or on the path.
It uses native Windows scripting and is robust and very quick on large files.
findrepl /o:-51 < "file.log" >"file.tmp" & move /y "file.tmp" "file.log"

Trying to use a batch file to work with filenames that have a forbidden character

Okay, a brief explanation of what I am doing: I use Windows Media Center (Windows 7) to record Jeopardy every evening. I then use Handbrake to convert the .wtv files to .mkv files and then transfer them to my NAS so I can watch them later using Plex Media Server/Center. Rather than doing this "by hand", I'm trying to automate the process using a batch file as a scheduled task. Initially, I had set up a script so that I could right-click > Send To > convert.bat and it would initiate the command-line interface for Handbrake and convert the file, move the output to my NAS, and delete the original file (worked great).
Now, what I'm doing is initiating the batch script as a scheduled task and looping through the contents of my "recorded tv" directory and looping through any .wtv files to convert/move/delete them.
The problem lies in the fact that Windows Media Center correctly names the Jeopardy files with the "!" in them (Eg: Jeopardy!_KHQ_2012_12_04_21_12_12.wtv), which completely bricks my script. The "Send To" batch file worked great, but when I loop through the *.wtv files in the directory, it returns all the filenames with the "!" stripped out which means I can't do squat with them. Files without "!" do process without a hitch.
Thanks in advance to anyone who can get me pointed in the right direction! (and if you happen to see any other areas where this script could be improved, that's fine too...)
Here is the basic code that I am attempting to use:
#echo off
setlocal ENABLEDELAYEDEXPANSION
SET count=0
SET getFolder=C:\Users\Public\Recorded TV\
SET ripFolder=C:\Rips\
SET putFolder=Z:\Videos\Recorded TV\
FOR %%F IN ("%getFolder%*.wtv") DO (
SET /A count=!count!+1
REM DETERMINE OUTPUT FILENAME
for /f "tokens=5,6,7,8,9,10 delims=\_" %%a in ("%%F") do (
set show=%%a
set station=%%b
set year=%%c
set month=%%d
set day=%%e
set hour=%%f
REM GENERATE OUTPUT NAMING CONVENTION
set output=!show! s!year!e!month!!day! !hour!
)
REM PROCESS WITH HANDBRAKE CLI
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%F" -t 1 -c 1 -o %ripFolder%!OUTPUT!.mkv -f mkv --deinterlace="fast" --crop 58:60:2:2 --strict-anamorphic -e x264 -q 20 --vfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0 --gain=0 --audio-copy-mask none --audio-fallback ffac3 -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1
REM MOVE CONVERTED FILE TO NAS
copy "%ripFolder%!OUTPUT!.mkv" "%putFolder%"
REM DELETE ORIGINAL
del "%%F"
REM DELETE LOCAL RIP
del "%ripFolder%!output!.mkv"
)
echo %count% files processed
pause
ENDLOCAL
As you have recognized, the exclamation mark is stripped before you can escape it.
That's because you expand the FOR-loop variable %%F while delayed expansion is enabled, and the exclamation mark tries to expand a variable.
You need to toggle the delayed expansion here, as the variable contents are safe when using with delayed expansion, but to get the value you need the disabled mode.
#echo off
setlocal DisableDelayedExpansion
SET count=0
FOR %%F IN ("%getFolder%*.wtv") DO (
set "orgFile=%%F"
SET /A count+=1
REM DETERMINE OUTPUT FILENAME
for /f "tokens=5,6,7,8,9,10 delims=\_" %%a in ("%%F") do (
set show=%%a
set station=%%b
set year=%%c
set month=%%d
set day=%%e
set hour=%%f
setlocal EnableDelayedExpansion
REM GENERATE OUTPUT NAMING CONVENTION
set output=!show! s!year!e!month!!day! !hour!
)
REM PROCESS WITH HANDBRAKE CLI
"C:\Program Files\Handbrake\HandBrakeCLI.exe" -i "%%F" -t 1 -c 1 -o %ripFolder%!OUTPUT!.mkv -f mkv --deinterlace="fast" --crop 58:60:2:2 --strict-anamorphic -e x264 -q 20 --vfr -a 1 -E faac -B 160 -6 dpl2 -R Auto -D 0 --gain=0 --audio-copy-mask none --audio-fallback ffac3 -x ref=1:weightp=1:subq=2:rc-lookahead=10:trellis=0:8x8dct=0 --verbose=1
REM MOVE CONVERTED FILE TO NAS
copy "%ripFolder%!OUTPUT!.mkv" "%putFolder%"
REM DELETE ORIGINAL
del "!orgFile!"
REM DELETE LOCAL RIP
del "%ripFolder%!output!.mkv"
endlocal
)

Resources