3

I'm looking for some help with the environment variable %COMPUTERNAME%. It is working in my script as a means to name a file after the local host name. At another point I am using the script to build a folder in a different directory over the network (to a mapped drive) and I need to name the folder the local host name of the original computer. This may not make sense but I will provide an example below:

Comp1 = BobPC
Comp2 = JakePC

I am making a zip file on BobPC, which is then being copied over to JakePC, but I need this file to be copied into a directory like... C:\CopiedFiles\BobPc because this script will be run on many computers and each of them needs a folder where the files will be located named after the computer it came from.

I hope this makes sense.

Right now I can make it build a folder just fine but it names it the name of "JakePC" obviously because I am using the environment variables to grab the local host name.

My question basically is how can I go about telling it to name the folder after the the original computer?

If you have any questions let me know, I will be more than happy to explain because I know that I might not be making much sense.

Smandoli
  • 6,919
  • 3
  • 49
  • 83
parchambeau
  • 1,141
  • 9
  • 34
  • 56
  • Can you be more specific about your question? Like: how to create a folder, or how to work on remote PC, or how to get the computer name. – Jay Jul 25 '12 at 19:37
  • The script is being run on 1 machine, and it is producing a folder on another, but I need the folder to be named the host name of the machine that the script is being run from. – parchambeau Jul 25 '12 at 23:12
  • You can get the host name from `%COMPUTERNAME%` environment variable. That variable will be always the local host name where the source files are, and since scripts will always run locally. If the script is run on "JakePC", then it will produce a folder named "JakePC" on remote computer. Just like what you already state in the post. Or are your trying to name the folder using the *remote* host name? – Jay Jul 25 '12 at 23:35
  • Yea, I can get it to build the folder just fine. What I am doing is having it build the folder with the host name on the local machine, over onto the remote machine before I zip or copy anything. My problem comes when I try to copy over the file that I zipped locally it will not recognize the path using the computername environment variable in the path. So it just doesnt copy correctly – parchambeau Jul 26 '12 at 03:42
  • Is the remote PC already have the required path already present? e.g.: `Z:\CopiedFiles\BobPC` assuming `Z:` is mapped to `\\JakePC\DriveC`. If that path isn't already present, you'll have to create it first. One subfolder at a time if necessary. Otherwise, you'll get an error. – Jay Jul 27 '12 at 03:30
  • I managed to get it to work, it was honestly just silly mistakes on mny part not having parens and tick marks in correct places. Overlooked them. – parchambeau Jul 27 '12 at 14:24
  • Possible duplicate of [Can I pick up environment variables in vbscript WSH script?](http://stackoverflow.com/questions/904739/can-i-pick-up-environment-variables-in-vbscript-wsh-script) – user692942 Mar 05 '17 at 09:44

2 Answers2

6

In case you run the script from the source pc you can use the following

dim oFso, oShell, oShellEnv, computerName, target, source
const overwrite = true
set oFso      = CreateObject("Scripting.FileSystemObject")
set oShell    = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
computerName  = oShellEnv("ComputerName")
source =  "c:\test\yourZip.zip"
target = "\\JakePC\copiedFiles\" & computerName & ".zip"
'copiedFiles needs to be a share with write permission for the user who runs the script
oFso.CopyFile source, target, overwrite
'do check on errors and the cleanup of your objects

in case you run it from the targetpc you should use remote scripting but that would not be smart since you must know the name of the pc where you need to run it so there is no need for the environmentvariable.

peter
  • 41,770
  • 5
  • 64
  • 108
  • "oFso.CopyFile(source, target, overwrite)" - syntax error 1044, 'Cannot use parentheses when calling a Sub' – Ekkehard.Horner Jul 27 '12 at 13:34
  • couldn't test this one, in that case just remove the parentheses or put a call before it, i'll adapt the answer, thanks Ekkehard – peter Jul 27 '12 at 14:05
  • Thank you for the response. I have managed to get the script working. It was more of just a "duh" error on my part regarding parens / tick marks in wrong spots. Rather annoying thinking I had something fundamentally wrong. – parchambeau Jul 27 '12 at 14:24
  • Peter, is there anyway I could message you a copy of this script and you could give it a quick lookover? This is my first script so I'm trying to get as much learning experience out of it as I can and a good critique of it would be very helpful. – parchambeau Jul 27 '12 at 14:33
  • sure but why not publish it here ? i'm not the only vbscript uer who can help you. edit your answer or if too big put it on github and put a link here – peter Jul 27 '12 at 22:03
2

I've found 2 snippets to get the hostname. Both runs ok in windows 7 sp1 and windows server 2012:

'
' How to get the hostname
' 
' References
'
' Method1: http://www.robvanderwoude.com/vbstech_network_names_hostname.php
' method2: https://msdn.microsoft.com/en-us/library/s6wt333f(v=vs.84).aspx

WScript.Echo "Method 1 "

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strRegValue = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname"
strHostName = wshShell.RegRead( strRegValue )
WScript.Echo "Host Name: " & strHostName

WScript.Echo "Method 2  (include other network values)"

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "Computer Name = " & WshNetwork.ComputerName
WScript.Echo "User Name = " & WshNetwork.UserName

The difference is WshNetwork.ComputerName method retrieve the hostname in upper case.

gwarah
  • 241
  • 2
  • 10