quotes.bat, Quotes in a Batch file

Off-topic posts of interest to the "Everything" community.
Post Reply
therube
Posts: 4610
Joined: Thu Sep 03, 2009 6:48 pm

quotes.bat, Quotes in a Batch file

Post by therube »

quotes.bat, Quotes in a Batch file (& I've actually got what were two different batch files quote in the code below).
(I'll just leave the whole thing in a CODE, & take from it what you will.)

QUOTES.BAT:

Code: Select all

:: quotes.bat
:: 02-28-2024 SjB

:: let me quote, don't you just hate quotes
:: let me quote, don't quote me on what i am about to quote

:: summary: to me, the most fullproof way to quote is with "%%~fI", quoted ;-)

:: sample file set:
:: 	"x x.wmv"
:: 	"xxx"
:: 	"xxx - ( Copy ).wmv"
:: 	"xxx.mp4"
:: 	"xxx.wmv"

:: run quotes.bat from command-line with above files
:: quotes.bat x*

:: run quotes.bat from command-line with a sampling of files, quoted or not
:: quotes.bat "x x.wmv"
:: quotes.bat "xxx.mp4"
:: quotes.bat xxx.mp4

:: drag - drop above 5 files onto quotes.bat


:: want more fun, fun, fun
:: remove the REM from 'goto ffmpeg:' and experiment with various file names, spaced or not, & with drag-drop, & from command-line (& quoted on not on command-line)
:: helps if you have real files to play with (small size, short length, small dimensions too [say 320x240], works)
:: ffplay will open a console window in addition to the video window, & the pause too, which helps giving you an idea as to what is going on
:: simply hitting ESC on the video window will close it & drop you to the next iteration - & it's fun - you can quote me on that


@echo off
:: goto ffmpeg:



for %%i in (%*) do (echo "%%~fi")
echo       --------- single quote on ALL file names - both command-line AND drag-drop,  full pathname
pause
for %%i in (%*) do (echo "%%i")
echo       --------- drag - drop: single quote on x.mp4, BUT, double quote on a filename with a -space-, which is apt mess things up
echo       --------- commandline: single quote on ALL file names,  filename only
echo                              - UNLESS you quote the filename on the command-line, in which case it is double quoted
pause
for %%i in (%*) do (echo %%~fi)
echo       --------- no quotes AT ALL               - both command-line AND drag-drop,  full pathname
pause
for %%i in (%*) do (echo %%i)
echo       --------- drag - drop:
echo       --------- quotes ONLY around -spaced- filenames, but not NOT-spaced names - "x (2).mp4" vs. 'x.mp4' (no quotes on the latter)
echo       --------- commandline: no quotes at all - UNLESS you quoted the filename on the command-line, in which case it is quoted
pause

goto end:



:: for %%i in (%*) do (echo "%%~fi"  &  ffmpeg -v 4  -i "%%i"  -map 0  -c copy  -f streamhash  -hash murmur3 -)  2>&1  |  tee -a  c:\out\logfile.txt




with d&d, %i is always "full pathname"
from command line, files in CWD, hashvideo x*.mp4, %%i~fi is full path, %%i is the file /name/ only (no path) [so i guess having path is fine ?]

from command line, with a filename inclosed in quotes:
"%%~fi" gives "xxx.wmv"
"%%i"   gives ""xxx.wmv"" - double quotes - in some cases
	- if a "full" filename is given
		"y:/xout/ou/x/y/xxx - (copy).mp4" -> ""y...4""
		or even      "./xxx - (copy).mp4" -> ""./xxx - (copy).mp4"" (rather then "./xxx - copy).mp4")
		actually, any quoted name; "xxx.mp4" -> ""xxx.mp4""
		similarly, ffplay will fail on a quoted file name that contains <spaces>
			so "xxx.mp4" is OK (with both "%%~fi~" & "%%i")
			   "x x.mp4" is OK  with      "%%~fi~" BUT, *FAILS* with "%%i"
			   

:ffmpeg
for %%i in (%*) do (ffplay -hide_banner "%%~fi")
pause
for %%i in (%*) do (ffplay -hide_banner "%%i")
pause
for %%i in (%*) do (ffplay -hide_banner  %%~fi)
pause
for %%i in (%*) do (ffplay -hide_banner  %%i)
echo.
echo heh
pause

goto end:



:: quotes2.bat
:: 02-29-2024 SjB

@echo off



echo "%*"
pause

:: the only thing this does it to echo back the command line params
:: so if you quotes2.bat xxx.wmv, you get xxx.wmv, quotes2.bat "xxx.wmv" -> "xxx.wmv", quotes2.bat x* -> x*
for %%i in (%*) do (echo "%%~fi")
pause

:: here 
for %%i in (%*) do (echo %%~fi  &  ffplay -hide_banner  "%%~fi"  &  pause)
pause
for %%i in (%*) do (echo %%~fi  &  ffmpeg -v 4  -i "%%~fi"  -map 0  -c copy  -f streamhash  -hash murmur3 -  & pause)


:: so... all forms of %~i *REMOVES* any quotes (passed), so in order to handle a filename with a <sp> in it,
:: the batch file must physically put QUOTES around %i, so "%i", such that things work - in all cases, hopefully ;-)



:: for %%i in (%*) do (echo %%~fi  &  ffmpeg -v 4  -i "%%i"   -map 0  -c copy  -f streamhash  -hash murmur3 -  & pause)
:: FAILS with "x x.wmv" - a command-line QUOTED name WITH <sp>
::                      - cause, it ends up being ""x x.wmv"" with 2 sets of quotes

:: for %%i in (%*) do (echo %%~fi  &  ffmpeg -v 4  -i "%%~i"  -map 0  -c copy  -f streamhash  -hash murmur3 -  & pause)
:: where... -i "%%~i" or -i "%%~fi" would first STRIP any (command-line) quotes,
:: then, the PHYSICAL " adds them back, so you end up with only 1 set of quotes, which is what you want


pause



:end
Post Reply