Convert "\" to "\\" in a string or dir path

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This function is used if you want to place a directory path on a IO page as a label. If the directory path has single "\" in it, then these "\" are not displayed, and you will have the directory path shown without any "\". Using "\\" instead if "\" will display the path properly.

Usage

Push $INSTDIR (e.g. C:\program~1\nsis) 
Call ConvertBStoDBS 
Pop $R0

$R0 is now C:\\program~1\\nsis

The Function

Function ConvertBStoDBS
 Exch $R0 ;input string
 Push $R1
 Push $R2
 StrCpy $R1 0
loop:
  IntOp $R1 $R1 - 1
  StrCpy $R2 $R0 1 $R1
  StrCmp $R2 "" done
 StrCmp $R2 "\" 0 loop
  StrCpy $R2 $R0 $R1 ;part before
   Push $R1
  IntOp $R1 $R1 + 1
  StrCpy $R1 $R0 "" $R1 ;part after
 StrCpy $R0 "$R2\\$R1"
   Pop $R1
  IntOp $R1 $R1 - 1
Goto loop
done:
   Pop $R2
   Pop $R1
   Exch $R0 ;output string
FunctionEnd

-Stu