Haleem Posted December 30, 2024 Posted December 30, 2024 Subject: Request for Assistance with Lisp File Path Manipulation Hi All, I am wondering if someone could help me with a Lisp routine. I need to retrieve a file path as a variable from another routine and replace each single backslash with a double backslash. I have tried various methods but have not succeeded. I need to open the file location in Explorer and copy a folder from that location to another. All of these tasks depend on the file path being formatted with double backslashes. i have tried this code but not working: (defun ReplaceBackslashes (inputStr / outputStr index char) (setq outputStr "") ;; Initialize the output string (setq index 1) ;; Start at the first character ;; Loop through each character in the input string (while (<= index (strlen inputStr)) (setq char (substr inputStr index 1)) ;; Get the current character (if (= char "\\") ;; Check if the character is a backslash (setq outputStr (strcat outputStr "\\\\")) ;; Replace with double backslashes (setq outputStr (strcat outputStr char)) ;; Otherwise, add the character as is ) (setq index (1+ index)) ;; Move to the next character ) outputStr ;; Return the modified string ) even I used (chr 92) in the above code and it does not work Thank you for your assistance! Quote
Steven P Posted Thursday at 10:11 AM Posted Thursday at 10:11 AM I'll often use Lee Macs String to List function to split a string up by delimiter, in your case, \ and then recombine using list to string with the new text replacing the delimiter, in your case, \\ https://lee-mac.com/stringtolist.html https://lee-mac.com/listtostring.html Quote
Haleem Posted Thursday at 11:11 AM Author Posted Thursday at 11:11 AM Thanks alot Steven, thats great idea Quote
Haleem Posted Thursday at 01:34 PM Author Posted Thursday at 01:34 PM Hi Steven, I used the Lee Mack function and i called it (LM:str->lst "Haleem adel\adel\hussien\" "\\") but it is not working Is there a mistake in the call, or is there an alternative solution? Quote
Steven P Posted Thursday at 04:28 PM Posted Thursday at 04:28 PM The delimiter should be as it is, so in your case above try (LM:str->lst "Haleem adel\adel\hussien\" "\") (single backslash) Quote
GLAVCVS Posted Thursday at 04:44 PM Posted Thursday at 04:44 PM (edited) Such a string is not possible because it should be enclosed in double quotes at the end Where do you get the string "Haleem adel\adel\hussien\" from? Edited Thursday at 04:54 PM by GLAVCVS Quote
GLAVCVS Posted Thursday at 05:00 PM Posted Thursday at 05:00 PM There are 3 options: -Manual input from the keyboard -Reading from a file -From 'getfiled', 'dos_getdir' or similar functions In any of those 3 cases, it is impossible to provide in lisp a text string like the one you indicate Quote
Steven P Posted Thursday at 05:46 PM Posted Thursday at 05:46 PM Copy and paste path from Windows Explorer... single backslash, import from excel file, single backslash, another routine output in that format.... more than 3 options to get a file path with single backslashes. Quote
GLAVCVS Posted Thursday at 06:20 PM Posted Thursday at 06:20 PM 24 minutes ago, Steven P said: Copy and paste path from Windows Explorer... single backslash, import from excel file, single backslash, another routine output in that format.... more than 3 options to get a file path with single backslashes. Ok Although I was just thinking about options to get that string from Lisp. As for Excel I've never worked with it from AutoCAD. Anyway, I apologize for that. But.... '(LM:str->lst "Haleem adel\adel\hussien\" "\")' are you sure it works? 1 Quote
Steven P Posted Thursday at 07:43 PM Posted Thursday at 07:43 PM It should do - on holiday here so can't check though Quote
Steven P Posted Thursday at 09:30 PM Posted Thursday at 09:30 PM If it doesn't work - could try vl-princ-to-string or vl-prin1-to-string see if that works in your code or in string to list 1 1 Quote
mhupp Posted 22 hours ago Posted 22 hours ago Give this at try. used this to replace all spaces with %20 for url's uses a loops to keep searching string until none are found. (setq path "C:\Haleem adel\adel\hussien\") (while (vl-string-search "\" path) (setq path (vl-string-subst "\" "\\" path)) ) (princ path) ;output "C:\\Haleem adel\\adel\\hussien\\" Quote
Haleem Posted 20 hours ago Author Posted 20 hours ago Quote (LM:str->lst "Haleem adel\adel\hussien\" "\") I tested this one and it is not working 1 hour ago, mhupp said: Give this at try. used this to replace all spaces with %20 for url's uses a loops to keep searching string until none are found. (setq path "C:\Haleem adel\adel\hussien\") (while (vl-string-search "\" path) (setq path (vl-string-subst "\" "\\" path)) ) (princ path) ;output "C:\\Haleem adel\\adel\\hussien\\" Also, this one is not working Quote
Haleem Posted 18 hours ago Author Posted 18 hours ago 9 hours ago, Steven P said: If it doesn't work - could try vl-princ-to-string or vl-prin1-to-string see if that works in your code or in string to list That works fine, Thanks Steven Quote
GLAVCVS Posted 13 hours ago Posted 13 hours ago 4 hours ago, Haleem said: That works fine, Thanks Steven Are you really sure it works? The '\' character in Lisp, before any punctuation mark, modifies the original character. For this reason, the '\' before the final " character prevents the string from fulfilling its function as a string delimiter. In the attached image you can see what I mean. Quote
GLAVCVS Posted 13 hours ago Posted 13 hours ago The problem is in the text string itself that is used as an argument. That is why I asked you where the source of that argument is. If it is the result of copying a text string from Windows Explorer or any other external application, I think you will have no choice but to modify it manually. But if you get it from Lisp (which I believe is not possible) please tell us how you get it. Quote
Haleem Posted 13 hours ago Author Posted 13 hours ago I used the getfiled function to get the file path, which I then used to write a script file and execute it. However, I faced an issue with how the file path was being formatted. When I used princ or write, the file path displayed only single backslashes (\), causing errors in the script because backslashes need to be escaped. To fix this, I used prin1, which correctly preserved the double backslashes (\\) in the file path, making it work as expected. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.