0

In a batch file, you can prompt for values to environment variables using the format of set /p TestVar=Give me a string to save! and retrieve it using %TestVar%. However, when I try to do this in Java or C# and trying to immediately use it does not work:

cmd.exe /K "set /p TestVar=Give me a string to save! && ECHO %TestVar%"

the above code also only ECHOs the literal string "%TestVar%" rather than the string that was set to the TestVar variable.


I want to use that to set environment variables in Java and C# (and I don't want to use built in stuff like How do I get and set Environment variables in C#?)

For a Java example,

public class visibleCMD {
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"set /p TestVar=Give me a string to save! && ECHO %TestVar%\"");
        } catch (Exception e) {
            System.out.println("error");
        }
    }
}

the above code only ECHOs %TestVar%, rather than the string that was set to TestVar, almost as if it just isn't being parsed as a variable at all.

For a C# example,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisibleCMD
{
    class Program
    {
        static void Main(string[] args)
        { 
            System.Diagnostics.Process.Start("CMD.exe", 
                "/K set /p TestVar=Give me a string to save! && ECHO %TestVar%");
        }
    }
}

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 3
    Why do you want to do this (https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? 99% of the time when people are launching `cmd.exe` they are doing it wrong (i.e. they shouldn't be launching `cmd.exe`). They should launch the process they want to launch. – mjwills Oct 08 '20 at 22:37
  • I don't wrap a variable in `%` when I set it – Entranced Optics Oct 08 '20 at 22:41
  • How would I go about referencing a created variable if not using `&&` if that's the problem? – Entranced Optics Oct 08 '20 at 22:51
  • 2
    Whilst the issues with your `cmd.exe` string has been answered in the answer area, there's still a major issue not yet mentioned. The input string passed and assigned to the variable named `TestVar` is only available within the `cmd.exe` instance you launched. Whilst it's clear that you have used the `/K` option to keep that `cmd.exe` window open, that variable is of little use to you unless you keep adding more commands separated with ampersands. It would assist us therefore if you were to explain what reason you have for creating a variable like this, and for what purpose. – Compo Oct 09 '20 at 00:44

1 Answers1

2

The problem is with your command line, not with the language you're using. You have to enable delayed expansion if you want to access the variable's value on the same line as it's set. Passing the /V switch will enable delayed expansion of the variable, and using ! instead of % will get the current value of the variable:

Process.Start("CMD.exe", 
    "/V /K set /p TestVar=Give me a string to save! && ECHO !TestVar!");
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 1
    you also need to escape the `!` in `save!` – phuclv Oct 09 '20 at 00:11
  • @phuclv I didn't need to on my machine (using c#) – Rufus L Oct 09 '20 at 00:14
  • 1
    In this particular situation escaping the `!` in `save!` like `save^^!` is not necessary, because `&&` is recognised earlier than `!`, hence expansion of `!TestVar!` is not impacted here, and the `!` in `save!` is not removed as the command line is executed in Command Prompt context (in batch file context unpaired `!` become removed). However, I still would escape the extra `!`, because it might cause trouble when altering the command line in future, particularly when further `!` become added… – aschipfl Oct 09 '20 at 08:28