Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 为什么此VBScript在尝试创建文件时会引发错误?_Windows_File Io_Vbscript_Filesystemobject - Fatal编程技术网

Windows 为什么此VBScript在尝试创建文件时会引发错误?

Windows 为什么此VBScript在尝试创建文件时会引发错误?,windows,file-io,vbscript,filesystemobject,Windows,File Io,Vbscript,Filesystemobject,我找到了这个脚本,似乎无法理解为什么它在尝试创建文件C:\IPSecWeights.xls时会抛出错误 到目前为止,我遇到的问题是: Set objWorkbook = objExcel.Workbooks.Open(FileLoc) 我得到一个错误,文件找不到。如何重写代码来修复此问题 Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Dim objFSO,objFile Dim arrLines Dim str

我找到了这个脚本,似乎无法理解为什么它在尝试创建文件
C:\IPSecWeights.xls
时会抛出错误

到目前为止,我遇到的问题是:

Set objWorkbook = objExcel.Workbooks.Open(FileLoc)
我得到一个错误,文件找不到。如何重写代码来修复此问题

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Dim objFSO,objFile
Dim arrLines
Dim strLine
Dim objExcel,objWorkbook
Dim FileLoc
Dim intRow
Dim objDictionary

FileLoc = "C:\IPSecWeights.xls"

Sub ExcelHeaders()
    Set objRange = objExcel.Range("A1","G1")
    objRange.Font.Size = 12
    objRange.Interior.ColorIndex=15

    objexcel.cells(1,1)="Filter Name"
    objexcel.cells(1,2)="Source"
    objexcel.cells(1,3)="Destination"
    objexcel.cells(1,4)="Source Port"
    objexcel.cells(1,5)="Destination Port"
    objexcel.cells(1,6)="Protocol"
    objexcel.cells(1,7)="Direction"
End Sub

Function RegExFind(strText,strPattern)
    Dim regEx
    Dim match, Matches
    Dim arrMatches
    Dim i : i = 0
    Set regEx = New RegExp
    regEx.IgnoreCase = True
    regEx.Global = True
    regEx.Pattern = strPattern

    Set matches = regEx.Execute(strText)
    ReDim arrMatches(Matches.Count)
    For Each match In Matches
        For Each SubMatch In match.Submatches
            arrMatches(i) = Submatch
            i = i + 1
        Next
    Next
    RegExFind = arrMatches
End Function


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(WScript.Arguments(0),ForReading)

Set objExcel = CreateObject("excel.application")
Set objWorkbook = objExcel.Workbooks.Open(FileLoc)

objExcel.Visible = True

ExcelHeaders ' Create Excel Headers

rePolicy = "Policy Name\s+:\s(.+)"
reSRCAddr = "Source Address\s+:\s(.+)"
reDSTAddr = "Destination Address\s+:\s(.+)"
reProtocol = "Protocol\s+:\s(.+)"
reSRCPort = "Source Port\s+:\s(.+)"
reDSTPort = "Destination Port\s+:\s(.+)"
reDirection = "Direction\s+:\s(.+)"

strText = objFile.ReadAll
objFile.Close

Dim arrPolicy, arrSRCAddr, arrDSTAddr, arrProtocol, arrSRCPort, arrDSTPort, arrDirection

arrPolicy = RegExFind(strText, rePolicy)
arrSRCAddr = RegExFind(strText, reSRCAddr)
arrDSTAddr = RegExFind(strText, reDSTAddr)
arrProtocol = RegExFind(strText, reProtocol)
arrSRCPort = RegExFind(strText, reSRCPort)
arrDSTPort = RegExFind(strText, reDSTPort)
arrDirection = RegExFind(strText, reDirection)

intRow = 2

For i = 0 To UBound(arrPolicy)
    objExcel.Cells(introw,1) = arrPolicy(i)
    objExcel.Cells(introw,2) = arrSRCAddr(i)
    objExcel.Cells(introw,3) = arrDSTAddr(i)
    objExcel.Cells(introw,4) = arrSRCPort(i)
    objExcel.Cells(introw,5) = arrDSTPort(i)
    objExcel.Cells(introw,6) = arrProtocol(i)
    objExcel.Cells(introw,7) = arrDirection(i)

    intRow = intRow + 1
Next

objFile.Close
objWorkbook.save
'objExcel.Quit
我怀疑“找不到文件”是一个有点误导性的错误消息。也就是说,您的实际问题是文件系统权限,但VBScript顽固地将此报告为“找不到文件”,而不是更合理的错误

该脚本可能在早期版本的Windows中运行良好,但您现在正在安装Vista或更高版本的计算机上尝试它,这将引入一系列附加的安全功能和写保护。不允许应用程序或脚本写入硬盘的根级别

当然,这确实不应该是个问题,因为合法的应用程序不需要篡改硬盘驱动器根级别或共享系统文件夹(如
C:\Windows
)中的文件,但在测试过程中偶尔会出现这种情况

将脚本中的文件路径更改为保证具有读/写访问权限的文件路径,例如“我的文档”文件夹。对于部署,无论如何都不应该硬编码文件系统路径。使用
文件系统对象的
来检索路径。

我怀疑“未找到文件”是一条有点误导性的错误消息。也就是说,您的实际问题是文件系统权限,但VBScript顽固地将此报告为“未找到文件”,而不是一个更合理的错误

该脚本可能在早期版本的Windows中运行良好,但您现在正在安装Vista或更高版本的计算机上尝试它,这将引入一系列附加的安全功能和写保护。不允许应用程序或脚本写入硬盘的根级别

当然,这确实不应该是个问题,因为合法的应用程序不需要篡改硬盘驱动器根级别或共享系统文件夹(如
C:\Windows
)中的文件,但在测试过程中偶尔会出现这种情况


将脚本中的文件路径更改为保证具有读/写访问权限的文件路径,例如“我的文档”文件夹。对于部署,无论如何都不应该硬编码文件系统路径。而是使用
文件系统对象的
来检索路径。

“找不到文件”-似乎很简单。检查路径。文件不存在,文件名不正确,或者它的权限不正确。您从哪里运行脚本?在哪个用户帐户下?什么版本的Windows?正如米奇所说,这似乎很清楚,“文件找不到”-似乎很简单。检查路径。文件不存在,文件名不正确,或者它的权限不正确。您从哪里运行脚本?在哪个用户帐户下?什么版本的Windows?正如Mitch所说,这似乎很清楚。好吧,奇怪的是,当我将FileLoc=“C:\IPSecWeights.xls”更改为FileLoc=“C:\IPSecWeights.txt”时,脚本会运行,但显然有问题。thanks@KenWhite@MitchWheat@Codygray为什么它只是在.xls文件格式上有问题?有什么想法吗?由于某种原因,当我从实际文件夹创建文件时,它无法工作,我必须打开excel然后保存文件,谢谢你的帮助。奇怪的是,当我将FileLoc=“C:\IPSecWeights.xls”更改为FileLoc=“C:\IPSecWeights.txt”时,脚本运行,但显然有问题。thanks@KenWhite@MitchWheat@Codygray为什么它只是在.xls文件格式上有问题?有什么想法吗?由于某种原因,当我从实际文件夹创建文件时,它不起作用,我必须打开excel然后保存文件,谢谢你的帮助