Batch Script Reading TXT File - windows

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.

Related

windows oneliner to set a command output in an environment variable

As stated here Is it possible to set an environment variable to the output of a command in cmd.exe I always used that
mycommand.exe>%TEMP%\out.txt
set /P FOO=<%TEMP%\out.txt
But that's ugly because it creates a file.
The for method is better but complicated
I wanted something simple a la unix, like:
mycommand.exe|set /P FOO=
No error, but FOO is not set after I run that.
Why is this not working?
Best way I can think of doing this would be to create your own little batch file that silently uses the FOR construct. For instance, you could create a batch named BatchSet.bat, stored somewhere on your path. The batch would contain the following code:
#Echo off
for /f "delims=" %%i in ('%2') do set %1=%%i
Set %1
If you run this with the following command:
BatchSet MyVar whoami
You'll get something like:
MyVar=servername\John
Obviously, the command you run should limit its output to a single line to be stored properly in the environment variable.
For instance, if you run it like this:
BatchSet MyVar Vol
Then you'll only get the first line of the Vol command's output
MyVar= Volume on drive C is labeled MyDisk
But all in all, it's a fairly elegant way of doing what you were looking for.
Note that the last line in the batch is simply there to provide visual output. It can be removed altogether.

cmd batch recall commands history

I have a batch script that takes an url as argument and before doing the main operations of the script, It saves the url in a log file.
Is there a way to read the log file (when I execute it next time) and add the entries to the history so it can be used (by UP KEY) like a recall history ?
it isn't that hard, you need to make a log.txt holding all the data.
Use this to add something in it:
echo [action]>>log.txt
And this to read it:
for /f "tokens=* delims=" %%a in (a.txt) do echo %%a
by the way, this is in #echo off
A very similar thing is possible using For loops, Line counts, an Array containing the stored Urls retrieved from the file, and the Choice command within a Loop to Page through the array.
Here is an example

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

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%

Assigning a command BAT file environment variable via a perl script

I have a batch file I'm running under cmd.exe window. I want to look at a web config file and get a value. I have no idea of the right way to do this, but I figure I can cheat by writing a perl script to do this - and returning the value to the batch file.
I'm looking for something that looks like:
set var1=(evaluate perl script)
How does one do such a thing?
#echo off
set var1=
echo var1=%var1%
for /f "usebackq delims=" %%q in (`perl -E"say 'foo'"`) do set var1=%%q
echo var1=%var1%
Use %q instead of %%q outside of a batch file.

Running command-line in batch file

I have the following code which doesn't seem to be working properly - is someone able to assist with how to run command-lines in batch files
#echo off
set changeFrom=321
set changeTo=123
set origFile=config.txt
set newFile=config1.txt
test.bat %changeFrom% %changeTo% %origFile%>%newFile%
del %origFile%
ren %newFile% %origFile%
::end
I have a file "test.bat" which has code to replace strings in a file - but I Don't get how it can work ?
You need to use call to execute the second bat file from the first like this:
call test.bat %changeFrom% %changeTo% %origFile%>%newFile%
without call the first batch script will exit when the second one exits.

Resources