Exiftool via batch file: redirecting output to original console - batch-file

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!

Related

Looping .bat file with 2 changed variables

Trying to automate some ScreamingFrog (SF) website crawls, which can be done via Command Line (CL). I've created a bat file that sets some variables, tells CL where the .exe is, and runs the relevant commands within SF:
set domain=http://example.com
set company=ExampleCom
set crawlResults=C:\Users\brand\Documents\ScreamingFrog\Crawls
set sf=C:\Program Files (x86)\Screaming Frog SEO Spider\
set configFile=C:\Users\brand\Documents\ScreamingFrog\setup\ConfigSchedule.seospiderconfig
chdir /d "%sf%"
ScreamingFrogSEOSpiderCli.exe --config "%configFile%" --crawl "%domain%" --save-crawl --task-name "%company%" --headless --output-folder "%crawlResults%" --google-drive-account "account#example.com" --export-format "gsheet" --bulk-export "Response Codes:Client Error (4XX) Inlinks,Response Codes:Server Error (5XX) Inlinks"
I could create a .bat file for every domain/company - however, I was wondering if I could instead loop the .bat and replace the %domain% and %company% with the next pair from a list?
Apologies for my lack of expertise, I really appreciate anyone who takes a peek.
(untested) The following should do fine:
#echo off
setlocal
set "crawlResults=C:\Users\brand\Documents\ScreamingFrog\Crawls"
set "sf=C:\Program Files (x86)\Screaming Frog SEO Spider\"
set "configFile=C:\Users\brand\Documents\ScreamingFrog\setup\ConfigSchedule.seospiderconfig"
chdir /d "%sf%"
for /f "skip=1 tokens=1,2 delims=;" %%a in (data.csv) do (
ECHO ScreamingFrogSEOSpiderCli.exe --config "%configFile%" --crawl "%%a" --save-crawl --task-name "%%b" --headless --output-folder "%crawlResults%" --google-drive-account "account#example.com" --export-format "gsheet" --bulk-export "Response Codes:Client Error (4XX) Inlinks,Response Codes:Server Error (5XX) Inlinks"
)
with contents of data.csv like:
Domain;Company
http://example.com;Example
http://guugle.com;Searcher
http://sellstuff.com;E-Shop
Remove the ECHO after verifying it does work as intended.

How to remove the new line being printed in the .dat file

I am doing an ftp batch script. It's an mget command but I have an error with the file name because it has ":"(colon)s in it. So my codes does a for loop which does the get command multiple times. My problem is, is that the .dat file has an empty line which is an error for the ftp command.
Below is my code:
#echo off& setlocal enableextensions
Echo open sample.net>second.dat
Echo user»second.dat
Echo pass»second.dat
Echo ascii»second.dat
Echo cd path/path/path»second.dat
setlocal enabledelayedexpansion
for /f "delims=" %%a in (file_list.txt) do (
set "remote=%%a"
echo get !remote:~0,-1! !remote::=.!»second.dat)
endlocal
Echo bye»second.dat
ftp -v -i -s:second.dat
del second.dat
pause
The file_list.txt is the list of file names that I need to rename using this code
setlocal enabledelayedexpansion
for /f "delims=" %%a in (file_list.txt) do (
set "remote=%%a"
echo get !remote:~0,-1! !remote::=.!»second.dat)
endlocal
The output file is this one
sample.net
user
pass
ascii
cd path/path/path
get logsample-txt-871749063-10-17-2017_22:00:05:692474.txt logsample-txt-871749063-10-17-2017_22.00.05.692474.txt
get logsample-txt-871749063-10-18-2017_08:00:57:671751.txt logsample-txt-871749063-10-18-2017_08.00.57.671751.txt
get logsample-txt-871749063-10-18-2017_08:00:07:724779.txt logsample-txt-871749063-10-18-2017_08.00.07.724779.txt
bye
What I what is, I need to remove the blank spaces for the ftp command to read it properly
sample.net
user
pass
ascii
cd path/path/path
get logsample-txt-871749063-10-17-2017_22:00:05:692474.txt logsample-txt-871749063-10-17-2017_22.00.05.692474.txt
get logsample-txt-871749063-10-18-2017_08:00:57:671751.txt logsample-txt-871749063-10-18-2017_08.00.57.671751.txt
get logsample-txt-871749063-10-18-2017_08:00:07:724779.txt logsample-txt-871749063-10-18-2017_08.00.07.724779.txt
bye

Batch File - Get filename from directory and save as variable

I am trying to read in a directory and get the filename from that directory.
I then want to save the filename as a variable and echo that filename out.
Here is the code I am using:
for /F %%a in ('dir C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\ *.csv') do set FileName=%%a
echo %FileName%
I am getting the following when command prompt runs:
"File not found
Directory"
Does anyone know how to resolve this or where I'm going wrong?
Thanks
Safer way of doing the same:
#echo off
setlocal
set "yourDir=C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\"
set "yourExt=*.csv"
pushd %yourDir%
for %%a in (%yourExt%) do echo %%a
popd
endlocal
Sets both: Your directory and the extension you are searching for, Changes the directory to the one previously setted possibly including a /drive change and then runs a loop over all files matching your extension and echo them out. To save only the last one you can use:
...do set fileName=%%a
echo %FileName%
Or to use them all within the loop you can use:
#echo off
Setlocal EnableDelayedExpansion
REM Other things done here
do (
REM Do stuff with %%a here
Set filename=%%a
echo !filename!
echo !filename:~0,6!
echo !filename:a=b!
)
If you just want to echo them, you can just go for echo %%a. If you want to do other things like string-substitution or substrings as described in the comments you need DelayedExpansion as shown above. There are a lot of questions on SO as well.
Note that you can get different "parts" of the path of your file. Have a look on this answer I always have a look on as well. Alternatively check the documentation for the for command typing for /? into the command-line.

Using a .bat to extract text between two words

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

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