Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I mean to use it like this:

echo some data | this-tool-m-searching-for -vn myvariable

or

time | this-tool-m-searching-for -vn current_time

etc..

and then

echo %myvariable%

would produce some data etc...

This is not about echo or time only, I mean any program that gives output to stdout.

share|improve this question
1  
Consider echo %TIME% and echo %DATE%. On robvanderwoude.com you can find plenty of code that touches the topic you are looking into. –  0xC0000022L May 12 '12 at 22:42

2 Answers 2

This is the usual construction, I believe:

for /F %i in ('time /t') do set current_time=%i

(as usual, the percent signs need to be doubled in a command script.)

Another construction, though awkward:

time /t > tempfile.txt
set /p current_time= < tempfile.txt

There appears to be no way to avoid the temporary file; if you use a pipe, the set command happens in a subprocess. (In some situations you might be able to pipe the output to a second copy of your batch script, but that sort of thing can get ugly fast.)

Additional

To expand on my last point, in response to a question in the comments, here is an example batch file which pipes output into a new instance of itself:

if "%1" NEQ "" @goto %1
:step1
time /t | %0 step2
goto :eof
:step2
set /p current_time=
echo %current_time%
rem processing here...

I don't think this solution scales well for more complicated batch scripts.

share|improve this answer
    
Yes but it expands the %i (%%i in bath file) variable, and if program output contains some special characters you are in trouble. Pipe doesn't have this issue. –  rsk82 May 12 '12 at 22:43
    
People do some amazing things with command scripts, but for my part if you need to deal with special characters I always recommend bailing out and using vbscript or powershell instead. –  Harry Johnston May 12 '12 at 22:48
    
I know but I've written so much in *.cmd and it works well, the only weak link is here, this little tool and I'm saved. –  rsk82 May 12 '12 at 22:52
    
I've added another possible approach. –  Harry Johnston May 12 '12 at 23:10
    
how would you use the set command with a pipe? I see echo abc | set /p a= <-- doesn't do it. –  barlop May 12 '12 at 23:27

Try export.exe, it seems to do exactly what you're after.

share|improve this answer
    
Cool, but a 'bit' bloated though - 216kB for so tiny functionality.... And doesn't allow doing it thru pipe. –  rsk82 May 12 '12 at 22:40
    
In the absence of source code, I'd recommend caution with this tool. If it works as advertised, it must be doing DLL insertion or something equally nasty on the parent process, which means it might have unexpected side-effects. –  Harry Johnston May 12 '12 at 22:43
    
(I'd also run it past a few online virus/malware scanners if I were you.) –  Harry Johnston May 12 '12 at 22:44
    
And it just has, some ugly error came, something around writting to a parent process. –  rsk82 May 12 '12 at 22:45
    
virusscan.jotti.org/pl/scanresult/… - all ok, a stone dropped from my heart –  rsk82 May 12 '12 at 22:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.