Search code examples
cmdhostnamempiexec

How do I include a variable in cmd command?


For example, I want the process "myexe.exe" to run on any host computer. So i want hostname to be evaluated on each computer and inserted into the command. Ideally i want this all done in one line. I tried the following code.

mpiexec -n 8 -hosts 1 %hostname% myexe.exe myinputfile.txt

But i got this error

... unable to get host address for %hostname% (11001)

So it doesn't appear to have evaluated hostname properly. How do i get hostname to be evaluated properly in the command?


Solution

  • You could use localhost instead.

    But, on Windows (implied by the cmd tag) you probably should use:

    mpiexec -n 8 -hosts 1 %COMPUTERNAME% myexe.exe myinputfile.txt
    

    The magic for setting a variable from the output of hostname is:

    FOR /F %A IN ('hostname') DO (SET "V=%~A")
    

    In a .bat file script, double the PERCENT SIGN character.

    FOR /F %%A IN ('hostname') DO (SET "V=%%~A")