VBScript环境变量

时间:2012-07-23 21:41:51

标签: vbscript environment-variables

我有一个关于如何修复运行脚本时遇到的错误的问题。我很确定它与我使用%COMPUTERNAME%环境变量的方式有关。

我的脚本做的是在本地压缩一些文件,然后使用robocopy将它们复制到已安装或共享的驱动器,然后检查文件大小是否相同,如果是,则删除文件原装电脑。如果进程中的任何步骤产生错误,它将退出脚本。

现在,如果我没有将“%COMPUTERNAME%”添加到最终目标路径,则脚本可以正常运行。 (压缩文件最终将在哪里)我需要将压缩文件放入他们自己的文件夹中,并使用它所源自的主机的名称,因为这个脚本将在许多不同的机器上运行,所有这些机器都将运行到同一位置。 / p>

所以基本上它需要看起来像这样:

E:\ LocalHostName \ TestZip.zip

现在,当复制压缩文件时,脚本会很好地构建文件夹,一旦文件大小检查开始就会出现问题。我收到“FileToBeCompared2”行的“找不到文件”的错误。我理解为什么会产生错误,因为它没有识别%COMPUTERNAME%环境变量,但我不知道如何解决这个问题。

我还将尝试添加一些功能,如果发生错误,则会在输出文件夹中生成类似“脚本期间发生错误”的文本文件。

提前感谢您的帮助。脚本如下:

'-------------------------------------------------------------------------------------------
'This script is used to zip files locally, copy them to a new location, verify that the
'files were copied correctly, and then delete the files from the original source.
'In it's current state it is being used as a means to zip event files and move them
'to a central location.

'Run with administrator priveleges.

'-----------------------------------------------------------------------------------------------------
Option Explicit

Dim sDirectoryPath, sLocalDestinationPath, sFinalDestinationPath, sOutputFilename, Shell, sFileExt, sFilePrefix

Set Shell = WScript.CreateObject("WScript.Shell")

'Specify Directory Path where files to be zipped are located
'Specify local destination for zipped files
'Specify final destination path for zippped files
'Specify file extension name to look for
'Specify prefix of filename to look for

sDirectoryPath = "C:\Testscripts\"
sLocalDestinationPath = "C:\ScriptOutput\"
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\" 
sFileExt = ".evtx"
sFilePrefix = "Archive*"
sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%") 'Environment variables needed for grabbing hostname

Dim ZipCommand, RobocopyCommand, RunCommand, filesys, filetext
Dim d : d = Date() 
Dim dateStr : dateStr = Year(d) & "-" & Right("00" & Month(d), 2) & "-" & Right("00" &     Day(d), 2) 'Date String
Dim t : t = Time()
Dim timeStr: timeStr = Hour(t) & "-" & Right("00" & Minute(t), 2) & "-" & Right("00" & Second(t), 2) 'Time String
Dim FullFileName

FullFileName = sOutputFilename & "-" & dateStr & "-" & timeStr & ".zip "

'Following command runs 7-zip and grabs the files to be zipped from your set sDirectoryPath, zips them into set sLocalDestinationPath
'and names the file with the localhost name and date/time

ZipCommand = """C:\Program Files\7-zip\7z.exe"" a " & sLocalDestinationPath & FullFileName & sDirectoryPath & sFilePrefix & sFileExt

RunCommand = Shell.Run(ZipCommand,0,true)

if err.Number <> 0 then
    WScript.Echo "An error has occurred during the zip process, re-run Script."     WScript.Quit
end if

Wscript.Sleep 2000

'The following command creates a folder named after the host computer where the files are being copied from 

Dim newfolder, newfolderpath, filesys2

newfolderpath = "E:\CopyTestFolder\" & sOutputFilename & "\"
set filesys2 = CreateObject("Scripting.FileSystemObject")
If Not filesys2.FolderExists(newfolderpath) Then
    Set newfolder = filesys2.CreateFolder(newfolderpath)
End If

'Following command runs Robocopy from command line, moves files from your set sLocalDestinationPath to your set sFinalDestinationPath       

WScript.Echo "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath  
RobocopyCommand = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath 
RunCommand = Shell.Run(RobocopyCommand,0,true)

if err.Number <> 0 then
    WScript.Echo "An error has occured copying the files, re-run Script."
    WScript.Quit
end if

Dim fso, FileToBeCompared1, FileToBeCompared2

Set fso = CreateObject("Scripting.FileSystemObject")

'Setting the Local file to be compared
Set FileToBeCompared1 = fso.GetFile(sLocalDestinationPath & FullFileName) 
WScript.echo sFinalDestinationPath & FullFileName

'Setting the file copied to final destination to be compared
Set FileToBeCompared2 = fso.GetFile(sFinalDestinationPath & FullFileName)       

If FileToBeCompared1.size = FileToBeCompared2.size then
    fso.DeleteFile("C:\Testscripts\Archive*.evtx") 'This will be the path where events are being Archived to. (Non restricted path)
    fso.DeleteFolder("C:\ScriptOutput") 'This deletes the archive folder that 7-zip builds each time this script is run
else
    WScript.Echo "File sizes do not match, File was not fully copied, Re run script."   
    WScript.Quit
end if

2 个答案:

答案 0 :(得分:4)

由于fso.GetFile()不会自动展开%COMPUTERNAME%,因此请修改sFinalDestinationPath以使用sOutputFilename,如下所示:

sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\"

答案 1 :(得分:0)

您在为sOutputFilename(第24行)指定值之前引用了该值(第27行)。

24:sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\"

...

27:sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%") 'Environment variables needed for grabbing hostname

所以向下移动24或向上移动27。