2014-10-18 76 views
1

我想追加1行文本到$ APPDATA文件夹中的一个文件,这是随机生成的文件夹内,所以我不知道它的完整路径像:NSIS写入一个子目录中的APPDATA上的文件

C:\Users\MyUser\AppData\Roaming\MyApp\RANDOM_CRAP\config.json 

虽然RANDOM_CRAP看起来有些随机字符串的文件夹,如G4F6Hh3L

我在这里有什么选择?我需要使用Search For a File还是Search for a File or Directory (Alternative)这是给定MyApp文件夹的唯一子文件夹是RANDOM_CRAP文件夹,它包含我想要编辑的文件。

如果没有其他的方式来访问这个文件没有搜索它,我试过这样做,但无法得到这个工作。 (我很新的NSIS)

这是我试过(通过另一种方法):

Push "config.json" 
Push "$APPDATA" 
Push $0 
GetFunctionAddress $0 "myCallback" 
Exch $0 
Push "1" ; include subfolders because my desired file is in the random folder 
Push "0" ; no need the . option 
Call SearchFile 

比我复制了SearchFile code from this post,把一个回调:

Function myCallback 
    Exch 3 
    Pop $R4 
    MessageBox MB_OK "Callback executing!" 
    MessageBox MB_OK "File is at : $R4" 
FunctionEnd 

我知道SearchFile正在运行(我把一个MessageBox里面),但myCallback似乎并没有被调用。

非常感谢。

回答

1

如果你正在寻找一个已知的文件,只有一个路径目录是未知的,那么你可能只需要做一个基本的FindFirst搜索:

Section 
; Create "random" folders: 
CreateDirectory "$temp\MyApp\foo" 
System::Call kernel32::GetTickCount()i.r1 ; random enough 
CreateDirectory "$temp\MyApp\bar$1" 
FileOpen $0 "$temp\MyApp\bar$1\config.json" a 
FileWrite $0 '{bogus:"data"}$\n' 
FileClose $0 
CreateDirectory "$temp\MyApp\baz" 

!include LogicLib.nsh 
; Do the actual search: 
StrCpy $9 "$temp\MyApp" ; The folder we are going to search in 
FindFirst $0 $1 "$temp\MyApp\*" 
loop: 
    StrCmp $1 "" done 
    ${If} ${FileExists} "$9\$1\config.json" 
     DetailPrint "Found: $9\$1\config.json" 
    ${EndIf} 
    FindNext $0 $1 
    Goto loop 
done: 
FindClose $0 
SectionEnd 
相关问题