#1
|
||||
|
||||
Using sd-sys-exec and paths containing spaces
I searched this forum for some help concerning this topic and didn't find the answer. After some trial and error I figured it out, so I thought I would post it for future searches.
We have some custom lisp code which calls a batch file with a command similar to: (sd-sys-exec batch_file_path "parameter 1" "parameter 2") This worked find as long as batch_file_path didn't contain any spaces. When I tested from a path with spaces (C:\Program Files\...) I got the following error: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. I thought no problem, I'll just put double quotes around the batch file path and tried this. (sd-sys-exec "batch_file_path" "parameter_1" "parameter_2") But to my surprise that produced the same error message. After experimenting with some different combinations of quotes, I found the following works. (sd-sys-exec ""batch_file_path" "parameter_1" "parameter_2"")
__________________
John Scheffel |
#2
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
Hmmm.... this doesn't work for me, I only get Lisp errors, and it doesn't look like valid Lisp syntax anyway.
If you need to place a double-quote character into a string, use a backslash to "escape" it. Example: Code:
(oli:sd-sys-exec "\"c:/temp/foo bar/write.exe\" c:/temp/test.txt") Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#3
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
OK, I guess I didn't do a very good job of documenting the problem and solution, but the original code was fairly complex and I was trying to simplify the explanation.
The original code looked like this. Code:
(setq pcb-update-command (format nil "~A \"~A\" \"~A\" " pcb-update-batch pcb-source-path pcb-dest-path)) (oli:sd-sys-exec pcb-update-command) I thought the following would work if pcb-update-batch contained spaces, but it failed with the same error. I believe this is similar to your example. Code:
(setq pcb-update-command (format nil "\"~A\" \"~A\" \"~A\" " pcb-update-batch pcb-source-path pcb-dest-path)) (oli:sd-sys-exec pcb-update-command) Code:
(setq pcb-update-command (format nil "\"\"~A\" \"~A\" \"~A\"\" " pcb-update-batch pcb-source-path pcb-dest-path)) (oli:sd-sys-exec pcb-update-command)
__________________
John Scheffel |
#4
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
John,
thanks for the clarification. This actually rings a bell. I remember I came across a variation of this problem roughly two years ago when I experimented with the DOS command prompt; I even took some notes about it back then. And so I just dusted off those notes, reformatted and completed them, and then posted them on my blog site at http://www.clausbrod.de/Blog/BlogOnSoftware20060318 . I hope you don't mind that I'm explicitly referring to you and your posting; if you don't like a teenie weenie little bit of well-deserved fame , let me know, and I'll update the blog post accordingly. The blog entry also shows how to save one level of confusing quotes when writing Lisp code. Adapted to your example: Code:
(oli:sd-sys-exec (format nil "\"~S ~S ~S\"" pcb-update-batch pcb-source-path pcb-dest-path)) Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#5
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
Interesting blog entry. I didn't realize that cmd.exe required this type of syntax as well.
So does the format command just replace the ~S with the variable value enclosed in double quotes? I was not aware of this method, but it is much cleaner. Thanks.
__________________
John Scheffel |
#6
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
The ~S directive tells format to print the argument in such a way that it could be read back by Lisp. For a string, this means that it will be automatically quoted ( http://www.lisp.org/HyperSpec/Body/sec_22-3-4-2.html ).
Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#7
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
Today, I confirmed that the same issue also applies to OSDD (as expected, since this is really an issue of the underlying DOS shell), and attached some notes on this to the blog entry at http://www.clausbrod.de/Blog/BlogOnSoftware20060318 (including a few lines of macro code).
Cheers, Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#8
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
I ran into an odd problem today with use of the ~S substitution. It seems that in addition to adding double quotes around the string, if the string contains any back slashes they will be doubled. This is usually not an issue with drive paths since Windows seems to ignore a double slash in a path. For example, the path C:\\temp works the same as C:\temp.
However, it did cause a problem when the string was a UNC share path such as \\myshare\folder. This string was converted into "\\\\myshare\folder" which caused an error in the program it was passed to (in this case RoboCopy). The ~A substitution does not change the number of backslashes. So I ended up going back to the longer and more complex syntax of ~A shown above.
__________________
John Scheffel |
#9
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
Argl. John, you are of course right - ~S was a dumb idea since it's doing more than what we need here.
Thanks for spotting this. I also just fixed my corresponding blog entry at http://www.clausbrod.de//Blog/BlogOnSoftware20060318 Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#10
|
|||
|
|||
Re: Using sd-sys-exec and paths containing spaces
Actually, I don't think using ~S is a dumb idea at all -- it helps to make the code much more understandable (and easy to write!), which is a good thing!
I believe the problem John found can be avoided simply by a call to sd-string-replace: Code:
(oli:sd-sys-exec (oli:sd-string-replace (format nil "\"~S ~S ~S\"" pcb-update-batch pcb-source-path pcb-dest-path)) "\\\\" "\\") Code:
(setq cmdstr (format nil "\"~S ~S ~S\"" pcb-update-batch pcb-source-path pcb-dest-path)) (setq cmdstr (oli:sd-string-replace cmdstr "\\\\" "\\")) (oli:sd-sys-exec cmdstr) This will help to show the original intent, and will still pass the right string to the operating system. Of course, in a situation where fast processing is critical, you would likely want to avoid the call to sd-string-replace since it will take additional processing cycles, but with today's processing power it's most likely not an issue. Just something to consider....
__________________
Andy Poulsen AI MAXTools: Dream. Design. Done. It's that easy! Add-ins bringing new functionality and speed to Creo Elements/Direct and CoCreate products. Now available for v17-v20+! See them in action at www.ai-maxtools.com and then try them for yourself -- FREE! |
#11
|
||||
|
||||
Re: Using sd-sys-exec and paths containing spaces
I share the preference for clearer code, of course. But somehow, adding extra backslashes to a string by using the slightly wrong directive, only to remove them again in another step doesn't feel elegant to me.
But then, I guess it all boils down to a matter of taste. The important part is to fix the issue which John found, and that can be done in both described ways. Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/ |
#12
|
|||
|
|||
Re: Using sd-sys-exec and paths containing spaces
Very good point, Claus. I agree that it's not the best or most elegant way to do it; however, I did want to point out that if someone wanted to use the ~S directive to keep things simple (especially for an even more complex type of function call), there is a way to make it work.
As you say, it's primarily up to the programmer... and it's nice that there are usually many different ways to solve the same problem!
__________________
Andy Poulsen AI MAXTools: Dream. Design. Done. It's that easy! Add-ins bringing new functionality and speed to Creo Elements/Direct and CoCreate products. Now available for v17-v20+! See them in action at www.ai-maxtools.com and then try them for yourself -- FREE! |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|