0

I am trying to make a VBScript for Altap Salamander that would take files from current selection and separately archive them as TAR.

Most of the code below works, but the shell command on line 27 returns Shell error 1 and no TAR files get created.

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"

I've tried changing 7z.exe to both 7zG.exe and 7zFM.exe, adding and removing quotation marks and debugging.

I've also tried the CMD methods from here, but they didn't make much sense to me and I didn't get any to work.

What should I do to make this work?

O_W
  • 37
  • 4
  • 2
    Have you verified your `.Run` string using `MsgBox`? Is `FSO.GetFile(objFile).Path` the same as `tarFilePath`? If so, that's something that can be simplified. Also `Cmd.exe /c` is not needed. Just run the Zip program directly. – LesFerch Feb 09 '23 at 14:10
  • 2
    I would also create a .vbs version of the code to test outside of Altap Salamander. That could help simplify debugging. – LesFerch Feb 09 '23 at 15:04
  • 1
    You haven't accounted for the space in `Program Files`. Try `strRun = """C:\Program Files\7-Zip\7z.exe"" a -ttar -r " & tarFile & " " & FilePath & ""` – LesFerch Feb 10 '23 at 21:56

1 Answers1

1

Update & Solution:

I did a clean up on the code as advised and everything seems to be working just fine!

The for loop now looks like this:

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\7-Zip\7z.exe"" a -ttar -r " & tarFilePath & " " & FilePath & ""
  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

For anyone curious, the strRun command looks like this:

"C:\Program Files\7-Zip\7z.exe" a -ttar -r C:\Users\ondre\img123.tar C:\Users\ondre\img123.jpg

This will work with any archiving extensions 7zip has to offer. Just remember to change the file extension and the -txxx switch. You can also add the mx9 switch to ensure highest level of compression. Example here:

"C:\Program Files\7-Zip\7z.exe" a -tzip -mx9 -r C:\Users\ondre\img123.zip C:\Users\ondre\img123.jpg

WshShell.Run returns a 1 now. I didn't manage to find any documentation from which I would tell if this is ok or not, so I guess I'll just disable the 'result' check completely and will be on my way.

Also, since this is a script for Altap Salamander, I found out an easier way of getting the files from one location to another, for example:

tarFileFold = Salamander.TargetPanel.Path
'will output the archived files to the oposing panel of Salamander, both left -> right & right -> left

tarFileFold = Salamander.SourcePanel.Path
'will output the files to the same folder as the source files. Similar to the first version, but without the use of FSO

Thanks everyone!

O_W
  • 37
  • 4