# save text file as a variable



## JimZ (Jun 27, 2003)

Is there anyway to save the output of a DOS command to a variable file name. Such as %hostname%.txt Anyone know of anything possible?


----------



## JohnWill (Oct 19, 2002)

dir >c:\hostname.txt

The above command saves the directory listing the that name in the root of C: Most other commands will accept the same redirection character.

Redirection is dependent on exactly what command you're running, can you be more specific?


----------



## JimZ (Jun 27, 2003)

I want to have the file name be a variable. Such as dir > C:\_hostname_.txt I need to have a machine unique filename. I have a script that saves a log file, I need to run this script locally on 30 machines on a regular basis. Right now I think it will be easier to have a single script that saves the log to a single location. The unique file name will help me sort which machine each log came from.


----------



## Squashman (Apr 4, 2003)

Environmental Variables. Type *set* at a cmd prompt. You can use those variables in a batch file.
%COMPUTERNAME%


----------



## JohnWill (Oct 19, 2002)

What I would do if the bare name is in the file in question is the following.

Create a file named SET.TXT with the text "SET=MYVAR " (without the quotes) and save it.

COPY SET.TXT+C:\HOSTNAME.TXT SETCMD.BAT
CALL SETCMD

This will set the environment variable MYVAR to the file name in question.


----------

