Jump to content

Single backslash and double backslash in the file path


Recommended Posts

Posted

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!

Posted

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?

Posted

The delimiter should be as it is, so in your case above try

 

(LM:str->lst "Haleem adel\adel\hussien\" "\")

 

(single backslash)

Posted (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 by GLAVCVS
Posted

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

Posted

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.

Posted
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?

  • Like 1
Posted

It should do - on holiday here so can't check though

Posted

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

  • Like 1
  • Agree 1
Posted

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\\"

 

Posted
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

 

 

Posted
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

Posted
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.

Img1.png

Posted

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.

Posted

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.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...