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.
Related
I'm trying to create a simple application launcher for the software Nuke that puts the path together by evaluating an environment variable. The value of the variable is used in two ways. First it's used as-is. The second time I need to split the variable and use the first half.
The env variable set for the system:
NUKE_VERSION = 10.0v5
The path to the application:
C:\Program Files\Nuke10.0v5\Nuke10.0.exe
The code below works fine on the cmd prompt:
FOR /F "delims=v tokens=1" %i IN ("%NUKE_VERSION%") DO set NUKE_MAJOR=%i
"C:\Program Files\Nuke%NUKE_VERSION%\Nuke%NUKE_MAJOR%.exe"
But when I run a .bat with the code, it returns this error:
NUKE_VERSIONi was unexpected at this time.
Any insights into what is going on? I could just do this in python, but something this simple I shouldn't have to, right? Many thanks in advance.
In a CMD Window a FOR-LOOP uses a single % sign as you have listed in your question.
In a Batch file a FOR-LOOP uses a double %% sign.
FOR /F "delims=v tokens=1" %%i IN ("%NUKE_VERSION%") DO set NUKE_MAJOR=%%i
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.
Hi all I'm currently writing up a small bash script to automate some stuff for me but I've hit a bit of a snag My current file looks like the following:
for /f "delims=" %%f in ('dir /b "D:/*"') do C:\MediaInfo\MediaInfo.exe "--Inform=Video;%Width% "D:\%%f"
pause > nul
The pause thing is just there so I can see the output. While the part after the |do| command works fine if I manually type it in (as in I know my syntax for that is correct) however when running the batch script instead of actually executing the above commands it simply prints them out to the command console. Am I missing some syntax here or similar. Also as a side note I would like to push the resulting value of that query into an int so I can use it, do you know if this is possible in bash or should I look at trying to use a higher level language? Thanks!
I have no notion of the intricacies of mediainfo - but it would be unusual if it was to accept unbalanced quotes in its command line as you have posted. I'd suggest an extra after %Width%
At the following location
\ncsusnasent02.na.jnj.com\its_diq_na_win_dev\PowerCenter\infa_shared\WCPIT_BIO_EDW\SrcFiles\DDDMD\DDD.CLI026.WK0933.DDDMR45.001.head
I have one file
DDD.CLI026.WK0933.DDDMR45.001.head
if i open this file
i get data as following(in a single line)
HEADER0101IMS HEALTHDMD Weekly D DD.CLI026.WK0933.DDDMR45 Centocor DMDDRM45 W2009080210120090831125325ssnyder#us.imshealth.com
TRAIL0101 000000000581 0000000000CKSUM000002236804730
we need to copy 581(it will not be same always it gets updated everyday) from this file
and put it in a variable
you can try the below. It will set the field into the environment variable id:
for /f "tokens=10" %%a IN (%1) do (
SET id=%%a
)
echo %id%
You can pass the full path and file name into the bat as the first argument.
edit:
This simple bat will take the input from the file you specify on the commandline (param %1), it will use the default separators of <space> and <tab> to break the line in your file - defined in the IN set - into a set of tokens. The "tokens=10" param tells the processor to pass the 10th token, which turns out to be your number in question, into the DO block. It is passed in as a param %%a. Within the DO block, I simply assign that value to an environment variable id. After the for command is complete, I echo the value out to the console.
Take a look at the FOR command, specifically the part about the /F parameter.
I'm not certain enough about the structure of that line to even try to write the full command, but you should be able to write it yourself given that information.
Hmm to me it looks more like the guy needs a dos substr... i.e.
#Echo Off
If not %1.==[]. (Cmd /V:On /C Call %0 [] %1 & GoTo :EOF)
Shift
Set MyVariable=HELLOWORLD
Set ASubStr=!MyVariable:~%1!
Echo [!ASubStr!]
So for example save this as test.bat and then call "test.bat 5" and it will echo WORLD
Google DOS Substring and work out how to parse your text variable the way you want it.
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.