Get parent directory: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Added category links.)
m (Adding new author and category links.)
Line 1: Line 1:
{|align=right
{{PageAuthor|sunjammer}}
|<small>Author: [[{{ns:2}}:sunjammer|sunjammer]] ([[{{ns:3}}:sunjammer|talk]], [[{{ns:-1}}:Contributions/sunjammer|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
This function gets the parent folder of a file or subfolder. The result is given without the last "\" (backslash). When a parent is not found, it preserves the path.
This function gets the parent folder of a file or subfolder. The result is given without the last "\" (backslash). When a parent is not found, it preserves the path.
Line 46: Line 44:
</highlight-nsis>
</highlight-nsis>


[[{{ns:14}}:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Revision as of 12:23, 24 June 2005

Author: sunjammer (talk, contrib)


Description

This function gets the parent folder of a file or subfolder. The result is given without the last "\" (backslash). When a parent is not found, it preserves the path.

The Script

 ; GetParent
 ; input, top of stack  (e.g. C:\Program Files\Poop)
 ; output, top of stack (replaces, with e.g. C:\Program Files)
 ; modifies no other variables.
 ;
 ; Usage:
 ;   Push "C:\Program Files\Directory\Whatever"
 ;   Call GetParent
 ;   Pop $R0
 ;   ; at this point $R0 will equal "C:\Program Files\Directory"
 
Function GetParent
 
  Exch $R0
  Push $R1
  Push $R2
  Push $R3
 
  StrCpy $R1 0
  StrLen $R2 $R0
 
  loop:
    IntOp $R1 $R1 + 1
    IntCmp $R1 $R2 get 0 get
    StrCpy $R3 $R0 1 -$R1
    StrCmp $R3 "\" get
  Goto loop
 
  get:
    StrCpy $R0 $R0 -$R1
 
    Pop $R3
    Pop $R2
    Pop $R1
    Exch $R0
 
FunctionEnd