Batch file piping output to variable




















JohannesM JohannesM 9 9 silver badges 17 17 bronze badges. Risoos Risoos 1. I don't understand how this answers the question. You seem to be answering a different question. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. If result. You could also make result. What you see above sends the output to a named file. If file does not exist, it creates one.

Overwrites existing file And you can also do this:. See also here: Using command redirection operators. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. I haven't tried much as I haven't been able to think of anything to try, but I have tried one method with two variations I know you can do it fairly easily with the FOR command, but I think it would look "nicer" with a pipe.

And if you're wondering, I don't have a specific reason I'm asking this other than I'm curious and I can't find the answer. The other problem is the pipe. A pipe starts two asynchronous cmd. That's the cause why it seems that the variables are not set, but a small example shows that they are set but the result is lost later.

Alternatives: You can use the FOR loop to get values into variables or also temp files. The definition of the macro can be found at SO:Assign output of a program to a variable using a MS batch file.

Using backquotes via for-loops is not at all cosy. So we need kinda of setvar myvar cmd-line command. What if the command you want to pipe to some variable contains itself a pipe? Then use that tempfile as the input to set a new variable I called it NOW 3. And if the Tempfile is ever deleted, it will be re-created as necessary. In a batch file I usually create a file in the temp directory and append output from a program, then I call it with a variable-name to set that variable.

Like this:. I find myself a tad amazed at the lack of what I consider the best answer to this question anywhere on the internet. I struggled for many years to find the answer. Many answers online come close, but none really answer it. The real answer is. The "secret sauce" being the "closely guarded coveted secret" that "echo. Copy the following code into Notepad and save it as "test. It is said Console cannot be redirected, and I believe that's true.

I can assure you I did try! In this case, we could also have used test. With the NUL device that's no problem, but when redirecting to a file one of the redirections will lock the file for the other redirection. The result will be an empty logfile.

A workaround that may look a bit intimidating is grouping the command line and escaping the redirection:. What this does is turn the part between parentheses into a "literal" uninterpreted string that is passed to the command interpreter of the newly started process, which then in turn does interpret it.



0コメント

  • 1000 / 1000