How can i set exe file output to variable in batch file? - windows

I have execution file (print.exe) which will print some numbers.I want to use those numbers. For that I wrote a batch file.
build.bat
set a=print.exe
FOR %I IN a DO prompt.exe %I%
I used the above 2 lines. But its not working. If, it is kernel command, the first line is working. For exe, it didn't work. How can i store the print.exe file output to variable.?

The Batch file below execute print.exe and get its output in numbers variable:
#echo off
for /F "delims=" %%a in ('print.exe') do set numbers=%%a
echo Output of print.exe is: %numbers%

Related

Why is my variable not passing all the data to a text file in my batch script?

I'm trying to parse a file that contains data, it has some numbers which I want to parse, store and use them as variables in the future. The problem is, these numbers are updated often so that's why I'm parsing them whenever I run my script.
The problem, is that when I collect the data as a variable in a for loop, it's not getting passed to my text file properly.
#echo off
setlocal EnableDelayedExpansion
for /f "skip=104 eol=/ tokens=4 delims=," %%A in (file location) do (
echo %%A
echo %%A > UICNumbers.txt
)
This is the output of the echo %%A command:
UIC00000701991
UIC00000710996
UIC00001701890
UIC00002701890
UIC00001701898
UIC00002701898
When I open the UICNumbers.txt file all I see listed is:
UIC00002701898
which is the last entry in the for loop variable. I tried changing the output to >> but this means every time I run the command it repeats every entry.

Batch file renames files incorrectly

I am using an executable file (called reduce.exe) to use two functions on PDB files and rename them once done, and am trying to do this to an entire file directory. I have a text file named pdblist.txt that sits right next to the batch file. My current file looks like this:
setlocal enabledelayedexpansion
echo off
for /F %%g in (pdblist.txt) do (
set var=!%%g:~0,4!
echo !var!
set trim=!var!no_H.pdb
echo !trim!
set build=!var!h.pdb
echo !build!
reduce.exe -Trim %%g > !trim!
reduce.exe -BUILD !trim! > !build!
)
However, when I run the batch file, it creates two files, called "~0,4h.pdb" and "~0,4no_H.pdb". I am supposed to end up with three files for each original file name, as in this example:
Original file name is 1csl.pdb, after the Trim function, it creates 1csl_noH.pdb, and after the BUILD function, it creates 1cslh.pdb
So, could you please explain why it is not creating two more files? Are the exclamation points in the wrong places on the set var line, or any other line?
first set var to g, then truncate
for /F %%g in (pdblist.txt) do (
set var=%%g
set var2=!var:~0,4!
set trim=!var2!no_H.pdb
echo !trim!
set build=!var2!h.pdb
echo !build!
my pdblist.txt contains first_item, second_item (1 per line). output is:
first_item
firsno_H.pdb
firsh.pdb
second_item
secono_H.pdb
secoh.pdb

windows batch script loop through file

How do I write a batch script that loops through each line in a text file and stores the line in a variable in the loop?
For example I have:
#echo off
for /F "tokens=*" %%A in (C:\Users\pmandayam\git\devops\UserRegistrationServices\scripts\file.txt) do (
set var1=%%A
echo %var1%
)
For some reason, the output keeps printing echo is OFF. multiple times, instead of the actual line

Batch Script Reading TXT File

I am looking for a way for a batch script to be able to read a txt file.
Lets say the txt file has a number such as "723121312", and I only put that number in there.
Now from the batch script, I want to be able to store that number as a variable, %update_id%.
The batch script will run a program like so:
call gmpublish.exe update -addon %folder%\*.gma -id %update_id% -changes %update_changes%
The other two variables don't matter, as I already made those automatic.
It will store inside the batch script so the program would run like so:
call gmpublish.exe update -addon %folder%\*.gma -id %723121312% -changes %update_changes%
Thank you
You probably want something like:
FOR /F %%i IN (file.txt) DO call gmpublish.exe update -addon %folder%\*.gma -id %%i -changes %update_changes%
If it's only one line to be stored, it's as simple as:
REM writing to file:
>file.txt echo 1234567
REM reading from file:
<file.txt set /p "var="
echo %var%
if there are more lines, for /f (as in razor's answer) is the better way.

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.

Resources