Cynosure.X International LLC

:

Add Comment | Related Links | TrackBack
Related Content

SetVariableToOutput.bat (GPL)

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.

Usage

SetVariableToOutput.sh command

Examples

call SetVariableToOutput.bat date /t
set date=%OutputVariable%

call SetVariableToOutput.bat time /t
set time=%OutputVariable%

Attached File: SetVariableToOutput.bat (513 B)

Chieh Cheng
Wed, 11 Jun 2008 00:03:45 +0000

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.

Attached File: 1 - SetVariableToOutput.bat (531 B)

Chieh Cheng
Thu, 25 Sep 2008 14:33:33 +0000

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.

Attached File: 2 - SetVariableToOutput.bat (530 B)

Chieh Cheng
Wed, 01 Oct 2008 09:41:25 +0000

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.

Attached File: 3 - SetVariableToOutput.bat (558 B)

Chieh Cheng
Thu, 18 Dec 2008 00:24:19 +0000

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.

Attached File: 4 - SetVariableToOutput.bat (581 B)

Chieh Cheng
Sun, 08 Mar 2009 05:27:22 +0000

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.

Attached File: 5 - SetVariableToOutput.bat (775 B)

Chieh Cheng
Thu, 19 Mar 2009 09:41:06 +0000

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.

  SetLocal EnableDelayedExpansion

if "%success%" == "true" (
call SetVariableToOutput.bat type "%drive%\Purify.cfg"
set purify=!OutputVariable!
echo !purify!
)

See the "Multi-Line IF Statements in Batch File" TrackBack below for more details.

Chieh Cheng
Tue, 22 Sep 2009 05:11:30 +0000

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.

Attached File: 6 - SetVariableToOutput.bat (975 B)

Chieh Cheng
Wed, 22 May 2013 23:06:22 +0400

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.

Attached File: 7 - SetVariableToOutput.bat (996 B)

Chieh Cheng
Sun, 16 Mar 2014 09:21:35 +0300

Here is another example of using double quotes to provide piping of commands:

  call SetVariableToOutput.bat "echo "%file%" | java eGrep -c " for ""

Chieh Cheng
Sat, 25 Jul 2015 14:12:36 +0400

Add Comment | Related Links | TrackBack
Related Content

Did your message disappear? Read the Forums FAQ.

TrackBack

Send Ping | TrackBack URL | Spam Control

Title: Multi-Line IF Statements in Batch File
Weblog: Cynosure.X International
Excerpt: Thanks, TheBlackOne! That worked. Delayed expansion has to be enabled for this trick to work. The following is an example batch program that demonstrates this trick. SetLocal EnableDelayedExpansion if "" == "" ( set output=*** just some text echo !output! ) EndLoc . . .
Tracked: Tue, 22 Sep 2009 05:12:40 +0000

Add Comment

Spam Control | * indicates required field
Your Name: *
E-mail:
Remember Me!
Comment: *
File attachment is optional. Please do not attach a file to your submission unless it is relevent.
Attach File:
(20 MB Max)
Spam Protection: * Answer of 3 - 7?
Click button only once, please!

Messages, files, and images copyright by respective owners.

Products | Services
Forums | Latest | RSS
Library | Search | Wiki
Help | Licenses

Login | Register

12 Users Online

Hacking Digital Cameras
Fun for Photographers


Amazon Associate


Copyright © 1996 - 2017. All Rights Reserved.