I'm trying to get a string from an url using a batch file.
String example:
e-e --ser u.g --p 3 --f 0 x,ss
I am using the command below to CURL output directly to a variable:
FOR /F %%I IN ('curl.exe -s -S %URL%') DO (SET W=%%I)
The problem is, when I echo the variable [W] after the command runs, most of the string is missing...
e-e
What is the best method to get around this issue?
By default, the FOR /F command delimits the output based on a space and tab. That is stated in the help file. To keep that from happening use the DELIMS option to tell the FOR command to not use any delimiters.
FOR /F "delims=" %%I IN ('curl.exe -s -S %URL%') DO (SET W=%%I)
Related
help please.
I am on Windows command line and I want ffmpeg error returned as variable.
Tried many things, still not working.
#echo off&setlocal disabledelayedexpansion
SET S="path\to\ffmpeg.exe" -hide_banner -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg
!S!
for /f "usebackq delims=" %%A in ('!S!') do set var=%%A
echo !var!
The result is that the statement !S! produces the correct output on the console (InputFile.FLV: No such file or directory) but does not put that into the variable. The output of echo !var! statement simply shows as "path\to\ffmpeg.exe" -hide_banner -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg
How do I get the output of !S! into !var!?
The command setlocal disabledelayedexpansion explicitly disables delayed expansion and so !S! is not expanded delayed at all. For that reason the command line with just !S! results in an error message on execution of the batch file and definitely not in output InputFile.FLV: No such file or directory. And last line just outputs the string !var!.
The usage of for option usebackq results in interpreting the string between '...' as string and not as command line to execute with %ComSpec% /c in a background command process. For that reason the string !S! is assigned to variable var.
Therefore it can be expected that batch file was executed with setlocal EnableDelayedExpansion in real to get the outputs as written in question.
This not tested batch file should work for this task.
#echo off
set "var="
for /F delims^=^ eol^= %%I in ('""path\to\ffmpeg.exe" -hide_banner -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg 2>&1"') do set "var=%%I"
if defined var set var
Command FOR with option /F without using option usebackq and with a command line defined between the two ' executes in background %ComSpec% /c and the string between the two ' appended. So executed with Windows being installed in C:\Windows is:
C:\Windows\System32\cmd.exe /c ""path\to\ffmpeg.exe" -hide_banner -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg 2>&1"
The started Windows command processor instanced running in background removes in this case first and last " before executing the remaining command line:
"path\to\ffmpeg.exe" -hide_banner -i InputFile.FLV -vframes 1 -an -s 400x222 -ss 30 OutputFile.jpg 2>&1
ffmpeg.exe not installed by me at all outputs information like this one as far as I know to handle STDERR (standard error) instead of STDOUT (standard output). But FOR captures just output written to handle STDOUT of started command process. For that reason 2>&1 is needed to redirect output written to handle STDERR of background command process by ffmpeg.exe to handle STDOUT of background command process for being also captured by FOR of command process which is processing the batch file.
With FOR options argument string delims^=^ eol^= an empty list of string delimiters and no end of line character is defined to really get always the entire line as captured by FOR assigned to specified loop variable I. The two options are specified here by way of an exception not enclosed in " as otherwise it is not possible to define an empty list of delimiters and no end of line character. The two equal signs and the space must be escaped with caret character ^ to be interpreted as literal characters and not as argument string separators because of not being enclosed in a double quoted argument string.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /? ... explains how double quotes in string(s) after option /C or /K are interpreted by Windows command processor and when a file name (or any other argument string) must be enclosed in " on containing a space or one of these characters &()[]{}^=;!'+,`~<|>.
for /?
if /?
set /?
See also the Microsoft article about Using command redirection operators.
These are the questions I used to build these commands:
Assign output of a program to a variable using a MS batch file
The filename, directory name, or volume label syntax is incorrect inside batch
How to set commands output as a variable in a batch file
If I run the command, by creating a temp file, everything works fine:
"%CYGWIN_ROOT%bin\cygpath.exe" -u "%InstallImprovedSettings%" > motherfockingtemp.txt
set /p InstallImprovedSettingsUnix=<motherfockingtemp.txt
"%CYGWIN_ROOT%\bin\rm" -fv motherfockingtemp.txt
But, creating a file to assign a variable is too much overkill. If I try to do so, without creating temporary and silly files as follows:
FOR /F "tokens=* USEBACKQ" %%g IN (`'%CYGWIN_ROOT%bin\cygpath.exe' -u "%InstallImprovedSettings%"`) do (
SET "InstallImprovedSettingsUnix=%%F"
)
Batch gives the error:
FOR /F "tokens=* USEBACKQ" %g IN (`'C:\CygwinPortable\Cyg win\bin\cygpath.exe' -u "C:\CygwinPortable\Cyg win\cygwin-install-improved-settings.sh"`) do (SET "InstallImprovedSettingsUnix=%F" )
The filename, directory name, or volume label syntax is incorrect.
And if I replace single quotes with double quotes:
FOR /F "tokens=* USEBACKQ" %g IN (`"C:\CygwinPortable\Cyg win\bin\cygpath.exe" -u "C:\CygwinPortable\Cyg win\\cygwin-install-improved-settings.sh"`) do (SET "InstallImprovedSettingsUnix=%F" )
'C:\CygwinPortable\Cyg' is not recognized as an internal or external command,
operable program or batch file.
How can I use commands with spaces on their name?
You cannot use single-quotes to just enclose the command itself; either use single-quotes to enclose the whole command line including all the arguments, or, when using the usebackq option, use back-ticks instead.
The last attempt of yours complies with that (here once again but with the correct for meta-variable used in the loop body):
for /F "tokens=*" %%g in ('"%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%"') do (set "InstallImprovedSettingsUnix=%%g")
But this still fails, because for /F executes the cygpath.exe command line in a new Command Prompt instance by cmd /c, which strips the first and last quotation marks and leaves the following invalid command line behind:
%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%
To prevent this behaviour, just place an additional pair of quotation marks; it is a good idea to escape them in order not to have to change any other potential escaping:
for /F "tokens=*" %%g in ('^""%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%"^"') do (set "InstallImprovedSettingsUnix=%%g")
I have read several posts here on Stackoverflow about binding a variable during a FOR loop. While I figure most of the help provided here has been for Linux/Unix, I'm reaching out for help with batch scripting in Windows. My main goal is to extract the "date created" from a mp4-file and "overlay the date on my video" using ffmpeg (and or ffprobe).
I have experimented a lot, but my latest attempt has been trying to bind the result from ffprobe onto a variable, and use the variable later. My latest and simplest attempt looks like this:
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in ("*.mp4") do (
for /F "tokens=*" %%G in ('ffprobe -v quiet %%a -print_format compact -show_entries format_tags=creation_time') do (
set DateC=%%G
echo !DateC!)
)
I was hoping to be able to print the tag result from ffprobe using that code, but apparently not. So helping me bind that variable, and how to call it again later inside the following code snippet in Windows, would be deeply appreciated:
ffmpeg -i %%a -filter_complex "drawtext=fontfile=/Windows/Fonts/Arial.ttf:x=28:y=650:fontsize=45:fontcolor=white:box=1:boxcolor=black#0.4:text='!DateC!'" -c:a copy output.mp4
I must also mention I've seen the following code on StackOverflow:
ffmpeg -i %%a -filter_complex "drawtext=fontfile=/Windows/Fonts/Arial.ttf:x=28:y=650:fontsize=45:fontcolor=white:box=1:boxcolor=black#0.4:text='%{metadata\:creation_time}'" -c:a copy output.mp4
But I have the same problem making Windows recognize and print the metadata.
I am certain the file in question contains this metadata.
I suggest this not tested code for the batch file:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for %%a in ("*.mp4") do (
for /F "delims=" %%G in ('ffprobe.exe -v quiet "%%a" -print_format compact -show_entries format_tags^=creation_time 2^>^&1') do (
set "DateC=%%G"
echo !DateC!
)
)
endlocal
The inner FOR runs in a separate command process started with cmd /C the command line:
ffprobe.exe -v quiet "%%a" -print_format compact -show_entries format_tags=creation_time 2>&1
"%%a" is already replaced by name of current *.mp4 file. The double quotes are necessary in case of current *.mp4 file name contains a space or one of these characters &()[]{}^=;!'+,`~.
It is necessary to escape the equal sign with ^ in arguments list to get the command line correct passed to cmd.exe started by FOR in background.
2>&1 results in redirecting output written to handle STDERR to handle STDOUT because of FOR captures only everything written to STDOUT of the started command process.
The redirection operators > and & must be escaped with caret character ^ on FOR command line to be interpreted as literal characters when Windows command interpreter processes this command line before executing command FOR which executes the embedded ffprobe.exe command line in the separate command process started in background.
I don't have ffprobe.exe and ffmpeg.exe installed, but I think those console applications write information about files to handle STDERR (standard error) instead of STDOUT (standard output) which is the reason for using 2>&1 to get the wanted information captured by FOR and assigned to an environment variable.
"tokens=*" is the same as "delims=". Both result in getting the entire line captured by FOR assigned to loop variable G without splitting it up into substrings (tokens) using space/tab as delimiters, except the line starts with a semicolon in which case FOR would ignore that line completely for processing because of internal default eol=;.
I have a PowerShell script that outputs a single string value. I have a cmd batch script that needs to execute the PowerShell script and place that single PowerShell output value into a variable in the batch script. I'm finding all sorts of methods for exporting to a file or reading a file but that's not what I want. Thanks!
(edit) Here's where I'm trying to use it (in response to posting the script):
#echo off
REM The next line puts the .ps1 output into the variable
REM and, obviously, this does not work
set pass_word=<C:\temp\PullPassword.ps1
tabcmd login -s "http://myserver.net" -u mylogon -p %pass_word%
( edit )
I saw the answer by the OP here Getting Powershell variable value in batch script and also looked at foxdrive's answer so I started playing with the FOR...DO statement. I thought I was pretty good with cmd and couldn't figure out why what I had wasn't working:
for /f "delims=" %%a in ('powershell . "C:\temp\PullPassword.ps1"') do set val=%%a
echo %a%
When I was looking at foxdrive's full answer in the other post it struck me: %a% was wrong, I needed %val%! Oh, the shame! The below works:
#echo off
set mypath=C:\temp\PullPassword.ps1
for /f "delims=" %%a in ('powershell . "C:\temp\PullPassword.ps1"') do set pass_word=%%a
tabcmd login -s "http://myserver.net" -u mylogon -p %pass_word%
So I'll credit where it's due and mark foxdrive's answer correct, even though it was the other post that clarified my mistake.
This may help: it expects that the powershell script outputs the text to STDOUT which is the normal place for it to appear.
#echo off
for /f "delims=" %%a in (' powershell "script.ps1" ') do set "var=%%a"
Windows XP
My batch file runs a command that has several lines of output. How can I count (and store in a variable) the lines of output without ever writing to the disk?
dir | find /v /c "zzzxxx"
gives a line count
Here's sample script that will count the lines in the output of the dir command.
#echo off
setlocal enabledelayedexpansion
set lc=0
for /f "usebackq delims=_" %%i in (`dir`) do (
echo %%i
set /a lc=!lc! + 1
)
echo %lc%
endlocal
You can substitute dir with your command and you can use quotes and specify parameters. You will have to escape some other characters though - ^, | < > and &.
If you need to not only count the lines, but also parse each line, you might have to change the token delimiter from _ (as I used in the example) to something else that will not result in the line split in multiple tokens.
and while you are at it, you can also download GNU packages(coreutils) for windows and use the wc tool: eg
dir | wc -l