Click here to Skip to main content
15,888,199 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to take c# output in batch file variable ,can anyone please help.



Following is my sample batch file code.
@echo off
for /F "delims=" %%a in (C:\Users\u316383\Documents\Backup\ConsoleApplication3.exe) do set datayyyymmdd=%%a. ECHO datayyyymmdd. @wait 15



simple c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program{ static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Posted
Comments
phil.o 23-Jun-14 10:53am    
What does a "Hello World" application that you caught somewhere have to do with your question?
What is the question, by the way?
vicvis 23-Jun-14 13:12pm    
actually I want to take string output generated in a c# programas a variable so that can be use in another program via batch file

1 solution

Understanding all the variants of the FOR command would be quite an achievement and I hope you started by reading the documentation http://technet.microsoft.com/en-us/library/bb490909.aspx[^].

To have the filename treated as a command and have it's standard output parsed, you will need to add the usebackq option to your parsing keywords and back quote the filename.

for /F "usebackq delims=" %%a in (`C:\Users\u316383\Documents\Backup\ConsoleApplication3.exe`) do set datayyyymmdd=%%a. 


If the filename contains spaces then use back quotes and double quotes.
for /F "usebackq delims=" %%a in (`"C:\Users\u316383\Documents\Backup Files\ConsoleApplication3.exe"`) do set datayyyymmdd=%%a. 


Alan.
 
Share this answer
 
Comments
vicvis 23-Jun-14 13:28pm    
I tried as per your suggestion but still it is not displaying(using echo command).
for /F "usebackq delims=" %%a in (`"C:\Users\u316383\Documents\Backup Files\ConsoleApplication3.exe"`) do set datayyyymmdd=%%a.

Any help will be appreciated.

Thanks in advance
Alan N 24-Jun-14 6:19am    
What does happen? The command processor usually outputs an error message if it can't interpret a line in a batch file.

Why not start with something simple using a built in console command, e.g.
FOR /F "usebackq delims=" %%a IN (`Date /t`) DO SET DATEVAR3000=%%a
ECHO DATEVAR3000 is %DATEVAR3000%

That should echo today's date.

Once you that working correctly insert your own application and test again making note of any error messages.
vicvis 24-Jun-14 11:07am    
Thanks for the suggestion>I tried and code is working without arguement.the correct code is :
@echo off

FOR /F "usebackq delims=" %%a IN (`C:\Users\u316383\Documents\ReturnFileName.exe`) DO SET DATEVAR3000=%%a
ECHO DATEVAR3000 is %DATEVAR3000%

@Wait 15

but how to pass arguement in this?
Alan N 24-Jun-14 11:41am    
Sorry I don't understand.

You know how to get the output from ReturnFileName.exe into a variable. What do you want to do with the variable?

It may help if you give an example of the command line you would like to use.
vicvis 24-Jun-14 11:32am    
I Got it.complete code :@echo off

FOR /F "usebackq delims=" %%a IN (`C:\Users\u316383\Documents\ReturnFileName.exe "C:\Users\u316383\Documents" "1232" "19901212"`) DO SET DATEVAR3000=%%a
ECHO DATEVAR3000 is %DATEVAR3000%

@Wait 15.Thanks all for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900