Passing value from a C# program into a Windows environment variable - batch-file

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.
Can someone please help me with c# code sample or any link .
Thanks

This is not a trivial task as each process has its own environment variables table, especially if the environment variable should not be set permanently for all running processes.
The solution is quite simple.
In your C# application create a batch file with the line:
set YourVar=datayyyymmdd
The C# application could create this batch file with name "SetYourVar.bat" in directory for temporary files. The path of the temporary files directory can be get within the C# application from environment variable TEMP.
Your batch file contains the lines:
#echo off
"Full\Name\Of\C#\ConsoleApplication.exe" "parameters for this application"
if exist "%TEMP%\SetYourVar.bat" (
call "%TEMP%\SetYourVar.bat"
del "%TEMP%\SetYourVar.bat" >nul
)
"Full\Name\Of\Other\DOS\Application.exe"
So instead of trying to set the environment variables to use in other applications directly within the C# console application, write the lines to set them into a batch file called next by the batch file which started the C# console application and next deletes this temporary batch file just used to set environment variables.

Modify your C# program so it just display in the screen the value of the variable instead of set the variable itself. Then, execute the .exe from the Batch file this way:
for /F "delims=" %%a in ('abc.exe "any parameter"') do set datayyyymmdd=%%a

Related

Running ant commands using batch file

Currently this is how I have written the code in the batch file:
C:\ cd C:\abc\xyz\build-scripts-master
call setEnv.cmd
cmd ant do-clean
cmd ant do-dist
This is not working. It just executes the setEnv and breaks out. It does not run the remaining commands
Manually this is how it works:
I first go to the folder C:\abc\xyz\build-scripts-master through the Command Prompt
Then I type in setEnv, which is a windows command script, and hit return.
Then I type in ant do-clean
And then ant do-dist
I want to automate this process and hence was trying to achieve this using batch file.
Try the following:
#CD /D "C:\abc\xyz\build-scripts-master"
#Call setEnv.cmd
#Call ant.bat do-clean
#Call ant.bat do-dist
The latter two lines assume that ant.bat is located somewhere in the current working directory or %PATH%
It is not imperative that the directory path is doublequoted in this case, just good practice.You could continue not to use the .bat extension with ant. I've included it just to make it clear that it is a batch file, and should be Called in the same way as the setEnv batch file.
it didn't run the bat files because you didn't specify the files' location in the code. As it stands right now, the script expects the .bats to exist in the working directory or it has been placed in the folder. The only way it will run the files arbitrarily is if you had placed your working location in the system variables or set the Path to the folders location. I don't know if the cmd and call are needed. I have never used them in my scripts.

Script to Replace Text in %PATH% Environment Variable

As of right now when we update Java on a PC which users a certain program we use I have had to manually update the system path environment variables with the latest Java version number in the path (C:\Program Files (x86)\Java\jre1.8.0_141\bin\client).
My goal is to automate this process via a DOS script if possible. I am far from a coder but would like this script to search for the "jre1.8.0_141" then replace it with whatever build number the latest update is in the %PATH% variable. This way I can leave each individual PC's environment variables in tact and only edit the Java string.
I have looked at merging entries with the registry but would like to avoid that as all systems are not identical.
This is my script so far:
This is what I have so far:
REM Exports the Systems Environmental Variables
reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "C:\Software\%COMPUTERNAME%_ENV_VAR_BACKUP.reg" /y
REM Searchs for Java path and changes if necessary
IF "%PATH%" = "*jre1.8.0_141*" (
THEN **NOT SURE WHERE TO GO FROM HERE**
)
I know what I want to accomplish but I have no idea how to translate that into a working batch file so I apologize I have no more information for you.
Thank you for any help.

Escape the content of a variable

In my batch script I need to pass the current directory path to a program, escaped.
So, when my batch script is running in C:\Program Files\ it has to pass C:\\Program Files\\ to the called program.
How can I do that? Many thanks.
Background: The called program expects a replacement string for a regex operation, thus it will expect a group parameter when using \ only. The data that will be targeted by the regex operation is used by a software that won't accept relative paths or environment variables.
My first guess is %~dp0 the current path where the batch are located
But there is also %CD%, the current working directory.
try something like:
#echo off
setlocal
set "x=%CD%"
set "x=%x:\=\\%"
echo %x%
as mentioned by #joey you can directly call the current directory like this %CD:\=\\%

Using a .bat file for integration to client

I was trying to put together a .bat file program that presents readable information. Basically, we have a application called vSphere that reads custom scripts directly from a xml file. So there are xml elements that are passed into the the client. Now, I have a similar situation where we are doing the same thing with vCloud. The difference is that vCloud only reads data from a .bat file instead of a xml file.. If I wanted to do such a thing what would that look like.. Below is my psuedo code as I'm not completely familiar with .bat file syntax. Is this how I can present the information to be read by the vCloud client? The client already knows how to read the .bat file, I just need help with a control structure example of a .bat file to tackle this problem.
xml version:
<_type>viss.CustomizationExample</_type>
<changeExample>1341932956</changeExample>
.bat file version:
#echo off
SET ChangeExample=1341932956
if changeExample==""(
echo changeExample==ChangeExample
)
Thank you!
this code may work for you
#echo off
set changeExample=1341932956
echo %changeExample%
This will display the value of changeExample 1341932956 on the cmd screen if you run the batch file is this what you wanted...
Your almost there, just a couple of things.
To access a variable after setting it, without any special circumstances, you need to put %'s at each side of it to tell cmd that you want the value contained in it.
By default if is case sensitive, so you must first make sure that your variables are spelt
correctly, also you need a space between the end of the if condition and the bracket.
Provided that this is the correct format for the batch (I don't have a clue what it actually wants to read) then this is the correct syntax
#echo off
SET ChangeExample=1341932956
if %ChangeExample%=="" (
echo changeExample==%ChangeExample%
)
I presumed you wanted to echo the variable contents rather than the string ChangeExample, if you just want the string remove the %'s.

Could someone help to understand this batch script?

I'm reading a batch file, but I do not understand it, can someone help to explain?
As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?
I can not find the variable %BatchPath% in the file, where do you think it's defined?
And it seems APATH is defined in the two loops?
for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%
You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.
The first line performs this action on %0, indeed the path and name of the current script.
The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.
So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.
pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.
%0 is the current batch file.
%%~dpsx gives the current batch file's
short path here its giving the Drive name for eg "D:\"
Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified
directory.
APATH is some variable used to store the path.
so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.

Resources