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.
Related
I'm having trouble running !var! examples ad described here http://ss64.com/nt/delayedexpansion.html
Instead of the expected variable content output as the example describes, I get the literal "bang V A R bang" output, any idea?
C:\>Setlocal EnableDelayedExpansion
C:\>Set _var=first
C:\>Set _var=second& Echo %_var% !_var!
first !_var!
thanks.
You are getting an unexpected result because you are issuing the commands at the command prompt. Create a batch file by putting the following commands in a file with a .bat extension then run the batch file.
#echo off
Setlocal EnableDelayedExpansion
Set _var=first
Set _var=second& Echo %_var% !_var!
E.g., if I created a batch file named delayedexp.bat with the above contents, I would see the following when I run it:
C:\Users\JDoe\Documents\>delayedexp
first second
setlocal only works within the confines of a command script:
help setlocal
If you have access to the parameters of the cmd call, you can set parameter /v. Must be first. And use ! instead % for variables.
%windir%\system32\cmd.exe /v /c set a=10&echo a=!a!&echo My Path is %CD%&pause
This is how, for example, you can get the date in the Russia-France format directly in the Windows shortcut, where a simple percentage is impossible due to its doubling. In std queries with the /v parameter, both a percentage and an exclamation mark will work fine, but single % for cicles.
%windir%\system32\cmd.exe /v /c echo off&for /F "tokens=1-6 delims=:., " %A In ("!date! !time!") Do (Echo %A.%B.%C %D:%E:%F)&pause
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%
Let's say I have a text file, it contains batch commands. How can I run that text file as a batch file from within one, without renaming it. I want too keep a portable aspect too it, so no registry keys or such.
The reason for no renaming is too prevent leftover unrenamed files upon unexpected closure.
The simplest way is this:
cmd < file.txt
As in the previous answers, there are several commands that will not work in this file, like GOTO, SETLOCAL and others. However, multiline nested if and for commands do work as long as for replaceable parameters use just one percent (like in the command-line).
Although this method alaways show in the screen the executed commands (#echo off not works here), you may redirect the output to NUL and in the "Batch" file redirect the desired output to CON. For example, this is test.txt:
#echo off
echo Hello World! > CON
(for /L %a in (1,1,10) do (
echo Turn: %a
if %a equ 4 echo TURN NUMBER FOUR!
)) > CON
Output example:
C:\> cmd < test.txt > NUL
Hello World!
Turn: 1
Turn: 2
Turn: 3
Turn: 4
TURN NUMBER FOUR!
Turn: 5
Turn: 6
Turn: 7
Turn: 8
Turn: 9
Turn: 10
type some.txt>temp.bat
call temp.bat
del /q /f temp.bat
Is creating a temp file cheating?It's not mentioned as restriction in the question.Though you can loose the %ERRORLEVEL% because of the del command , but you can keep it in temp variable:
type some.txt>temp.bat
call temp.bat
set temp_el=%errorlevel%
del /q /f temp.bat
exit /b %temp_el%
I'm pretty sure you cannot do what you want. Windows will not let you configure the OS to recognize any other extensions as batch files. Only .bat and .cmd are supported.
You could process a series of simple commands within a text file using a FOR /F loop, but it will be very restrictive. For example, it will not support IF, FOR, GOTO Label, or CALL :Label. There are probably other restrictions. Within your main batch file, you could have the following:
for /f delims^=^ eol^= %%A in (script.txt) do %%A
You might be able to support IF and/or FOR if you execute the command via a new CMD.EXE shell, but then you cannot preserve the value of variables that might be SET by the command.
for /f delims^=^ eol^= %%A in (script.txt) do cmd /c "%%A"
See the windows shell commands
assoc. Associates a filename extension (e.g. *.txt) with a file type.
ftype. Lets you create a new file type that tells the windows shell how to open a particular kind of file.
From a command prompt, typing assoc /? or ftype /? will get you help on them. Or use your google-fu to find the MS docs.
*.bat is mapped to the file type batfile; *.cmd is mapped to the file type cmdfile. In windows 7 they are identical. If you want to be able to run files named *.foobar as a batch files, just type:
assoc .foobar=cmdfile
Then, assuming a file named 'sillyness.foobar' existing on the path, you just just type
c:\> sillyness
and it will find sillyness.foobar and execute it as a batch file. The Windows shell has a priority for how it resolves conflicts when you have files with the same name and different extensions (.com vs .cmd vs .bat, etc.)
Something like
assoc .pl=perlscript
ftype perlscript=perl.exe %1 %*
will set you up to run perl scripts as if they were .bat files.
If your batch commands are simple sequential ones then you could use this at the command prompt. Double the % signs for use in a batch file.
for /f "delims=" %a in (file.txt) do %a
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.
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.