Windows Explorer 显示文件时出现 VBScript OpenTextFile File Not Found 错误

VBScript OpenTextFile File Not Found error when Windows Explorer shows the file

我正在用 VBScript 编写程序来自动执行文件加密过程,但遇到了一个问题。

我想根据文件比较 returns 错误级别是 0 还是 1 来测试脚本将执行的代码。(为简单起见,我从 post 中删除了该代码。 ) Google 搜索已将我指向以下内容,以开始为此目的修改其中一个比较文件的过程。

Set testFile = fso.OpenTextFile(testDestFile, 8, False, 0)

但是,VBScript 总是会对该行抛出 "File not found" 错误,除非我输入

WScript.Echo "testDestFile is '" & testDestFile & "'..."

就在它之前。

我不想这样,因为除非必要,否则脚本的操作应该对用户不可见。当我 运行 这个脚本时,我可以在 Windows Explorer 中看到它创建了由 testDestFile 表示的文件。我做错了什么?

Option Explicit

Dim baseDirLen, compareOpts, decryptOpts, destDataPath, destFolder, _
   destFolderPath, encDestFile, encryptorPath, encryptOpts, file, fileName, _
   folder, folderEnd, fso, keyPath, oShell, srcDataPath, srcDirEndLen, _
   srcFolder, strErrorCode, testDestFile, testDiff, testFile, t

Set oShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
srcDataPath = "e:\EZcrypt\TargetData"
keyPath = "e:\EZcrypt\Key\Demo010719.key.bin"
destDataPath = "E:\EZcrypt\EncryptedData"
encryptorPath = "E:\OpenSSL-Win32\bin\openssl"

Set srcFolder = fso.GetFolder(srcDataPath)
baseDirLen = Len(srcDataPath)

recurseFolders(srcFolder)

Sub recurseFolders(srcFolder)
    For Each folder In srcFolder.subfolders
        srcDirEndLen = (Len(folder) - baseDirLen - 1)
        folderEnd = Right(folder, srcDirEndLen)
        destFolderPath = destDataPath & "\" & folderEnd & "\"

        If Not fso.FolderExists(destFolderPath) Then
            fso.CreateFolder(destFolderPath)
        End If

        For Each file In folder.Files
            fileName = fso.GetFileName(file)
            testDestFile = destFolderPath & "test." & fileName
            encDestFile = destFolderPath & fileName & ".enc"

            If Not fso.FileExists(encDestFile) Then
                strErrorCode = ""
                encryptOpts = encryptorPath & " enc -aes-256-cbc -salt -in """ & _
                              file & """ -out """ & encDestFile & _
                              """ -pass file:""" & keyPath & """ -pbkdf2"
                oShell.Run (encryptOpts)
                decryptOpts = encryptorPath & " enc -d -aes-256-cbc -in """ & _
                              encDestFile & """ -out """ & testDestFile & _
                              """ -pass file:""" & keyPath & """ -pbkdf2"
                oShell.Run (decryptOpts)
                WEcript.Echo "testDestFile is '" & testDestFile & "'..."
                Set testFile = fso.OpenTextFile(testDestFile, 8, False, 0)                    
            Else
                WScript.Echo "'" & encDestFile & "' exists. Skipping..."
            End If
        Next
        recurseFolders(folder)
    Next
End Sub

您观察到的行为最可能的原因是 openssl 在尝试打开该文件之前命令您 运行(特别是加密命令,它似乎正在创建文件)还没有完成。您没有告诉 Run 方法等待 return 的命令,因此它们在后台异步 运行ning。据推测,WScript.Echo 增加了足够的延迟,以便在代码继续打开文件之前完成加密。使用 WScript.Sleep 而不是回显某些东西可能会产生相同的效果。

要解决此问题,请等待外部命令 return。

替换这些行:

encryptOpts = encryptorPath & ...
oShell.Run (encryptOpts)
decryptOpts = encryptorPath & ...
oShell.Run (decryptOpts)

有了这个:

encryptOpts = encryptorPath & ...
oShell.Run encryptOpts, 0, True
decryptOpts = encryptorPath & ...
oShell.Run decryptOpts, 0, True

检查外部命令的退出状态也是一个好习惯,这样您就可以查看是否出了问题:

rc = oShell.Run(encryptOpts, 0, True)
If rc <> 0 Then
    'an error occurred
End If