Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: hostname in batch file  (Read 48986 times)

0 Members and 1 Guest are viewing this topic.

037

  • Guest
hostname in batch file
« 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.

dnthns87



    Rookie

    Thanked: 2
    Re: hostname in batch file
    « Reply #1 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

    037

    • Guest
    Re: hostname in batch file
    « Reply #2 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...

    ghostdog74



      Specialist

      Thanked: 27
      Re: hostname in batch file
      « Reply #3 on: March 31, 2010, 08:07:48 AM »
      on your command prompt, type
      Code: [Select]
      env. You can see computer name there.

      037

      • Guest
      Re: hostname in batch file
      « Reply #4 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...

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: hostname in batch file
      « Reply #5 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.
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      gregflowers



        Rookie

        Thanked: 3
        Re: hostname in batch file
        « Reply #6 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


        037

        • Guest
        Re: hostname in batch file
        « Reply #7 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...

        BobJordan



          Greenhorn

          Thanked: 1
          Re: hostname in batch file
          « Reply #8 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>
          Hello

          ghostdog74



            Specialist

            Thanked: 27
            Re: hostname in batch file
            « Reply #9 on: April 04, 2010, 06:10:09 PM »
            another greg

            Helpmeh



              Guru

            • Roar.
            • Thanked: 123
              • Yes
              • Yes
            • Computer: Specs
            • Experience: Familiar
            • OS: Windows 8
            Re: hostname in batch file
            « Reply #10 on: April 04, 2010, 06:31:06 PM »
            another greg
            I already sent a PM to Nathan... >:(
            Where's MagicSpeed?
            Quote from: 'matt'
            He's playing a game called IRL. Great graphics, *censored* gameplay.

            BobJordan



              Greenhorn

              Thanked: 1
              Re: hostname in batch file
              « Reply #11 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.
              Hello

              Helpmeh



                Guru

              • Roar.
              • Thanked: 123
                • Yes
                • Yes
              • Computer: Specs
              • Experience: Familiar
              • OS: Windows 8
              Re: hostname in batch file
              « Reply #12 on: April 04, 2010, 09:05:11 PM »
              I use XP pro SP3 and there is no env command.
              Here.
              Where's MagicSpeed?
              Quote from: 'matt'
              He's playing a game called IRL. Great graphics, *censored* gameplay.