1

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

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

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

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

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
4

1 回答 1

5

因为fso.GetFile()不会自动展开%COMPUTERNAME%,修改sFinalDestinationPathsOutputFilename这样使用:

sOutputFilename = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
sFinalDestinationPath = "E:\CopyTestFolder\" & sOutputFilename & "\"
于 2012-07-23T23:09:32.043 回答