How to assign stdout to variable?

Discuss and share scripts and script files...
Huidong
Posts: 213
Joined: 18 May 2011 21:55

How to assign stdout to variable?

Post 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!

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: How to assign stdout to variable?

Post by binocular222 »

+1, should be better than my work around here http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=10166
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

admin
Site Admin
Posts: 66261
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: How to assign stdout to variable?

Post 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.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to assign stdout to variable?

Post by bdeshi »

Could be possible via vbs/wsh.
Anyone know how to use WM_COPYDATA with vbs?
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to assign stdout to variable?

Post 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
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: How to assign stdout to variable?

Post 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
To see the attached files, you need to log into the forum.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to assign stdout to variable?

Post by bdeshi »

It's the output. It has a dangling empty line.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: How to assign stdout to variable?

Post 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.

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to assign stdout to variable?

Post by bdeshi »

Don't bother. Its cmdprompt's own fault.
cmdemptyline.png
To see the attached files, you need to log into the forum.
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

klownboy
Posts: 4459
Joined: 28 Feb 2012 19:27
Location: Windows 11, 25H2 Build 26200.8037 at 100% 2560x1440

Re: How to assign stdout to variable?

Post 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);

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: How to assign stdout to variable?

Post 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!

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: How to assign stdout to variable?

Post by LittleBiG »

Years before I also needed this function, but I don't remember what the exact case was... :roll:

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: How to assign stdout to variable?

Post by bdeshi »

Here's dreaming of run "cmd /c some command | <xy>"
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

binocular222
Posts: 1423
Joined: 04 Nov 2008 05:35
Location: Win11, Win10, 100% Scaling

Re: How to assign stdout to variable?

Post 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
I'm a casual coder using AHK language. All of my xys scripts:
http://www.xyplorer.com/xyfc/viewtopic. ... 243#p82488

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: How to assign stdout to variable?

Post 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>;

Post Reply