如何使用NSIS读取包含键和值对的文本文件?

时间:2016-02-09 09:54:23

标签: nsis

我有一个文本文件,其中包含文件名作为键,路径/位置作为其值由单个空格分隔。文本文件如下 -

index1.html /htdocs/abc/htmls/
english.war /tomcat/webapps/
index10.html /htdocs/abc/home/auto/var/

文本文件如上所示。我想用nsis阅读它,我想将index1.html或english.war等等文件保存到该文件名旁边的位置。这里的键是filename,它的值是它的路径。谁能告诉我如何阅读这个文本文件作为关键值以及如何将它放在给定的路径上?

1 个答案:

答案 0 :(得分:2)

那些不是Windows路径,所以我真的不知道你希望如何将某些内容复制到这些位置,但解析文件可以这样做:

Function StrSplitOne
Exch $0 ; Separator
Exch 
Exch $1 ; String
Push $2
Push $3
StrCpy $2 0
loop:
    StrCpy $3 $1 1 $2
    IntOp $2 $2 + 1
    StrCmp $3 "" +3
    StrCmp $3 $0 0 loop
    IntOp $2 $2 - 1
    StrCpy $0 $1 $2
    IntOp $2 $2 + 1
    StrCpy $1 $1 "" $2
Pop $3
Pop $2
Exch $1 ; Remaining
Exch
Exch $0 ; Item
FunctionEnd

Section 
; Create config file for testing
InitPluginsDir
FileOpen $0 "$PluginsDir\test.txt" w
FileWrite $0 "index1.html /htdocs/abc/htmls/$\r$\n"
FileWrite $0 "english.war /tomcat/webapps/$\r$\n"
FileWrite $0 "index10.html /htdocs/abc/home/auto/var/$\r$\n"
FileClose $0
SectionEnd

Section
; Read and parse config file
FileOpen $0 "$PluginsDir\test.txt" r
loop:
    FileRead $0 $1
    StrCmp $1 '' eof
findnewline:
    StrCpy $2 $1 1 -1
    StrCmp $2 '$\r' killnewline
    StrCmp $2 '$\n' killnewline split
killnewline:
    StrCpy $1 $1 -1
    Goto findnewline
split:
    Push $1
    Push ' '
    Call StrSplitOne
    Pop $2
    Pop $3
    DetailPrint 'Use CopyFiles to copy "?:\$2" to "?:$3"...'
    Goto loop
eof:
FileClose $0
SectionEnd