CoCreate User Forum  

Go Back   CoCreate User Forum > Support > Customization

Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
  #1  
Old 03-16-2006, 02:24 PM
John Scheffel's Avatar
John Scheffel John Scheffel is offline
Administrator
 
Join Date: Sep 2002
Location: San Jose, CA
Posts: 1,288
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
Reply With Quote
  #2  
Old 03-17-2006, 12:27 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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")
See also http://www.clausbrod.de/Osdm/OsdmFaqLisp#DoubleQuotes for a related FAQ entry.

Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/
Reply With Quote
  #3  
Old 03-17-2006, 04:12 PM
John Scheffel's Avatar
John Scheffel John Scheffel is offline
Administrator
 
Join Date: Sep 2002
Location: San Jose, CA
Posts: 1,288
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)
pcb-update-batch, pcb-source-path and pcb-dest-path are string variables containing file paths defined earlier. The above worked fine provided pcb-update-batch contained no spaces. If it contained spaces (C:\Program Files\...) I got the error that 'C:\Program is not recognized...'.

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)
The following does work.
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)
It was necessary to create a string with double quotes around everthing in addition to the double quotes around each path in the command.
__________________
John Scheffel
Reply With Quote
  #4  
Old 03-18-2006, 06:49 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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))
Thanks for the inspiration!

Claus
__________________
CoCreate Modeling FAQ: http://www.clausbrod.de/CoCreateModeling/
Reply With Quote
  #5  
Old 03-20-2006, 09:25 AM
John Scheffel's Avatar
John Scheffel John Scheffel is offline
Administrator
 
Join Date: Sep 2002
Location: San Jose, CA
Posts: 1,288
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
Reply With Quote
  #6  
Old 03-20-2006, 11:50 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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/
Reply With Quote
  #7  
Old 03-29-2006, 09:25 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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/
Reply With Quote
  #8  
Old 06-23-2006, 02:08 PM
John Scheffel's Avatar
John Scheffel John Scheffel is offline
Administrator
 
Join Date: Sep 2002
Location: San Jose, CA
Posts: 1,288
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
Reply With Quote
  #9  
Old 06-24-2006, 04:18 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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/
Reply With Quote
  #10  
Old 06-24-2006, 06:09 PM
Andy Poulsen Andy Poulsen is offline
Administrator
 
Join Date: Apr 2003
Location: Fort Collins, Colorado
Posts: 273
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)) "\\\\" "\\")
or, using the original idea of defining the command string as a separate variable (and changing things one step at a time):
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)
Both of the above situations will change any instances of "\\" to "\" . If this doesn't give exactly the desired result, slight modifications should make it work.

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!
Reply With Quote
  #11  
Old 06-25-2006, 10:23 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
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/
Reply With Quote
  #12  
Old 06-25-2006, 08:38 PM
Andy Poulsen Andy Poulsen is offline
Administrator
 
Join Date: Apr 2003
Location: Fort Collins, Colorado
Posts: 273
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!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 11:54 PM.



Hosted by SureServer    Forums   Modeling FAQ   Macro Site   Vendor/Contractors   Software Resellers   CoCreate   Gallery   Home   Board Members   Regional User Groups  By-Laws  

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
You Rated this Thread: