Page 1 of 1
p7zip and runprogram
Posted: Fri Feb 21, 2020 5:02 pm
by pierre2
Apologies for another RunProgram question. I am trying to use p7zip (executable name: 7za) via RunProgram.
Having navigated to the correct directory, the following works in Terminal:
7za a MyBackup *.pdf
(this copies and compresses all .pdf files in the directory, and saves them in an archive called MyBackup, in the same directory)
The following
doesn't work, and I have tried dozens of permutations, including without the Chr(34)s, from the various posts in the forum on the subject.
Code: Select all
theSubDirs.s = "~/Documents/BusRecs testing/"
...
Case #PB_Event_Gadget
If EventGadget() = #theButton
SetCurrentDirectory(theSubDirs)
theApp.s = "7za a MyBackup *.pdf"
If RunProgram("open", Chr(34) + theApp + Chr(34), "")
MessageRequester("Test", "OK")
Else
MessageRequester("Test", "Problem")
EndIf
EndIf
It says "OK", but does not create the archive.
Any help please ? Many thanks.
Re: p7zip and runprogram
Posted: Fri Feb 21, 2020 5:33 pm
by Marc56us
RunProgram <> 0 mean launching program OK (program or document exist) but does not mean all parameters OK
Some things to test:
Add
#PB_Program_Wait to see console information (errors).
I think this is a path problem, I don't know if unix shell shortcut for home dir (~) can be use in a program ?
Try GetHomeDirectory() + "BusRecs testing/" instead
Add
-bb3 (to 7zip parameters) to see log
"show information about additional operations (Analyze, Replicate) for "Add" / "Update" operations."
PS. I don't have a Mac. This may be helpfull ?
viewtopic.php?p=548226#p548226

Re: p7zip and runprogram
Posted: Fri Feb 21, 2020 6:11 pm
by pierre2
Thanks for your ideas, and for your time. I tried all of your suggestions, including all variations of the Mac post (except, it seems, the correct one !), e.g.
Code: Select all
theApp.s = "-a 7za --args " + Chr(34) + "a MyBackup *.pdf" + Chr(34)
- no luck yet ...
Re: p7zip and runprogram
Posted: Sat Feb 22, 2020 6:39 pm
by pierre2
Making progress with a combination of zip (rather than p7zip), a shell script, and of course PB. The p7zip version of the shell script works from the command line, but I can't get it working from RunProgram, so I am now using zip instead.
Re: p7zip and runprogram
Posted: Tue Mar 10, 2020 2:18 am
by deseven
7za is not a default system utility, it's located in a directory that is not included in a default PATH, you can't call it without full path to it.
Zip is a default system utility, you can call it just fine by its name.
Oh, and you can't use ~ anywhere you want, RunProgram() (or SetCurrentDirectory()) is not a shell, it won't expand anything for you.
The correct way to do what you want:
Code: Select all
RunProgram("/usr/local/bin/7za","a MyBackup *.pdf","/path/to/my/cool/directory")
If you really need ~, expand it manually:
Code: Select all
RunProgram("/usr/local/bin/7za","a MyBackup *.pdf",GetEnvironmentVariable("HOME") + "/my/cool/directory")
But that's useless since PB's packer module fully supports both 7z and zip archives and can pack and unpack anything you want without using external tools. Look into UseLZMAPacker() and UseZipPacker().
Re: p7zip and runprogram
Posted: Tue Mar 10, 2020 7:28 pm
by pierre2
Stunning - thank you so much for your reply, which explains so much. I had no idea that PB had 7zip functionality built in. I have stashed your entire reply away in my file of "really useful stuff".
Re: p7zip and runprogram
Posted: Wed Aug 09, 2023 8:41 pm
by Piero
deseven wrote: Tue Mar 10, 2020 2:18 amIf you really need ~, expand it manually:
Code: Select all
RunProgram("/usr/local/bin/7za","a MyBackup *.pdf",GetEnvironmentVariable("HOME") + "/my/cool/directory")
Won't work: the "*"(.pdf) would be interpreted 'literally'

Re: p7zip and runprogram
Posted: Thu Aug 10, 2023 9:01 am
by deseven
Piero wrote: Wed Aug 09, 2023 8:41 pm
Won't work: the "*"(.pdf) would be interpreted 'literally'
Ah, right, it's also shell functionality.
You could try something like that:
Code: Select all
RunProgram("/bin/sh",~"-c \"/usr/local/bin/7za a MyBackup *.pdf\"",GetEnvironmentVariable("HOME") + "/my/cool/directory")
Explanation: spawn a copy of /bin/sh that will expand *.pdf into the list of files and run 7za with it.
Re: p7zip and runprogram
Posted: Thu Aug 10, 2023 2:44 pm
by Piero
deseven wrote: Thu Aug 10, 2023 9:01 am
spawn a copy of /bin/sh
Thanks, that can be useful and gives some possibilities (even pipe thru other commands…)
I tested workarounds like that; the BIG problem is when you need double quotes in your shell command…
Re: p7zip and runprogram
Posted: Thu Aug 10, 2023 2:54 pm
by deseven
Piero wrote: Thu Aug 10, 2023 2:44 pm
the BIG problem is when you need double quotes in your shell command…
Not a problem if you pipe your command into the shell instead of passing it as an argument (standard shell quote rules do apply, though):
Code: Select all
shell = RunProgram("/bin/sh","",GetEnvironmentVariable("HOME") + "/my/cool/directory",#PB_Program_Open|#PB_Program_Write)
If shell
WriteProgramStringN(shell,"/usr/local/bin/7za a MyBackup *.pdf")
WriteProgramData(shell,#PB_Program_Eof,0)
While ProgramRunning(shell) : Delay(10) : Wend ; comment out if you don't need to wait for completion
CloseProgram(shell)
EndIf
Re: p7zip and runprogram
Posted: Thu Aug 10, 2023 3:35 pm
by Piero
deseven wrote: Thu Aug 10, 2023 2:54 pm
Not a problem
Many, many, many thanks! That will be very useful to me!