1

I'm looking to call another CMD instance and pass data to it from the parent instance.

I'm aware of the general route of using variables, but it would not be feasible in this case as endlocal and bridging would be too much of an inconvenience (would have to relay tens of variables). I also know i could use temporary files to port all the data i want easily but this would leave unwanted footprints. EDIT: setx can also be used quite succesfully.

I'm looking specifically for something along the lines of common use of arguments, like:

test.bat -setvar"variable"="value" -setvar...

I did previously find one way with its limitations

Found out on other thread that i can add a command to the end of start cmd

start cmd /c call "test.bat" ^& PAUSE

Of course we can reverse this

start cmd /c call PAUSE ^& TIMEOUT 5 ^& PAUSE ^& test.bat

The commands can't have "" and, i suspect, any special characters. this presents an issue when trying to port complex variables.

Speaking of special characters:

start cmd /c call (PAUSE) ^& (TIMEOUT 5) ^& (REM !"#¤%&/=?`´^*"-.,_:;\*~¨) ^& test.bat

This will not work at all. I might be able to escape interpretation but in the case of some variables i might not have the luxury of knowing enough of the values to be able to process it into anything i could handle. -- Actually it seems parentheses aren't tolerated.

One of the main things i need to do is pass on volume names, which is easily achievable, but this isn't enough.

start cmd /c call echo \\?\Volume{6537febd-01bc-11d6-adb5-806e6f6e6963}\ ^& TIMEOUT 5 ^& PAUSE ^& test.bat

This may already be covered elsewhere and if so the issue is me not finding the right keywords or parallels to the relevant threads.

5
  • 1
    And there's a reason you cannot use setx command which creates a global variable in which you can store your variable so you can read it back from the other routine?
    – LPChip
    May 31, 2015 at 3:22
  • Good point, though as i recall that would require administrative privileges (writing to HKLM right?), which CMD might not have. A possible solution for another script, yes, as are the options i mentioned earlier, but not what i'm looking for.
    – Alex Oja
    May 31, 2015 at 13:09
  • 1
    No, a user can always write to HK_CU which is where setx "name" "variable" will write to, thus you don't need to run cmd with administrative privileges, unless you specify /M, then it writes to HK_LM and then you need administrative privileges. But for what you want, that is not necessary. To clear a previously assigned variable do setx "name" "" Also note that setx variables do not become operational in the current session, only in next sessions. Use set for the current session.
    – LPChip
    May 31, 2015 at 13:16
  • Clearly i had misunderstood the function of setx, back to ss64 then. Thank you for clearing up this one!
    – Alex Oja
    May 31, 2015 at 13:52
  • 1
    Does this mean you no longer need help? If so I'll happily write you a short answer to accept the question and let everyone know you're set. :)
    – LPChip
    May 31, 2015 at 14:03

2 Answers 2

2

A child cmd instance inherits all environment variables from the parent one. Proof: let's have two batch scripts created:

Test1.bat

@setlocal
@set _batch
@set "_batchParentName=%~nx0"
@set "_batchParentPath=%~dp0"
@set "_batchParentSpec=< LSS > GTR & amper | pipe"
start "%~n0" cmd /C call "test.bat" "%_batchParentSpec%" %* ^& pause
@set _batch

Test.bat

@setlocal
@set _batch
@echo %~n0: %%* = %*

Parent cmd instance output:

d:\bat>test1.bat first "1<2" third
Environment variable _batch not defined

d:\bat>start "test1" cmd /C call "test.bat" "< LSS > GTR & amper | pipe" first "1<2" third
 & pause
_batchParentName=test1.bat
_batchParentPath=D:\bat\
_batchParentSpec=< LSS > GTR & amper | pipe

d:\bat>

Child cmd instance output from test.bat:

_batchParentName=test1.bat
_batchParentPath=D:\bat\
_batchParentSpec=< LSS > GTR & amper | pipe
test: %* = "< LSS > GTR & amper | pipe" first "1<2" third
Press any key to continue . . .

Note right start command syntax with "title":

START "title" [/D path] [options] "command" [parameters]

Always include a TITLE this can be a simple string like "My Script" or just a pair of empty quotes "". According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

Ensure the start command does not contain the /I option switch (from start /?):

 /I      The new environment will be the original environment passed
         to the cmd.exe and not the current environment.

To eliminate another eventuality of the child environment changes, include the /D switch to cmd as follows:

start "test1" cmd /D /C call "test.bat"

As per cmd /?:

/D      Disable execution of AutoRun commands from registry
1

You can use setx to set an environmental variable that is global to your windows.

To use setx simply write this: setx "name" "variable" where name is a simple name, and variable is a variable that you want to set.

See setx /? for all the options and parameters in case you want to go a bit deeper into the options.

Do note though, setx will store variables directly to the current user's hive of the registry. This means that you do not need any administrative privileges, but it also means that the variables are not directly available in the current cmd session. Use the regular set if you just want to store variables locally.

You can of couse also store a local variable into the global one as shown in the following example:

@echo off
:: store "test" in variable "a"
set a=test

:: make this variable global
setx a %a%

As you can see in the above example, the %a% refers to the variable in the local scope, and setx makes it availble publicly

To unset a global variable, call this command: setx "name" "" for example: setx a ""

Also good to realise: you cannot modify one instance of cmd by using setx in another. As soon as you launch a cmd window, only set can change its variables inside, and setx can store them outside. (upon exit as you will)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .