0

I have a simple Console application which returns a int value out , when i run this application from command , How do i capture the Out put ? Code

static int Main(string[] args)
{
            return 1;
}

I used the following line of Command

for /f %a in ('D://Test//ConsoleApplication1//ConsoleApplication1//bin//Debug//ConsoleApplication1.exe') do set "dow=%a"

When i output echo %dow% , it prints what is in front of echo and not the value .

REDEVI_
  • 684
  • 8
  • 18
  • What do you mean by "in front of the echo?" Please show your `echo` command. Are you running both commands (the `for` and the `echo`) from the prompt or in a batch? Are you aware that the path-separator for windows is a backslash, not two successive forward-slashes? – Magoo Feb 05 '18 at 14:06
  • Replace `//` by `\ `and double the percent-sign in `%a`... – aschipfl Feb 05 '18 at 14:08
  • 1
    Possible duplicate of [Capture output command CMD](https://stackoverflow.com/questions/14646575/capture-output-command-cmd) – aschipfl Feb 05 '18 at 14:08
  • batch file or cmdline? – Gerhard Feb 05 '18 at 14:09
  • @GerhardBarnard i m first trying in a command line ,later will be moving to batch – REDEVI_ Feb 05 '18 at 14:10
  • @Magoo I meant when i tried to ouput the variable – REDEVI_ Feb 05 '18 at 14:11
  • ok, so do you want the full `static int Main(string[] args) { return 1; }` as the output? – Gerhard Feb 05 '18 at 14:11
  • no i just want 1 , the returned value – REDEVI_ Feb 05 '18 at 14:13
  • Have you shown us your `echo` command yet? – Magoo Feb 05 '18 at 14:14
  • C:\Windows\system32>for /f %a in ('D:\test\ConsoleApplication1\Consol eApplication1\bin\Debug\ConsoleApplication1.exe') do set "dow=%a" C:\Windows\system32>echo %dow% %dow% – REDEVI_ Feb 05 '18 at 14:15
  • Please check the suggested duplicate. Although that question specified Windows XP, it applies equally to subsequent versions of Windows. – Jeff Zeitlin Feb 05 '18 at 14:16
  • Additionally, if you have an EXE that exits returning a value, that value will be interpreted as `%ERRORLEVEL%`. – Jeff Zeitlin Feb 05 '18 at 14:17
  • I m running this on a windows server 2012r2 – REDEVI_ Feb 05 '18 at 14:17
  • @JeffZeitlin how do i use the ErrorLevel , can you send the full command , I m pretty new to this – REDEVI_ Feb 05 '18 at 14:17
  • I _strongly_ suggest you find some batch references - I generally use [SS64.com](https://ss64.com/nt/) for batch. The use of `%ERRORLEVEL%` is very nearly a _fundamental_ part of batch. – Jeff Zeitlin Feb 05 '18 at 14:19

1 Answers1

0

You can create bat file next to your exe file and write inside bat this:

YourExeApp.exe > d:\test.txt

Don´t use C:\test.txt directly. Use rather C:\test\test.txt

Mi1anovic
  • 458
  • 1
  • 4
  • 11