dasein
8th April 2004 20:42 UTC
Directory Listing in NSIS
  Hello,
Is there a way to get a directory listing into a variable? There is a random directory generated by Thunderbird's createprofile option that I need to install some files into.
I've tried to use this line:
nsExec::Exec 'dir "$APPDATA\Thunderbird\Profiles\default"'
but this gives me "error" in the stack.
Is there some easier way to do this?
Thanks!
Joost Verburg
8th April 2004 21:13 UTC
To enumerate directories you have to use FindFirst, FindNext and FindClose.
dasein
8th April 2004 21:42 UTC
Ah... Thank you very much. I didn't know the Find commands included directories and not just files.
Thanks again.
Brummelchen
26th June 2006 04:10 UTC
very old question, i needed that hint, here the result
findfile0
  Delete "$EXEDIR\\foldlist.ini"
 Delete "$EXEDIR\\filelist.ini"
 StrCpy $R7 "1"
 StrCpy $R8 "0"
 StrCpy $R9 "0"
 StrCpy $SEARCHDIR $EXEDIR ; <-- modify directory (from input or fixed)
  Push $SEARCHDIR
  Call findfile2
  Call findfile1
FunctionEnd
>Function findfile1
findfile1_loop:
 ReadINIStr $3 "$EXEDIR\\foldlist.ini" subdirs $R7
  StrCmp$3 "" findfile1_done
    Push$3
    Call findfile2
    IntOp $R7 $R7+ 1
    IntCmp $R7 $R8 0 0 findfile1_done
  Goto findfile1_loop
findfile1_done:
>FunctionEnd
>Function findfile2
Pop$2
FindFirst$0 $1 "$2\\*.*"
>findfile2_loop:
 StrCmp $1 "" findfile2_done
  StrCmp$1 "." findfile2_loop02
  StrCmp$1 ".." findfile2_loop02
  IfFileExists"$2\\$1\\*.*" 0 findfile2_loop01
    Call subdir
    Goto findfile2_loop02
  findfile2_loop01:
  Call filewrite
  findfile2_loop02:
 FindNext $0 $1
  Goto findfile2_loop
findfile2_done:
>FunctionEnd
>Function subdir
  IntOp $R8 $R8+ 1
  WriteINIStr"$EXEDIR\\foldlist.ini" subdirs $R8 $2$1
FunctionEnd
>Function filewrite
  IntOp $R9 $R9+ 1
  WriteINIStr"$EXEDIR\\filelist.ini" filelist $R9 $2$1
FunctionEnd 
>
The idea behind:
      - search starting folder and grab all files (filelist.ini)
      - write subfolders into foldlist.ini
      - then readout foldlist.ini and grab files and folders again
      
      $R9 is filelist-pointer
      $R8 is folderlist-pointer
      $R7 is helper-pointer
      
      $R8 raises and when $R7 > $R8 (or $3 is empty) the end of the folder list was reached.
      
      modify function "filewrite" as you wish (other output format eg.)
      
      HTH