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.

7
  • 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. Commented Jul 25, 2012 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. Commented Jul 25, 2012 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? Commented Jul 25, 2012 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 Commented Jul 26, 2012 at 3: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. Commented Jul 27, 2012 at 3:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

"oFso.CopyFile(source, target, overwrite)" - syntax error 1044, 'Cannot use parentheses when calling a Sub'
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
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.
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.
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
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.