0

I have a 2 line batch program. Very new to writing bat files.

This is my code,

C:\Users\X1231\AppData\Local\Continuum\Anaconda3\python.exe "\\ABCDE\A1\Python Testing Environment\Python Codes\Updated.py"

cmd/k

I want to pass X1231 as a variable inside that dynamically. If I open my cmd prompt and type in that echo %username%, it gives me the value that I have to pass it in.

For example, I want something like this -

set MyID = %username%

C:\Users\MyID\AppData\Local\Continuum\Anaconda3\python.exe "\\ABCDE\A1\Python Testing Environment\Python Codes\Updated.py"

cmd/k

But it is not working for me. Please help.

I tried on the command prompt -

cmd /k

  • 2
    Variables are referenced with surrounding percent signs - as you already did with `%username%`. And there are more useful predefined variables. So: `"%LocalAppData%\Continuum\Anaconda3\phyton.exe"` should do. – Stephan Apr 04 '19 at 11:59
  • 2
    and though @Stephan gave you what you needed, one issue in your code is the spaces surrounding the `=` in set. it should be `set "MyID=%username%"` else it will create a variable called `%MyID %` – Gerhard Apr 04 '19 at 12:06
  • 2
    What do you intend with the trailing `cmd /k`? This will open a **new** instance of cmd.exe (inheriting the envrionment) in the same window and keep it open. –  Apr 04 '19 at 12:18
  • Because the above line executes a script and once that finishes I want to keep the command prompt open. But, if I don't have that final line then it closes it. – Student of the Digital World Apr 04 '19 at 12:49
  • 1
    You should probably run your `python` command as an argument to `cmd.exe` then. Enter `cmd /?` at the Command Prompt for its usage information. – Compo Apr 04 '19 at 13:02
  • I recommend reading [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) and Wikipedia article about [Windows Environment Variables](https://en.wikipedia.org/wiki/Environment_variable#Windows). And don't use `cmd/k` which `cmd.exe` must correct to `cmd /k` because of command line syntax is __command__ or __file name__ SPACE __ArgumentOne__ SPACE __"argument two"__ and so on. There should be in general always a space between command `cmd` and its argument `/K` as shown on running in a cmd window `cmd /?`. – Mofi Apr 07 '19 at 16:54

0 Answers0