user3734639
user3734639

Reputation: 5

how to set the output of a command to a variable in batch file

im having difficulties trying to set the output of the hostname command as a variable. what I want to do is to have the text file that is outputted to have the name of the computer so computer1.txt, computer2.txt etc, but i want to do it without making a temp file for example

    set HNAME =`hostname` 

this is what i have currently but the script i am using is being run on several computers at the same and i believe that the temp file that i create is causing issues with the names of the .txt files.

    hostname >> hostname.txt


    set /p HNAME=<hostname.txt 

    pause

    echo hello > %HNAME%.txt
    pause

Upvotes: 0

Views: 2299

Answers (1)

Rob K
Rob K

Reputation: 8926

You have to use the for command, something like this:

for /f "usebackq" %i in ( `somecommand` ) do set envar=%i

It's very painful. for /? at the command line for more information.

Upvotes: 1

Related Questions