VBScript环境变量%COMPUTERNAME% [英] VBScript Environment Variable %COMPUTERNAME%

查看:360
本文介绍了VBScript环境变量%COMPUTERNAME%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找环境变量%COMPUTERNAME%的一些帮助。它在我的脚本中作为在本地主机名后面命名文件的方法。在另一点上,我正在使用脚本通过网络(到映射的驱动器)在不同的目录中构建一个文件夹,我需要将文件夹命名为原始计算机的本地主机名。这可能没有意义,但是我将在下面提供一个例子:

  Comp1 = BobPC 
Comp2 = JakePC

我在BobPC上制作一个zip文件,然后将其复制到JakePC,但是我需要这个文件被复制到像C:\CopiedFiles\BobPc这样的目录,因为这个脚本将在许多计算机上运行,​​并且每个文件都需要一个文件夹,在这个文件夹中的文件将被命名。 >

我希望这是有道理的。



现在我可以使它构建一个文件夹,但它的名称显然是JakePC,因为我正在使用环境变量来抓取本地主机名



我的问题基本上是如何告诉它在原始计算机之后命名文件夹?



<如果你有任何问题让我知道,我会很乐意解释,因为我知道我可能没有多大意义。

解决方案

如果您从源计算机运行脚本,可以使用以下

  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需要为运行脚本的用户写入权限的共享
oFso.CopyFile源,目标,覆盖
'检查错误和清理您的对象

如果您从targetpc运行它,您应该使用远程脚本,但不会很聪明因为你必须知道你需要运行它的pc的名称,所以不需要环境变量。


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.

解决方案

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.

这篇关于VBScript环境变量%COMPUTERNAME%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆