Computer Hope

Microsoft => Microsoft DOS => Topic started by: 037 on March 31, 2010, 07:06:40 AM

Title: hostname in batch file
Post by: 037 on March 31, 2010, 07:06:40 AM
how can i get the hostname from a workstation and make a folder named the same as the workstation? i have tried "setlocal, set hostname=%hostname%, endlocal".

or in other words, "hostname" is a command. how can i make a file from the output of a command?

xpp sp3.

i am trying to backup multiple folders to one folder with the name of the workstation it came from.
Title: Re: hostname in batch file
Post by: dnthns87 on March 31, 2010, 07:31:41 AM
Im assuming you mean appending to a file, when you say "make a file from the output of a command"

> Clears the file
>> adds a line to a file

example:
echo hello>>myfile.txt
Title: Re: hostname in batch file
Post by: 037 on March 31, 2010, 07:55:16 AM
i am trying to go to a workstation and run my batchfile. it needs to find the computer name and make a file with the name of the workstation. then it will backup some folders and save them under the folder that it just created with the computer name so i can find it later to restore them. i have the batch file created and it can store folders under the %username% variable, but i would much rather have it store under computer name.

example: when i type "echo %username%" in a cmd it displays my user name. but when i type "echo %hostname%" it does not display. i can however type "hostname" by itself to get the workstation name....

thanks in advance...
Title: Re: hostname in batch file
Post by: ghostdog74 on March 31, 2010, 08:07:48 AM
on your command prompt, type
Code: [Select]
env. You can see computer name there.
Title: Re: hostname in batch file
Post by: 037 on March 31, 2010, 09:09:54 AM
env does not work. isnt that for linux? i need to create a folder with the name of the pc...
Title: Re: hostname in batch file
Post by: Sidewinder on March 31, 2010, 09:56:32 AM
ComputerName is a system variable that you can use:

md %computername%

OR

You can still use the hostname command in a batch file:

Code: [Select]
@echo off
for /f %%a in ('hostname') do (
  md %%a


Good luck.  8)

Env works on WinXP but you can also use set to list all the environment variables.
Title: Re: hostname in batch file
Post by: gregflowers on March 31, 2010, 09:59:02 AM
@echo off
md c:\%computername%

or if you want to name a file:

@echo off
echo. >> c:\%computername%.txt

Title: Re: hostname in batch file
Post by: 037 on March 31, 2010, 11:32:26 AM
thank you both, i ran the set comand to see the variables but "computername" was not in there. but it does work with computername so thanks again...
Title: Re: hostname in batch file
Post by: BobJordan on April 04, 2010, 05:47:34 PM

"Hostname" is a command. How can I make a file from the output of a command?



C:\>type  thirty7.bat
Code: [Select]
@echo off

hostname  >  host.txt

type host.txt

pause
md Okie
cd Okie

echo hello okie > host.txt

type host.txt

Output:

C:\>thirty7.bat
Okie
Press any key to continue . . .
hello okie

C:\Okie>
Title: Re: hostname in batch file
Post by: ghostdog74 on April 04, 2010, 06:10:09 PM
another greg
Title: Re: hostname in batch file
Post by: Helpmeh on April 04, 2010, 06:31:06 PM
another greg
I already sent a PM to Nathan... >:(
Title: Re: hostname in batch file
Post by: BobJordan on April 04, 2010, 08:55:11 PM
on your command prompt, type
Code: [Select]
env. You can see computer name there.

I use XP pro SP3 and there is no env command.
Title: Re: hostname in batch file
Post by: Helpmeh on April 04, 2010, 09:05:11 PM
I use XP pro SP3 and there is no env command.
Here.