【问题标题】:Trouble creating TAR files using 7-zip (VBScript+CMD)使用 7-zip (VBScript+CMD) 创建 TAR 文件时遇到问题
【发布时间】:2023-02-10 18:13:51
【问题描述】:

我正在尝试为 Altap Salamander 制作一个 VBScript,它将从当前选择中获取文件并将它们单独存档为 TAR。

下面的大部分代码都有效,但第 27 行的 shell 命令返回 Shell 错误 1,并且没有创建任何 TAR 文件。

Dim FSO, WshShell
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")

Dim Items, Item, result

' Pick the collection of items to make the list from.
If Salamander.SourcePanel.SelectedItems.Count = 0 Then
  If Salamander.MsgBox("No items are selected. Do you want to make list from all items in the panel?", 4, "Question") = 6 Then
    Set Items = Salamander.SourcePanel.Items
  End If
Else
  Set Items = Salamander.SourcePanel.SelectedItems
End If

ReDim args(Items.Count - 1)
For i = 0 To Items.Count - 1
  args(i) = Items.Item(i).Path
Next

tarFilePath = FSO.GetParentFolderName(args(0))

For i = 0 To UBound(args)
  objFile = args(i)
  tarFileName = FSO.GetFile(objFile).Name & ".tar"
  tarFile = tarFilePath & "\" & tarFileName
  result = WshShell.Run("cmd.exe /c ""C:\Program Files\7-Zip\7zFM.exe"" a -ttar -r """ & tarFile & """ """ & FSO.GetFile(objFile).Path & """", 0, True)
Next

If result = 0 Then
    result = "Shell ran successfully"
Else
    result = "Shell error " & result
End If


MsgBox result, vbInformation, "Archiving Complete"

我试过将 7z.exe 更改为 7zG.exe 和 7zFM.exe,添加和删除引号以及调试。

我也试过 here 的 CMD 方法,但它们对我来说没有多大意义,我没有得到任何工作。

我应该怎么做才能使这项工作?

【问题讨论】:

  • 您是否使用MsgBox 验证了您的.Run 字符串? FSO.GetFile(objFile).PathtarFilePath一样吗?如果是这样,那是可以简化的事情。也不需要 Cmd.exe /c。直接运行 Zip 程序即可。
  • 我还将创建一个 .vbs 版本的代码以在 Altap Salamander 之外进行测试。这可能有助于简化调试。

标签: vbscript tar 7zip


【解决方案1】:

更新:

我已经尝试按照建议清理代码。

for 循环现在看起来像这样:

For i = 0 To UBound(args)
  objFile = args(i)
  FilePath = FSO.GetFile(objFile).Path
  tarFileName = FSO.GetFile(objFile).Name 
  tarFileName = Split(tarFileName, ".")(0) & ".tar"
  tarFilePath = tarFileFold & "" & tarFileName
  strRun = "C:Program Files-Zipz.exe a -ttar -r " & tarFilePath & " " & FilePath & ""
  MsgBox strRun
  Err.Clear
  On Error Resume Next
  result = WshShell.Run(strRun, 0, True)
  If Err Then
    MsgBox "Error " & Err.Number & " " & Err.Description
  End If
  On Error Goto 0
Next

strRun 命令如下所示:

C:Program Files-Zipz.exe a -ttar -r C:Usersondreimg123.tar C:Usersondreimg123.jpg

shell 命令返回了一个未指定的错误,我设法将其捕获为错误-2147024894没有描述。 据我所知,这个错误应该意味着没有找到文件。

关于这可能有什么问题的任何想法?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多