Fixing Backslash Display Issues When Writing File Paths in a Function

Fixing Backslash Display Issues When Writing File Paths in a Function

adaptacad
Advocate Advocate
667 Views
9 Replies
Message 1 of 10

Fixing Backslash Display Issues When Writing File Paths in a Function

adaptacad
Advocate
Advocate

I’m trying to create a function to generate a file and write the path, but the backslashes (\) aren’t displaying correctly when I select them. How can I fix this?

(setq software_name_search_path "C:\\\Users\Public\Music\")

the goal is this:

(setq software_name_search_path "C:\\Users\\Public\\Music\\")

 

 

(defun formatar-caminho (caminho)
  "Formata um caminho para usar barras duplas em todas as barras invertidas."
  (vl-string-subst "\\\\" "\\" caminho))

(defun criarouatualizar ()
  (setq cfg-path "C:\\software_name\\values.cfg")
  (setq path-installation (strcat (getvar "roamablerootprefix") "support\\installation_folder.lsp"))
  (setq search-path nil)
  (if (findfile cfg-path)
    (setq search-path "C:\\software_name\\")
    (progn
      (setq search-path (LM:browseforfolder "Select a folder" "C:\\" 0))
      (if (null search-path)
        (progn
          (princ "\nNenhuma pasta selecionada. A operação foi cancelada.")
          (exit)
        )
      )
      (setq search-path (formatar-caminho search-path))
    )
  )

  (setq search-path (formatar-caminho search-path))
  
  (setq _open (open path-installation "w"))
  (if _open
    (progn
      (write-line (strcat "(setq software_name_search_path \"" search-path "\\\")") _open)
      (close _open)
      (princ (strcat "\nArquivo criado/atualizado com sucesso: " path-installation))
    )
    (princ "\nErro: Não foi possível abrir o arquivo para escrita.")
  )
  (princ)
)




;; Browse for Folder  -  Lee Mac
;; Displays a dialog prompting the user to select a folder.
;; msg - [str] message to display at top of dialog
;; dir - [str] [optional] root directory (or nil)
;; bit - [int] bit-coded flag specifying dialog display settings
;; Returns: [str] Selected folder filepath, else nil.

(defun LM:browseforfolder ( msg dir bit / err fld pth shl slf )
    (setq err
        (vl-catch-all-apply
            (function
                (lambda ( / app hwd )
                    (if (setq app (vlax-get-acad-object)
                              shl (vla-getinterfaceobject app "shell.application")
                              hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                              fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg bit dir)
                        )
                        (setq slf (vlax-get-property fld 'self)
                              pth (vlax-get-property slf 'path)
                              pth (vl-string-right-trim "\\" (vl-string-translate "/" "\\" pth))
                        )
                    )
                )
            )
        )
    )
    (if slf (vlax-release-object slf))
    (if fld (vlax-release-object fld))
    (if shl (vlax-release-object shl))
    (if (vl-catch-all-error-p err)
        (prompt (vl-catch-all-error-message err))
        pth
    )
)


; (setq software_name_search_path "C:\\\Users\Public\Music\")


; (criarouatualizar)

 

 
0 Likes
Accepted solutions (1)
668 Views
9 Replies
Replies (9)
Message 2 of 10

paullimapa
Mentor
Mentor

According to online help documentation

(vl-string-subst new-str pattern str [start-pos])

Your current code shows this:

 

(vl-string-subst "\\\\" "\\" caminho))

 

You may want to try this way:

 

(vl-string-subst "\\" "\\\\" caminho))

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 10

Moshe-A
Mentor
Mentor

@adaptacad  hi,

 

(setq software_name_search_path "C:\\\Users\Public\Music\")

 

The above is invalid assignment. AutoLISP built-in functions does not produce this, it's only a manual mistake.

make sure to use "\\" or "/".

 

Moshe

 

0 Likes
Message 4 of 10

adaptacad
Advocate
Advocate

@paullimapa I've tried that and a few other edits...

it just returns a \ when I edit.

(setq software_name_search_path "C:\Users\Public\Music\")
0 Likes
Message 5 of 10

adaptacad
Advocate
Advocate

@Moshe-A , the issue lies exactly here:

(setq software_name_search_path "C:\\Users\\Public\\Music\\")

My function is writing this variable to a file, but the output ends up with \\\ and \, which is not ideal. I want to adjust it to return \\ as properly supported.

 

 

0 Likes
Message 6 of 10

Moshe-A
Mentor
Mentor

@adaptacad ,

 

(setq software_name_search_path "C:\\\Users\Public\Music\")

 

if you are doing this then this is not a valid assignment, if AutoLISP can not hold this value you can not pass it as a function argument (cause passing a value to function involve assignment inside that function)

 

Moshe

 

 

 

 

 

 

 

Message 7 of 10

paullimapa
Mentor
Mentor
Accepted solution

Try the attached modified criarouatualizar.lsp code as a workaround.

The installation_folder.lsp written now looks correct:

(setq software_name_search_path "C:\\Users\\Public\\Music\\")

What I did was used this aec_txt-sr_txt  function to search & replace the backslashes:

; aec_txt-sr_txt searches given txt 
; for a special character findtxt and
; replaces with apostrophe preceeding special character repltxt
; returning new txt nametmp
; Example #1:
; (aec_txt-sr_txt "#" "`#" "temp.REV#00#A")
; Returns:
; temp.REV`#00`#A
; Example #2:
; (aec_txt-sr_txt "@" "`@" "temp.REV@00@A")
; Returns:
; temp.REV`@00`@A  
(defun aec_txt-sr_txt (findtxt repltxt txt / i nametmp txtstd)
  (setq txtstd "=") ; set to unique text character that won't be used in text string
  (setq nametmp (vl-string-translate findtxt txtstd txt)) ; translate findtxt using unique text character
  (if(/= nametmp txt) ; run replacement only if something changed
    (progn
      (setq i 1)
      (while i         
       (setq nametmp(vl-string-subst repltxt txtstd nametmp i)) ; replace text string
       (setq i (+ i (strlen repltxt)))
       (setq i (vl-string-search txtstd nametmp i)) ; continue loop if more text strings found
      )
	)
  )
  nametmp
) ; defun aec_txt-sr_txt

I also commented out your search-path statement inside the if function:

  (if (findfile cfg-path)
    (setq search-path "C:\\software_name\\")
    (progn
      (setq search-path (LM:browseforfolder "Select a folder" "C:\\" 0))
      (if (null search-path)
        (progn
          (princ "\nNenhuma pasta selecionada. A operação foi cancelada.")
          (exit)
        )
      )
;      (setq search-path (formatar-caminho search-path))
    )
  )

Because you will run this afterwards here where I added an extra set of backslashes at the end to make it work:

;  (setq search-path (formatar-caminho search-path))
   (setq search-path (strcat (aec_txt-sr_txt "\\" "\\\\" search-path) "\\"))

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 8 of 10

adaptacad
Advocate
Advocate

This was perfect!!! Thank you very much!!😘
@paullimapa 

0 Likes
Message 9 of 10

adaptacad
Advocate
Advocate

Thank you very much @Moshe-A !!

0 Likes
Message 10 of 10

paullimapa
Mentor
Mentor

You are welcome…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes