Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a C# program that calculates a date. I want to set an environment variable datayyyymmdd to be read outside the program to be expaned into a filename that I need to look for with the Dos code.

To be more specific:
input arguements for abc are x and y.
I’d like to basically do like this… calling my program (say abc.exe")  that give date from batch file and in that batch file i wish to add something like this :
    Set var1=[call abc.exe.exe "E:\\flags\\reportVersions" "x" "y"

var1 should have the date that comes from abc.exe
Posted
Updated 20-Jun-14 4:14am
v3

The Environment.SetEnvironmentVariable[^] method provides three targets via the EnvironmentVariableTarget parameter:

  • Process - the variable only applies to the current process;
  • User - the variable is stored in the registry for the current user, and persists across reboots;
  • Machine - the variable is stored in the registry for the entire computer (requires elevation), and persists across reboots;


None of those options look suitable for what you're trying to achieve.

Since you're just setting the variable to an eight-digit number, which would fit within a 32-bit integer, why not simply return the value from your program, and use the batch file to set the variable?

C#
static class Program
{
    static int Main()
    {
        DateTime theDate = DoSomeFunkyCalculations();
        int result = (10000 * theDate.Year) + (100 * theDate.Month) + theDate.Day;
        return result;
    }
}


YourProgram.exe
SET date = %errorlevel%
...
 
Share this answer
 
Comments
vicvis 20-Jun-14 10:30am    
I am comfortable with .net part ..but have no idea how to pass date to calling batch file.
writting this in batch file SET date = %errorlevel% will give errorlevel not date.
Appolozies if I am wrong in underatanding..
Richard Deeming 20-Jun-14 10:41am    
SET date = %errorlevel% will set an environment variable called "date" to the integer value returned from the program you executed on the previous line.

The C# program is returning the date in the form "yyyyMMdd" as an integer, which is the same format you want in your filename.

Therefore, you will get an environment variable called "date" which contains, for example, "20140620", which you can then use to build your filename.
vicvis 20-Jun-14 10:58am    
Thanks Sir,

But does the below code make any sense to you.it was given to me as a sample by my senior.Since I am novice in batch file,its bit confusing to me.


I write out the the stdout my value and in the bat file.

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('FileDateCrtBDM.exe') do (
set varjp=%%a
)
echo myvar: %varjp%
vicvis 23-Jun-14 10:04am    
sir i tried but still,no result.Following is my sample code._:@echo off for /F "delims=" %%a in (C:\Users\u316383\Documents\Backup\ConsoleApplication3.exe) do set datayyyymmdd=%%a. ECHO datayyyymmdd. @wait 15:and simple 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!"); } } }
vicvis 26-Jun-14 6:13am    
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.
I haven't taken the time to find out how, so in the past I have had the console app write a BAT file that can then be called. :shrug:
 
Share this answer
 
First of all, there is no such thing as "DOS command", because, on all systems capable of running .NET, there is not such thing as "DOS". And of course, the variables don't "persist" when a program is closed. Data can persist, so you can assign the same very set of data to the same variables when you run an application again. This is called "serialization". But you would need physically write data to file or some other stream and read it back.

Please see:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

Now, it is not recommended to use such archaic and intrusive way of persisting any data using environment or system registry. Such things contaminate the system and not really needed. You should better save data using so called "special folders" assigned per user account or for "all users". Please see my past answers:
http://www.codeproject.com/Answers/454611/How-to-find-my-programs-directory#answer2[^],
which is better : use of registery in order to store setting or use of Settings class[^],
How to access a file from html folder of root directoty[^].

[EDIT]

If you really want to use the batch file (why, if you already program in C#?), you can save the data in another file, say, in the same directory as the batch file. This is all real low-tech stuff, can be use only in a pinch, and only if it does not make batch files too complex. Doing anything too serious with batch file looks like lame.

—SA
 
Share this answer
 
v2
Comments
vicvis 20-Jun-14 10:33am    
Thanks for your reply Sir,but my requirement is to use something like "
Set var1=[call abc.exe.exe “E:\\flags\\reportVersions” “x” “y”"[abc is my c# program with x and y as input arguemenment.

I would have called another c3 but my requirement passed to me by my architect ask me to do my work in the prescribed format.

Any help will be appreciated.

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