SetVariableToOutput.bat is a Windows batch program that sets a variable to the output of an execution. With it, you can use the output of another program with your batch file. SetVariableToOutput.bat sets %OutputVariable% that your batch script can use.
To use this script, simply call this script with the command you would like to execute. Then the %OutputVariable% will contain the output from the execution. See examples below for more details.
SetVariableToOutput.bat takes advantage of the SetNumberOfArguments.bat (GPL) script that is available on this web site. So to use SetVariableToOutput.bat, you'll have to download SetNumberOfArguments.bat, if you don't already have it.
SetVariableToOutput.bat is distributed as executable source code under the GNU General Public License. Please see the license agreement elsewhere on this site.
call SetVariableToOutput.bat date /t
I was trying to use this SetVariableToOutput.bat script today on Japanese version of Windows Vista today. However, it refused to let me write or change any files in the root directory of any drive from the command-line. So I had to update the SetVariableToOutput.bat script to write its temporary files to the temporary directory specified by the Windows environment variable %temp%. The new version of SetVariableToOutput.bat is attached below.
The earlier versions of SetVariableToOutput.bat echo'ed a space character to standard out whenever it is executed. That is an undesirable result that is fixed in the version that is attached below.
The temporary variable names used in earlier versions of SetVariableToOutput.bat was too common; it stepped on new scripts that I was writing. So I had updated this script to use unlikely variable names. The new version is attached below.
I was using this SetVariableToOutput.bat script today in a new script that I was writing. The new script used this script in a loop. When the output of the command is an empty string, the %OutputVariable% doesn't get set to an empty string (strange). So in this new version of this SetVariableToOutput.bat script, I explicitly set %OutputVariable% to an empty string at the beginning of the execution.
Found another shortcoming with SetVariableToOutput.bat. If you use this batch file to call another batch file that call this batch, the temporary files and variables gets overwritten. I have made improvements to SetVariableToOutput.bat so that the temporary files do not step on each other. And it has also been improved to start new Windows command shell so that you can recursively call this script as many time as you want (until you exhausts system memory). The newly improved version of this script is attached below.
Starting with this version, you'll need to install the GetTempPathName.bat (GPL) script, if you haven't already done so. SetVariableToOutput.bat uses it to create temporary files.
You can use this batch file in a multi-line IF statements with delayed expansion enabled. The following example demonstrates how to use it in a multi-line IF statement.
See the "Multi-Line IF Statements in Batch File" TrackBack below for more details.
Found a concurrent process problem, where SetVariableToOutput.bat isn't concurrent process safe. I've made a small change, now you can run SetVariableToOutput.bat in multiple processes at the same time. The new version is attached below.
Made a minor change to the way commands are executed in SetVariableToOutput.bat. With this simple change, you can now pass in commands containing spaces. Quoting the command is much easier now. Here is an examples:
call SetVariableToOutput.bat ""With Space.cmd" "a b c""
Note the two double-quotes in the front and the two double-quotes at the end? The extra double-quote at the front and at the end encloses the entire string as one command.
The new version of SetVariableToOutput.bat is attached below.
Here is another example of using double quotes to provide piping of commands:
call SetVariableToOutput.bat "echo "%file%" | java eGrep -c " for ""
Did your message disappear? Read the Forums FAQ.
Title: Multi-Line IF Statements in Batch File
Spam Control | * indicates required field
Messages, files, and images copyright by respective owners.
24 Users Online
Copyright © 1996 - 2017. All Rights Reserved.