Assigning a command BAT file environment variable via a perl script - windows

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.

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.

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.

Set environment variable to function result

I'm a bit of a noob to batch programming. I've got this very small and simple code to do some stuff with Tuppers self-referential formula:
java -jar Tuppers.jar --read-image <img file you want to read>
I have the image file in a folder called Read. So I use this command line:
java -jar Tuppers.jar --read-image Read\*
since I will only have one file at a time in that folder.
I wanna set a variable num to the result of the function and then echo it out, but if I use
set /p num = java -jar Tuppers.jar --read-image Read\*
then its just gonna set the variable to the command line.
For /f "delims=" %A in ('java -jar Tuppers.jar --read-image Read\*') do echo %A
See for /? and use %%A in a batch file and %A when typing.
The standard output of any Windows command line program can be picked up in various ways.
Redirection to a file via > and >>
Piping to another command via |
And in your case picked up as a line of output. Try this from the command line:
for /f "tokens=*" %i in ('java -jar Tuppers.jar --read-image Read\*') do set myvar=%i
After that myvar, which you can check the contents of via "echo %myvar%", will contain the last line output from the java command.
Note that for this to work your java Tuppers.jar needs to print the number you want to standard output.

How to store the hostname in a variable in a .bat file?

I would like to convert this /bin/sh syntax into a widely compatible Windows batch script:
host=`hostname`
echo ${host}
How to do this so that it'll work on any Windows Vista, Windows XP, and Windows 2000 machine?
To clarify: I would then like to go on in the program and use the hostname as stored in the variable host. In other words, the larger goal of the program is not to simply echo the hostname.
hmm - something like this?
set host=%COMPUTERNAME%
echo %host%
EDIT: expanding on jitter's answer and using a technique in an answer to this question to set an environment variable with the result of running a command line app:
#echo off
hostname.exe > __t.tmp
set /p host=<__t.tmp
del __t.tmp
echo %host%
In either case, 'host' is created as an environment variable.
I usually read command output in to variables using the FOR command as it saves having to create temporary files. For example:
FOR /F "usebackq" %i IN (`hostname`) DO SET MYVAR=%i
Note, the above statement will work on the command line but not in a batch file. To use it in batch file escape the % in the FOR statement by putting them twice:
FOR /F "usebackq" %%i IN (`hostname`) DO SET MYVAR=%%i
ECHO %MYVAR%
There's a lot more you can do with FOR. For more details just type HELP FOR at command prompt.
I'm using the environment variable COMPUTERNAME:
copy "C:\Program Files\Windows Resource Kits\Tools\" %SYSTEMROOT%\system32
srvcheck \\%COMPUTERNAME% > c:\shares.txt
echo %COMPUTERNAME%
Why not so?:
set host=%COMPUTERNAME%
echo %host%
Just create a .bat file with the line
hostname
in it. That's it. Windows also supports the hostname command.
set host=%COMPUTERNAME%
echo %host%
This one enough. no need of extra loops of big coding.

Setting a variable from an executable

I am running an executable in a batch file with two parameters;
cmd /k ""executable" "param1" "param2""
This returns a string that I want to launch. I can't figure out how to set this return in a variable and subsequently launch it in IE.
Any ideas?
If the returned string contains a single line you may use FOR /F to set the value of an environment variable. For example:
s1.cmd
echo this is a one line string
s2.cmd
#SETLOCAL
#ECHO OFF
for /f "tokens=*" %%a in ('cmd /c s1.cmd') do set MY_VAR=%%a
echo got: %MY_VAR%
ENDLOCAL
Result
C:\> s2.cmd
got: this is a one line string
C:\>
You can use the following syntax to capture the output of your executable into a variable:
FOR /F "tokens=*" %%i in ('%~dp0YOUR_APP.exe') do SET TOOLOUTPUT=%%i
Source
then you can pass the value on to IE like so:
START "YOUR_WINDOW_NAME" /MAX /D"C:\Program Files\Internet Explorer\" iexplore %TOOLOUTPUT%
I take it that the application code that determines the url is too complicated to be reproduced in a batch file directly, or the source to the executable has been lost. If not I personally would prefer to have the logic visible in the batch file itself.
start %1 %2
Edit: Romulo A. Ceccon posted a much better solution which doesn't involve any file system access and dirty tricks. Left this here for reference (it works with command.com as well if you need 9x compatibility), but please prefer Romulo's solution.
Go through an environment variable you set by using an intermediate helper script you dynamically generate from a template. You will need write permissions somewhere, otherwise it cannot be done (the Windows command shell language is very, very limited.)
Let's call your helper script template helper.tpl with the following contents:
set INTERMEDVAR=
Make sure that helper.tpl has only a single line (no trailing CRLF!) and make sure you don't have any spaces after the equals sign there.
Now, in your main script, capture the output from your command into a temporary file (let's call it my_output_file.tmp):
cmd /k ""executable" "param1" "param2"" > my_output_file.tmp
Then copy the contents of the helper template and the output together into your helper script, let's call it my_helper_script.cmd:
copy /b helper.tpl + my_output_file.tmp my_helper_script.cmd
Then evaluate the helper script in the current context:
call my_helper_script.cmd
Now the INTERMEDVAR variable is set to the first line of the output from "executable" (if it outputs more than one line, you're on your own...) You can now invoke IE:
start iexplore.exe "%INTERMEDVAR%"
And don't forget to clean up the created files:
del /q /f my_output_file.tmp my_helper_script.cmd
This will obviously not work when invoked multiple times in parallel - you'll have to parametrize the temporary file and helper script names using the current cmd.exe's PID (for example) so that they won't overwrite each other's output, but the principle is the same.
However, if you can get a real shell, use that. cmd.exe is extremely cumbersome.

Resources