Trying to Run a Shell Command Not Working

Trying to Run a Shell Command Not Working

mgorecki
Collaborator Collaborator
1,662 Views
3 Replies
Message 1 of 4

Trying to Run a Shell Command Not Working

mgorecki
Collaborator
Collaborator

I'm trying to zip files in a folder using Windows 10.  In Windows 7, I just created a vb script and used the built in zipping function.  Windows 10, however, doesn't have that, so we have to use a 3rd party zipping software.

My code looks like this:

(setq batFileName (strcat (ReplaceSlash sourceFolder) dwgNumber ".bat"))
     (setq batFile (open batFileName "w"))
     (princ (strcat "\"C:/Program Files/7-Zip/7z.exe\" a \"" (ReplaceSlash zipFileName) "\" \"" (ReplaceSlash sourceFolder) "zipped/*\"" ) batFile)
     (close batFile)
     (command "shell" batFileName)

This gets me a .bat file with the one line:

"C:/Program Files/7-Zip/7z.exe" a "C:/UA_TEST/Unit Array Test/1234567GB0A.zip" "C:/UA_TEST/Unit Array Test/zipped/*"

When it does the shell command, I can see the DOS window show up for a second, then disappear, doing nothing.

If I double pick the batch file, it runs and zips all the files within the folder called "zipped" into a zip file called 1234567GB0A.zip.

So the batch file that gets created will work, but just not from the shell command.

 

I also tried to just use a command, like this:

(setq runZip (strcat "\"C:/Program Files/7-Zip/7z.exe\" a \"" (ReplaceSlash zipFileName) "\" \"" (ReplaceSlash sourceFolder) "zipped/*\"" ))
(command "shell" runZip)

It also pops up the DOS window, but does nothing.

Thanks in advance for any help with this.

Mark

0 Likes
Accepted solutions (1)
1,663 Views
3 Replies
Replies (3)
Message 2 of 4

dlanorh
Advisor
Advisor
This could be a result of trying to run the batch file before it is created. The middle lines are passed off to the OS, which must create the file. You are immediately trying to run this file, most likely before the OS has created the file.

Try inserting a delay (delay 2000) between (close batfile) and (command "shell" batFileName). This will pause the lisp for 2 second (2000 millsecs). Then play around with the delay time until you reach something optimal.

I am not one of the robots you're looking for

0 Likes
Message 3 of 4

mgorecki
Collaborator
Collaborator

Hello, thank you for your suggestion, but I was executing the commands line by line.  I waited until the bat file showed up before I executed the shell command.

I will put that bit of code into the program before I'm done.  I totally forgot about the timing issue.

I recorded the screen as the DOS window popped up, and ran the video very slowly.  It showed that the DOS window opened, did not execute any command, then close.

 

0 Likes
Message 4 of 4

mgorecki
Collaborator
Collaborator
Accepted solution

Ok, I finally got it.

I changed these two lines:

(princ (strcat "\"C:/Program Files/7-Zip/7z.exe\" a \"" (ReplaceSlash zipFileName) "\""  " ./zipped/*" ) batFile)

(command "shell" (strcat "\"" batFileName "\""))

I had added the quotes to the filename in the batch file (because the path could contain spaces), but it seems I also need to add quotes to the file name that is being run in the shell command.

Now to celebrate with 4 advil 😊