get the console output and print sametime in a batch file - windows

i want to print console output and get at the sametime.
i tried to this with below code to get and printing the result but it's not work.
how can i do this?
code:
set myvar="C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p"
for /f "delims=" %%i in ('%myvar%') DO (
echo %%i
set OUTPATH=%%i )
result:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

Your error message is arising from the FOR /F command.
Solution: A small change to the FOR /F statement
for /f "delims=" %%i in ('^"%myvar%^"') DO (
Explanation:
When executing a command via something like FOR /F .... IN ('command') DO ..., the command is executed in a new cmd.exe process via a command that looks like the following:
C:\Windows\system32\cmd.exe /c command
Your command requires quotes around the executable path because of the spaces. So your command, as currently written, becomes:
C:\Windows\system32\cmd.exe /c "C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p"
But CMD.EXE has an unfortunate design that if the first character of the command is a quote, then it normally strips the outer most quotes from the command string before it is executed. So the command becomes
C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p
Looking at the above, you should be able to see why you are getting the error message.
The solution is to add an extra set of quotes around the entire command that will get stripped. You want the command to look like:
C:\Windows\system32\cmd.exe /c ""C:\Program Files (x86)\Inno Setup 5\iscc.exe" asd.iss /SSign_PATH="%cd%\DigitalSign\signtool.exe sign $p""
But you don't want the extra set of quotes to throw off the batch parser, so they should be escaped within the IN() clause.

Related

Batch script - usage of FOR /F with spaces in command and usebackq

Pretty new to batch scripting. I need the output of a command to be assigned to a variable. I got to know about the FOR /F command. My command has spaces and also double quotes. So I tried using the usebackq option. But it fails with the error on the space.
for /F "usebackq delims=" %%G IN (`"C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"`) DO (
SET var=%%G
)
ECHO %var%
pause
The failure :
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
As per other questions on StackOverflow, I did everything right - usebackq for the usage of back tick, demlims= to ignore spaces as a delimiter. I am not sure why it is still failing. Any help is appreciated!
A command line within ' or within ` on using usebackq is executed by FOR with option /F respectively the Windows command process cmd.exe processing the batch file in background with %ComSpec% /c and the command line appended as additional arguments.
ComSpec is a Windows environment variable predefined with %SystemRoot%\System32\cmd.exe as system environment variable.
The help output on running cmd /? in a command prompt window explains how the strings are interpreted by Windows command processor after option /C (execute command line and close) or option /K (execute command line and keep running).
The usage of backquotes is not needed here as the command line to execute does not contain itself one or more arguments with '. So it is possible to enclose the command line in '.
It must be taken into account for the command line to execute by FOR, how the Windows command interpreter parses a batch script, i.e. how the FOR command line looks after parsing and processing it by cmd.exe processing the batch file which can be seen on debugging the batch file and how the command line is passed as argument string(s) to the second cmd.exe instance executed in background.
There are at least following two solutions.
for /F "delims=" %%G in ('^""%ProgramFiles(x86)%\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"^"') do set "var=%%G"
for /F "delims=" %%G in ('""C:\Program Files (x86^)\ABC\DEF\XYZ.exe" Get /a "P1^=D2" /b "Q1^=D3" /c "D5""') do set "var=%%G"
The first solution was suggested by Compo and is the easier one. The entire command line is enclosed in " which cmd.exe started in background removes before processing and executing the remaining command line. The additionally added " at beginning and at end of the command line are escaped with ^ for cmd.exe processing the batch file to be interpreted as literal character and not as beginning/end of an argument string. So the argument strings between the two escaped " are interpreted normally by cmd.exe processing the FOR command line.
The second solution is more difficult to understand. The entire command line is also enclosed in " for cmd.exe started in background. But the double quotes are not escaped for cmd.exe processing the batch file and for that reason this cmd.exe instance interprets the command line now as list of following argument strings:
""
C:\Program
Files
(x86
)
\ABC\DEF\XYZ.exe
" Get /a "
P1=D2
" /b "
Q1=D3
" /c "
D5
""
The closing round bracket after (x86 is not interpreted as literal character in this case because of being outside a double quoted argument string for cmd.exe processing the entire FOR command line. For that reason it must be escaped with ^ to be interpreted as literal character and not as end of set placed at wrong position before ' marking end of the command line to execute.
The two equal signs are both outside a double quoted argument string for cmd.exe processing the batch file. Therefore cmd.exe would interpret them as argument separator and would replace each = by a space character. That is not wanted here because of the arguments should be passed as P1=D2 and Q1=D3 to the executable and not as P1 D2 and Q1 D3. So both equal signs must be escaped with ^ to be interpreted as literal character and not as argument separator by cmd.exe processing the batch file.
Both solutions result in running in background with Windows installed into C:\Windows and 32-bit program files into C:\Program Files (x86):
C:\Windows\System32\cmd.exe /c ""C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5""
The background command process removes the first and last " and then runs
"C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"
It is possible to create in directory C:\Program Files (x86)\ABC\DEF a batch file XYZ.bat containing just the command line #echo %0 %* and replace .exe by .bat in the batch file containing the FOR command line to see if the batch file is executed at all and with which parameters passed to the batch file. This helps to understand what is going on with removing #echo off and running the batch file with the FOR command line from within a command prompt window.
The posted line fails because of the additional quotes around parameters after the program name. One workaround is to embed the entire command line inside another pair of outer quotes (and escape reserved characters like () to ^(^) because they now fall outside a quoted string).
for /F "usebackq delims=" %%G IN (`""C:\Program Files ^(x86^)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5""`) do (...)
As noted in a comment, usebackq is not actually needed in this case, and the same syntax will work if using regular quotes, instead.
There is no need to use backticks or usebackq here, simply use single quoted string and double quote your paths.
#echo off
for /F "delims=" %%G IN ('"C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"') do set "var=%%G"
Also note that the parenthesized do code block to set the variable is not needed, in fact, as far as I can see, you do not even need the variable as you could get away by just using the metavariable.
#echo off
set "prog=C:\Program Files (x86)\ABC\DEF\XYZ.exe"
for /F "delims=" %%G IN ('"%prog%" Get /a "P1=D2" /b "Q1=D3" /c "D5"') do echo %%G

BatchScript - catch command result (with whitspace)

I'm trying to safe the result of a where command in a batch-script
Without a script (directly in the command prompt), it looks like this:
where python
C:\Program Files\Anaconda3\short\python.exe
C:\Python\Python36\python.exe
but, when catching the result in a script-file:
#echo off
FOR /F %%p IN ('where python.exe') DO ECHO %%p
C:\Program
C:\Python\Python36\python.exe
the output of the first result ends with the whitespace.
Is there a way, to force the command, to assign a fullstring to the var, without any external tools?
FOR /F "delims=" %%p ....
See for /? from the prompt for documentation.

Run Autocad exe and load script file error

This is My first post in here
I need a help for your side I have made a batch file for run autocad exe and load a script file but give error when I run the batch file
#echo off
set KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools
set VALUE_NAME=installpath
set FN=loadload
set FE=scr
FOR /F "tokens=2*" %%A IN ('REG.exe query "%KEY_NAME%" /v "%VALUE_NAME%"') DO (set pInstallDir=%%B)
set approot=%pInstallDir:~0,-1%
echo %approot%\%FN%.%FE%
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %approot%\%FN%.%FE%
pause
Error: while running batch file autocad opens and in commandline the script file not loading "Xpresslisp.scr": Can't find file."
and bellow one is working
script file loading without getting error
#echo off
set path=%USERPROFILE%
set fol=Documents
set NAME=1
set SUFFIX=scr
"C:\Program Files (x86)\AutoCAD 2002\acad.exe" /b %path%\%fol%\%NAME%.%SUFFIX%
pause
Regarding your second question in the comments...
Bellow command will create the text file and write the first line to it e.g. "some text" like in the command below.
Echo some text > full_path_to_txt_file
Command below will append new text to same file.
Echo some text >> full_path_to_txt_file
'>' char creates file and writes firs line
'>>' char append text
check that %path%\%fol%\%NAME%.%SUFFIX% returns the Full Path to the "Xpresslisp.scr" file !
if it does, inspect the Full Path and see if it contains any white spaces.
if it does, enclose the %path%\%fol%\%NAME%.%SUFFIX% with apostrophes
"%path%\%fol%\%NAME%.%SUFFIX%"
It may be something as simple as blindly removing the last character of the installpath without knowing for sure what it is, (doublequote or backslash?).
As there is unlikely to be multiple copies of any filename in the Xpresslisp Tools tree, I would suggest something like this:
#Echo Off
Set "KEY_NAME=HKCU\Software\Laxman Enterprises\Xpresslisp Tools"
Set "VALUE_NAME=installpath"
Set "FN=loadload"
Set "FE=scr"
(Echo=FILEDIA 0
Echo=(LOAD "C:\\loadmyfile.lsp"^)
Echo=FILEDIA 1)>%FN%.%FE%
For /F "Tokens=2*" %%A In ('Reg Query "%KEY_NAME%" /v "%VALUE_NAME%"') Do (
For /F "Delims=" %%C In ('Dir/B/S/A-D "%%~B"\"%FN%.%FE%" 2^>Nul') Do (
Start "" "%ProgramFiles(x86)%\AutoCAD 2002\acad.exe" /b "%%~C"))
This doesn't care if there is a trailing backslash or not and will only run the AutoCAD command if the file is there.

Why does FOR loop command not work in a batch file which works on Windows command prompt?

FOR /L %A IN (1,1,100) DO echo %A
The code above in a batch script results in this error:
A was unexpected at this time.
The same command line works fine on Windows command prompt.
What is the problem?
You need to use double percent characters:
FOR /L %%A IN (1,1,100) DO echo %%A
If you run FOR /? you'll find that the first paragraph after the parameter list starts as follows:
To use the FOR command in a batch program, specify %%variable instead
of %variable.
As an example, let's start with a simple FOR loop:
FOR %x in (*) DO ECHO %x
This will run just fine from a command prompt, printing out the name of each file in the current directory, but if we use this verbatim in a Batch file, we'll get an error saying this:
x was unexpected at this time.
This is because Batch files have some extra abilities that use the percent sign immediately followed by some text, so when FOR is called from inside a Batch file it instead looks for two percent signs. So if we want to use that same FOR loop in a Batch script, we need to replace each instance of %x with %%x.
What we end up putting in our Batch file is this:
FOR %%x in (*) DO ECHO %%x
The problem is with %, %A is for use on command lines only.
when used in batch files %A should be substituted with %%A.

How to escape in windows command shell?

I've a file in c:\Program Files directory named tmp.txt
For each line in tmp.txt, I would like to execute a command.
I am trying to use command prompt for loop but it is not able to find the tmp.txt. Note that I've to run this command out of c:\Program Files directory.
Here is how I am trying:
C:\>for /F %i in ("c:\Program Files\tmp.txt") do echo "%i"
the output is:
C:\>echo "c:\Program"
"c:\Program"
which means that for is considering "c:\Program" as parameter and passing it to do
If I put file in c:\, and run for loop as-
C:\>for /F %i in (c:\tmp.txt) do echo "%i"
it works just fine
So my question is- how do I pass full path to for loop so that for consider it as file
Use the usebackq option to for /f:
for /f "usebackq" %i in (...)
This changes the semantics of the various quote characters, as the help states as well:
usebackq - specifies that the new semantics are in force,
where a back quoted string is executed as a
command and a single quoted string is a
literal string command and allows the use of
double quotes to quote file names in
file-set.

Resources