如何用VBScript调用7-Zip计算含空格文件名的CRC32值?
在VBScript中调用7-Zip生成含空格路径文件的CRC32值问题解决
问题背景
需要在VBScript中高效生成文件的CRC32值,由于用CRC32表实现处理大文件速度过慢,选择调用7-Zip.exe的h选项(默认哈希类型为CRC32)——Windows无内置相关功能,且7-Zip应用广泛。
但脚本无法处理文件名含空格的文件:无空格文件名不加引号可正常运行,命令行中给路径加引号(或不加)均可执行,示例命令:
"D:\Program Files\7-Zip\7z.exe" h "D:\OneDrive\Desktop\script test\test.png"
但在VBScript脚本中给路径加双引号后却失效。
原始测试代码
Dim filePath, zip7FilePath, objShell, strCommand, objExec, output filePath = "D:\OneDrive\Desktop\script-test\test.png" '*** working 'filePath = """D:\OneDrive\Desktop\script test\test.png""" '*** not working! zip7FilePath = """D:\Program Files\7-Zip\7z.exe""" Set objShell = CreateObject("WScript.Shell") strCommand = "cmd /c " & zip7FilePath & " h " & filePath Set objExec = objShell.Exec(strCommand) output = "" Do While Not objExec.StdOut.AtEndOfStream output = output & objExec.StdOut.ReadLine & vbCrLf Loop WScript.Echo output
问题
请问我忽略了什么?
修正后的可用脚本
感谢提示cmd /c已过时,修正后的可用脚本如下:
Dim filePath, zip7FilePath, objShell, strCommand, objExec, output filePath = """D:\OneDrive\Desktop\script test\test.png""" zip7FilePath = """D:\Program Files\7-Zip\7z.exe""" Set objShell = CreateObject("WScript.Shell") strCommand = zip7FilePath & " h " & filePath Set objExec = objShell.Exec(strCommand) output = "" Do While Not objExec.StdOut.AtEndOfStream output = output & objExec.StdOut.ReadLine & vbCrLf Loop WScript.Echo output
内容的提问来源于Stack Exchange,提问作者Oliver Brehm
相关产品推荐
相关产品推荐

