Page 1 of 2
How to assign stdout to variable?
Posted: 02 Jun 2014 00:36
by Huidong
Hi Don,
A naive question, how, if possible at all, can I assign the output of a third-party utility to a variable, and then work from there? I mean without having to redirect the output to a file and then read from it.
I got interested in this due to the tremendous potential of the Custom Columns / Scripted Columns. I like it very much that image dimensions can now be displayed in a column, but not video dimensions. So I thought maybe there's a way to hook
MediaInfo to XY?
For instance, there's a neat command
Code: Select all
mediainfo --output=Video;%Height% C:\video.mp4
that returns the height of the video in pixels to stdout.
But is it possible to directly assign this output to an XY variable?
In case it's not doable yet, I am also wondering if Don would be interested in looking into this possibility? (That could open up a new world, making use of any command-line tools, MediaInfo is just one example to make Custom columns work with most video files.)
Thank you very much!
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 05:13
by binocular222
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 09:47
by admin
I think it would be possible to add that, but it's a bit tricky to do. Sorry, but no time for it now.
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 09:49
by bdeshi
Could be possible via vbs/wsh.
Anyone know how to use WM_COPYDATA with vbs?
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 10:32
by bdeshi
meanwhile here's a very crude workaround.
Run this vbscript instead of the command.
paste your command where noted and replace the path to XYplorer to match yours.
A new permanent variable $somevar is created with the stdoutput.
THIS WON'T WORK if the output has any characters that confuse the XYplorer commandline parser (or the vbscript itself). Also it probably doesn't work with large outputs.
Note that all linebreaks are replaced with the pilcrow ¶.
Code: Select all
'VBSCRIPT code
Set WshShell = WScript.CreateObject("WScript.Shell")
Set cmd = WshShell.Exec("%COMSPEC% /c echo somevalue") ' paste your command here
'Do While cmd.Status = 0
' WScript.Sleep 50
'Loop
'cmd.Terminate
cmdOutput = cmd.StdOut.ReadAll
cmdOutput = Replace(cmdOutput, vbcrlf, "¶")
WshShell.Run "C:\portable\XYplorer\XYplorer.exe /flg=2 /script=""::perm $somevar=" & chr(39) &cmdOutput & chr(39) & chr(34)
'$somevar holds the output now
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 17:06
by klownboy
MediaInfo sounded interesting so I downloaded it. I wasn't positive which of the 3 Windows x64 versions I should download (GUI, CLI, or DLL) so I grabbed the CLI version. Anyway, I put this little script in a Custom Column and it seems to work fine other than the fact that I get a Paragraph symbol after the video size and I haven't figured out why yet.
Code: Select all
run "cmd /c D:\DVD\MediaInfo\MediaInfo.exe --output=Video;%Height%x%Width% ""<cc_item>"" | clip", ,2,0;
return <clipboard>;
You can assign the clipboard a variable/perm variable as well, but it doesn't seem to be necessary.
MediaInfo_Capture.PNG
I haven't worked with CC enough yet, does anyone know why I might be getting the paragraph symbol after the video size. Maybe something about the output from Mediainfo possibly. I tried using Number, Text, and Mixed for the CC format (with my 'X" in between the height and Width, I think it has to be mixed).
Thanks,
Ken
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 17:26
by bdeshi
It's the output. It has a dangling empty line.
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 17:37
by klownboy
Thanks Sammay, I kind of figured it was something along those lines [no pun intended

]. I wasn't sure if it was the "output" format from Mediainfo or due to the use of clip. I'll look a the MediaInfo help for command lines to see if there a way to eliminate it or maybe Huidong might know.
It would be nice if we could capture output like this without using these work arounds.
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 18:07
by bdeshi
Don't bother. Its cmdprompt's own fault.
cmdemptyline.png
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 18:23
by klownboy
OK Sammay thanks, I saw your last post as I was posting. So it isn't something I can probably change via the MediaInfo command line. Wow, what a huge amount of info it's capable of pulling out. I did manage to get rid of the linefeed marker by using "substr". Use this instead of what I posted earlier as a script in Custom Columns.
Code: Select all
run "cmd /c D:\DVD\MediaInfo\MediaInfo.exe --output=Video;%Height%x%Width% ""<cc_item>"" | clip", ,2,0;
return substr("<clipboard>", 0, -1);
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 18:37
by Huidong
Thank you klownboy and SammaySarkar for helping me out! It's always good to learn from power users!
And I guess we'll just check back with Don once in a while regarding a "native" mechanism of redirecting stdout to XY!
Re: How to assign stdout to variable?
Posted: 02 Jun 2014 20:00
by LittleBiG
Years before I also needed this function, but I don't remember what the exact case was...

Re: How to assign stdout to variable?
Posted: 03 Jun 2014 04:52
by bdeshi
Here's dreaming of run "cmd /c some command | <xy>"
Re: How to assign stdout to variable?
Posted: 03 Jun 2014 15:46
by binocular222
As normal script (not custom column) I cannot make it to work (I'm using CLI version too)
Code: Select all
run "cmd /c ""E:\4Media\MediaInfo\MediaInfo.exe"" --Inform=Video;%Width% ""<curitem>"" | clip",,2,0;
echo <clipboard>
I doubt that the semicolon and percentage sign is problematic, but enclosing them in single quote doesn't help
Re: How to assign stdout to variable?
Posted: 03 Jun 2014 16:23
by Huidong
binocular222 wrote:As normal script (not custom column) I cannot make it to work (I'm using CLI version too)
Code: Select all
run "cmd /c ""E:\4Media\MediaInfo\MediaInfo.exe"" --Inform=Video;%Width% ""<curitem>"" | clip",,2,0;
echo <clipboard>
I doubt that the semicolon and percentage sign is problematic, but enclosing them in single quote doesn't help
I tried without the double quotes around the mediainfo executable, and that seemed to work:
Code: Select all
run "cmd /c E:\4Media\MediaInfo\MediaInfo.exe --Inform=Video;%Height% ""<curitem>"" | clip", , 2, 0;
echo <clipboard>;