User1551892
User1551892

Reputation: 3384

How to use the output of one operation as input for other operation in batch file?

I want to perform two tasks in batch file. First, I create a file using a small application in c# and this application returns some value if file creation done successfully. Then on the basis of output from task, I want perform robocopy operation. I need some help related to keep the output of first operation and later on use it in comparison to perform second operation.

xyz.exe
if outputofxyz=="OK" Robocopy "\\Directory1" "Directory2" 

I want to know that can I save output of xyz.exe in some variable? or should I use below syntax:

if xyz.exe=="OK" Robocopy "\\Directory1" "Directory2" 

Upvotes: 0

Views: 88

Answers (2)

Endoro
Endoro

Reputation: 37589

for /f "delims=" %%a in ('MyProgram') do set "output=%%~a"
robocopy [sourcefolder] [targetfolder] [files] %output%

Upvotes: 0

Monacraft
Monacraft

Reputation: 6610

Ok, the best thing you could do is modify your program so that instead of returning the value you used Console.WriteLine(); To announce whether the job was success full:

Program.cs

// Do work
if (completed)
    Console.WriteLine("OK");
else
    Console.WriteLine("Fail");
// Note don't output anything else to the Console.
// Rest Of code

Main.bat

Program.exe > Out.txt
set /p out=<Out.txt
del Out.txt
if "%out%" EQU "OK" (
Robocopy "\\Directory1" "Directory2" 
) Else (
Echo Task not Complete.
)
Exit

That should do what you want, but restricts you from outputting anything else to the console. Ofcourse if you had to do this, you could instead output the the data as the last last line of the file and the use for /f. Lastly, you could utilise the Environment Variable errorlevel but that would require you to show us you're code for modification.

Mona.

Upvotes: 1

Related Questions