gpt4 book ai didi

VBScript 环境变量

转载 作者:行者123 更新时间:2023-12-02 01:15:44 30 4
gpt4 key购买 nike

我有一个关于如何修复运行脚本时看到的错误的问题。我很确定这与我使用 %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

最佳答案

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

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

关于VBScript 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620970/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com