Get last directory path part: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Afrow UK}}
|<small>Author: [[{{ns:2}}:Afrow UK|Afrow UK]] ([[{{ns:3}}:Afrow UK|talk]], [[{{ns:-1}}:Contributions/Afrow UK|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
This script will get the last part of a directory path.
This script will get the last part of a string after a specified character. Useful to get file extensions, last file name or last directory part.


== Usage ==
== Usage ==
<highlight-nsis>
<highlight-nsis>
Push "C:\program files\geoffrey\files"
Push "C:\program files\geoffrey\files"
Call GetFileName
Push "\"
Call GetAfterChar
Pop $R0
Pop $R0
</highlight-nsis>
</highlight-nsis>


$R0 is now "files"
$R0 is now "files"
Push "." instead of "\" to get a file extension or perhaps "/" for URL.


You can also use this function to get the file extension of an entered file path.
The return value is empty "" if the input char was not found.
In the function replace...
:StrCmp $2 "\" exit
...with
:StrCmp $2 "." exit
 
The file extension (after the ".") will then be popped off the stack.


== The Function ==
== The Function ==
<highlight-nsis>
<highlight-nsis>
Function GetFileName
Function GetAfterChar
   Exch $0 ; input string
   Exch $0 ; chop char
   Push $1
  Exch
   Exch $1 ; input string
   Push $2
   Push $2
   StrCpy $1 0
  Push $3
   StrCpy $2 0
   loop:
   loop:
     IntOp $1 $1 - 1
     IntOp $2 $2 - 1
     StrCpy $2 $0 1 $1
     StrCpy $3 $1 1 $2
     StrCmp $2 "" exit2
     StrCmp $3 "" 0 +3
     StrCmp $2 "\" exit1 ; replace "\" with "." if you want file extension
      StrCpy $0 ""
      Goto exit2
     StrCmp $3 $0 exit1
     Goto loop
     Goto loop
   exit1:
   exit1:
     IntOp $1 $1 + 1
     IntOp $2 $2 + 1
     StrCpy $0 $0 "" $1
     StrCpy $0 $1 "" $2
   exit2:
   exit2:
    Pop $3
     Pop $2
     Pop $2
     Pop $1
     Pop $1
     Exch $0
     Exch $0 ; output
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>


Written by [[user:kichik|KiCHiK]]
Original written by [[user:kichik|KiCHiK]] (GetFileName)
Modified for character input by me (GetAfterChar).


(I also wrote a similar script, but this one is better)
(I also wrote a similar script, but this one is better)


-Stuart :)
-Stu
 
[[Category:Disk, Path & File Functions]]

Latest revision as of 11:28, 7 September 2005

Author: Afrow UK (talk, contrib)


Description

This script will get the last part of a string after a specified character. Useful to get file extensions, last file name or last directory part.

Usage

Push "C:\program files\geoffrey\files"
Push "\"
Call GetAfterChar
Pop $R0

$R0 is now "files" Push "." instead of "\" to get a file extension or perhaps "/" for URL.

The return value is empty "" if the input char was not found.

The Function

Function GetAfterChar
  Exch $0 ; chop char
  Exch
  Exch $1 ; input string
  Push $2
  Push $3
  StrCpy $2 0
  loop:
    IntOp $2 $2 - 1
    StrCpy $3 $1 1 $2
    StrCmp $3 "" 0 +3
      StrCpy $0 ""
      Goto exit2
    StrCmp $3 $0 exit1
    Goto loop
  exit1:
    IntOp $2 $2 + 1
    StrCpy $0 $1 "" $2
  exit2:
    Pop $3
    Pop $2
    Pop $1
    Exch $0 ; output
FunctionEnd

Original written by KiCHiK (GetFileName) Modified for character input by me (GetAfterChar).

(I also wrote a similar script, but this one is better)

-Stu