NSIS获取文件路径的最后一个字符串
如何从NSIS语言的字符串中仅获取jre1.8.091NSIS获取文件路径的最后一个字符串,nsis,Nsis,如何从NSIS语言的字符串中仅获取jre1.8.091 String "C:\Program Files\Java\jre1.8.0_91" @Vinod希望您使用的是最新版本的NSIS。此外,还可以使用NSIS(nsh.zip)包含最新版本的headers,或者包含WordFind函数 !include "WordFunc.nsh" 在您的.nsi脚本中 这样,您可以使用WordFind函数从文件路径(示例中为C:\Program Files\Java\jre1.8.091)中提取文件名(
String "C:\Program Files\Java\jre1.8.0_91"
@Vinod希望您使用的是最新版本的NSIS。此外,还可以使用NSIS(nsh.zip)包含最新版本的headers,或者包含WordFind函数
!include "WordFunc.nsh"
在您的.nsi脚本中
这样,您可以使用WordFind函数从文件路径(示例中为C:\Program Files\Java\jre1.8.091)中提取文件名(示例中为jre1.8.091),如下所示:
${WordFind} "${FilePath}" "\" "-1" $R0
!macro PathGetFilename path outvar
Push "${path}"
Call PathGetFilename
Pop ${outvar}
!macroend
Function PathGetFilename
Exch $1
Push $2
Push $3
StrCpy $2 ""
loop:
IntOp $2 $2 - 1
StrCpy $0 $1 1 $2
StrCmp $0 "" done
StrCmp $0 '\' +3
StrCmp $0 '/' +2
Goto loop
IntOp $2 $2 + 1
IntCmp $2 0 "" +2 +2
StrCpy $1 "" ; Ended with slash, return empty string
StrCpy $1 $1 "" $2
done:
Pop $3
Pop $2
Exch $1
FunctionEnd
Section
!insertmacro PathGetFilename "c:\test" $0
DetailPrint |$0|
!insertmacro PathGetFilename "c:\foo/bar" $0
DetailPrint |$0|
!insertmacro PathGetFilename "nameonly" $0
DetailPrint |$0|
!insertmacro PathGetFilename "\endslash\" $0
DetailPrint |$0|
SectionEnd
$R0将包含文件路径中的最后一个“字”(jre1.8.091)
这是通过使用反斜杠(\)作为字符串的分隔符,并选择第一个单词,从字符串末尾向后计数(“-1”)来实现的。@Vinod希望您使用的是最新版本的NSIS。此外,还可以使用NSIS(nsh.zip)包含最新版本的headers,或者包含WordFind函数
!include "WordFunc.nsh"
在您的.nsi脚本中
这样,您可以使用WordFind函数从文件路径(示例中为C:\Program Files\Java\jre1.8.091)中提取文件名(示例中为jre1.8.091),如下所示:
${WordFind} "${FilePath}" "\" "-1" $R0
!macro PathGetFilename path outvar
Push "${path}"
Call PathGetFilename
Pop ${outvar}
!macroend
Function PathGetFilename
Exch $1
Push $2
Push $3
StrCpy $2 ""
loop:
IntOp $2 $2 - 1
StrCpy $0 $1 1 $2
StrCmp $0 "" done
StrCmp $0 '\' +3
StrCmp $0 '/' +2
Goto loop
IntOp $2 $2 + 1
IntCmp $2 0 "" +2 +2
StrCpy $1 "" ; Ended with slash, return empty string
StrCpy $1 $1 "" $2
done:
Pop $3
Pop $2
Exch $1
FunctionEnd
Section
!insertmacro PathGetFilename "c:\test" $0
DetailPrint |$0|
!insertmacro PathGetFilename "c:\foo/bar" $0
DetailPrint |$0|
!insertmacro PathGetFilename "nameonly" $0
DetailPrint |$0|
!insertmacro PathGetFilename "\endslash\" $0
DetailPrint |$0|
SectionEnd
$R0将包含文件路径中的最后一个“字”(jre1.8.091)
这是通过使用反斜杠(\)作为字符串的分隔符,并选择第一个单词,从字符串末尾向后计数(“-1”)来完成的。NSIS中的每个字符串操作都可以使用
StrCpy
、StrCmp
和StrLen
进行编码。您可以尝试以下方法:
${WordFind} "${FilePath}" "\" "-1" $R0
!macro PathGetFilename path outvar
Push "${path}"
Call PathGetFilename
Pop ${outvar}
!macroend
Function PathGetFilename
Exch $1
Push $2
Push $3
StrCpy $2 ""
loop:
IntOp $2 $2 - 1
StrCpy $0 $1 1 $2
StrCmp $0 "" done
StrCmp $0 '\' +3
StrCmp $0 '/' +2
Goto loop
IntOp $2 $2 + 1
IntCmp $2 0 "" +2 +2
StrCpy $1 "" ; Ended with slash, return empty string
StrCpy $1 $1 "" $2
done:
Pop $3
Pop $2
Exch $1
FunctionEnd
Section
!insertmacro PathGetFilename "c:\test" $0
DetailPrint |$0|
!insertmacro PathGetFilename "c:\foo/bar" $0
DetailPrint |$0|
!insertmacro PathGetFilename "nameonly" $0
DetailPrint |$0|
!insertmacro PathGetFilename "\endslash\" $0
DetailPrint |$0|
SectionEnd
NSIS中的每个字符串操作都可以使用
StrCpy
、StrCmp
和StrLen
进行编码。您可以尝试以下方法:
${WordFind} "${FilePath}" "\" "-1" $R0
!macro PathGetFilename path outvar
Push "${path}"
Call PathGetFilename
Pop ${outvar}
!macroend
Function PathGetFilename
Exch $1
Push $2
Push $3
StrCpy $2 ""
loop:
IntOp $2 $2 - 1
StrCpy $0 $1 1 $2
StrCmp $0 "" done
StrCmp $0 '\' +3
StrCmp $0 '/' +2
Goto loop
IntOp $2 $2 + 1
IntCmp $2 0 "" +2 +2
StrCpy $1 "" ; Ended with slash, return empty string
StrCpy $1 $1 "" $2
done:
Pop $3
Pop $2
Exch $1
FunctionEnd
Section
!insertmacro PathGetFilename "c:\test" $0
DetailPrint |$0|
!insertmacro PathGetFilename "c:\foo/bar" $0
DetailPrint |$0|
!insertmacro PathGetFilename "nameonly" $0
DetailPrint |$0|
!insertmacro PathGetFilename "\endslash\" $0
DetailPrint |$0|
SectionEnd
理想情况下,您应该同时将\和/作为路径分隔符。理想情况下,您应该同时将\和/作为路径分隔符。