You are not logged in.

#1 08 Dec 2014 06:36

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 7

redirecting program return value as a parameter value

I can't figure out if this is possible or not - I have tried looking this up on Google and here, but nothing is coming up.

I often call a certain command line program that collects a huge number of arguments. (Lets say it's SoX.exe).

So, I have something like:

sox.exe <input file> <output file> "option1" <option1 param1> <option1 param2> "option2".......
e.g. 
sox.exe input.wav output.wav trim 10 45.25 fade 1

For one of those parameters/arguments, say 45.25 above, I'd like to get that value from a console program that returns the value I need, as opposed to typing it in manually.

So, the call to that console program would look like

get_my_value.exe input_file_from_which_this_value_is_taken.xyz

And the return is 45.25 in the console output.


Is it possible to somehow plug this call into my command line at the top?

something like

sox input.wav output.wav trim 10 {[some clever set of redirection operators here]get_my_value.exe input.txt} fade 1

Win8.1 x64

Offline

#2 08 Dec 2014 09:10

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: redirecting program return value as a parameter value

You could do something like this:

for /f "usebackq tokens=*" %%a in (`get_my_value.exe input_file_from_which_this_value_is_taken.xyz`) do set someValue=%%a
sox.exe input.wav output.wav trim 10 %someValue% fade 1

cmd | *sh | ruby | chef

Offline

#3 08 Dec 2014 15:13

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 7

Re: redirecting program return value as a parameter value

Thanks for the suggestion. What am I doing wrong?

C:\Windows\system32>for /f "usebackq tokens=*" %%a in ('c:\temp\test\test.bat') do set someValue=%%a

%%a was unexpected at this time.

test.bat is a test file that contains

@echo off
echo 45.22

Win8.1 x64

Offline

#4 08 Dec 2014 16:45

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: redirecting program return value as a parameter value

At a prompt the for command will use %a instead of %%a

Offline

#5 08 Dec 2014 17:46

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 7

Re: redirecting program return value as a parameter value

Thanks - forgot about that.

Now it's still weird:

C:\Windows\system32>for /f "usebackq tokens=*" %a in ('c:\temp\test\test.bat') do set someValue=%a & sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 %someValue% fade 1

results in

C:\Windows\system32>set someValue=c:\temp\test\test.bat   & sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 c:\temp\test\test.bat fade 1

sox FAIL trim: usage: start [length|=end]

C:\Windows\system32>%someValue%
45.22

You can see that when I check someValue through %...% dereferencing it is properly assigned, but for some reason when I run "set" to display environment variables, the someValue variable is indeed assigned the name of the file in the for statement.

e.g.

ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files (x86)
ProgramFiles(x86)=C:\Program Files (x86)
ProgramW6432=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
SESSIONNAME=Console
someValue=c:\temp\test\test.bat
SystemDrive=C:
SystemRoot=C:\Windows

What's my problem?

(I have already tried doing everything from a batch file - there, after the first time, before someValue gets set, %someValue% dereferencing in the command line results in a blank.)
BTW, I should probably mention: Win8.1 x64 (don't know if there is something special about how this OS handles environment variables).


Win8.1 x64

Offline

#6 08 Dec 2014 19:01

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: redirecting program return value as a parameter value

The problem has to do with variable expansion (when the variable name is replaced with the value).  In your single line command, the %someValue% variable will be expnaded before it has been set since command.exe consumes the entire line at the same time.  See this thread http://stackoverflow.com/a/7970912/891976

for /f "usebackq tokens=*" %a in ('c:\temp\test\test.bat') do set someValue=%a & sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 %someValue% fade 1

If you want it all in a single line, do something like this

for /f "usebackq tokens=*" %A in ('c:\temp\test\test.bat') do sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 %A fade 1

Offline

#7 08 Dec 2014 19:11

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 7

Re: redirecting program return value as a parameter value

Thanks for explaining.

I tried your single line suggestion and got

C:\Windows\system32>for /f "usebackq tokens=*" %A in ('c:\temp\test\test.bat') do sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 %A fade 1

C:\Windows\system32>sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 c:\temp\test\test.bat fade 1

so, does that mean it's not the expansion that's the culprit?


Win8.1 x64

Offline

#8 08 Dec 2014 20:50

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: redirecting program return value as a parameter value

Remove the usebackq option as it is changing the purpose of the single quotation marks and instead of executing the test.bat is treating it as a literal string.

for /f "delims=" %A in ('c:\temp\test\test.bat') do sox c:\temp\test\r.wav c:\temp\test\output2.wav trim 10 %A fade 1

Last edited by DigitalSnow (08 Dec 2014 20:52)

Offline

#9 08 Dec 2014 21:47

a1s2d3f4
Member
Registered: 02 Oct 2008
Posts: 7

Re: redirecting program return value as a parameter value

Yes! Excellent.
Thanks all for your help.


Win8.1 x64

Offline

Board footer

Powered by