Skip to content
⌘ NSIS Forum Archive

GetFolderName

2 posts

theblazingangel#

GetFolderName

i don't know whether a function already exists for this, but i had a need to get the last folder in a specified path and so wrote my own.

note, it requires GetParent! (which is found in the documentation)

;usage example:
Push "C:\folder1\folder2"
Call GetFolderName
Pop $0
;in this case returns 'folder2'

Function GetFolderName
   Exch $R0
   Push $R1
   Push $R2
   ; Remove trailing slash(es)
    loop:
    StrCpy $R1 $R0 1 -1
    StrCmp $R1 '\' 0 +3
    StrCpy $R0 $R0 -1
    goto loop
   ; Get parent directory
    Push $R0
    Call GetParent
    Pop $R1
   ; Take parent dir off this dir leaving last dir name
    StrLen $R2 "$R1"
    StrCpy $R0 $R0 "" $R2
   ; Remove starting slash if present (probably is)
    StrCpy $R2 $R0 1
    StrCmp $R2 '\' 0 +2
    StrCpy $R0 $R0 "" 1
   Pop $R2
   Pop $R1
   Exch $R0
FunctionEnd 
its pretty robust, you can throw anything in the following format at it and it'll handle it:
[anything][any_number_of_backslashes]folder[any_number_of_backslashes]

would love it if this made it's way into the documentation 🙂