Include/RecFind.nsh
changeset 0 d5ce4c64ef88
equal deleted inserted replaced
-1:000000000000 0:d5ce4c64ef88
       
     1 #################################################################################################
       
     2 # RecFind.nsh - Recursive FindFirst, FindNext, FindClose.
       
     3 #  by Afrow UK
       
     4 #
       
     5 # Last modified 5th July 2005
       
     6 
       
     7 ; Usage:
       
     8 ; ------------------------------------------------------------
       
     9 ; ${RecFindOpen} "Dir\Path" $CurrentDirVar $CurrentFileVar
       
    10 ;  ... Do stuff with $CurrentDirVar ...
       
    11 ; ${RecFindFirst}
       
    12 ;  ... Do stuff with $CurrentFileVar ...
       
    13 ; ${RecFindNext}
       
    14 ; ${RecFindClose}
       
    15 
       
    16 ; Notes:
       
    17 ; ------------------------------------------------------------
       
    18 
       
    19 ; Looping is handled by the macro's internally.
       
    20 
       
    21 ; ${RecFindOpen} opens a search in a new directory in the tree.
       
    22 ;  The macro's will loop back to this instruction when a new
       
    23 ;  directory is opened for searching.
       
    24 
       
    25 ; ${RecFindFirst} gets file names out of the current directory.
       
    26 ;  The macro's will loop back to this instruction when a new file
       
    27 ;  is found.
       
    28 
       
    29 ; ${RecFindNext} gets the next file in the current directory, and loops to
       
    30 ;  ${RecFindFirst} again.
       
    31 
       
    32 ; ${RecFindClose} closes the search and clears the stack.
       
    33 
       
    34 ; Example 1:
       
    35 ; ------------------------------------------------------------
       
    36 ; ${RecFindOpen} "C:\Dir" $R0 $R1
       
    37 ;  DetailPrint "Dir: C:\Dir$R0"
       
    38 ; ${RecFindFirst}
       
    39 ;  DetailPrint "File: C:\Dir$R0\$R1"
       
    40 ; ${RecFindNext}
       
    41 ; ${RecFindClose}
       
    42 
       
    43 ; Example 2:
       
    44 ; ------------------------------------------------------------
       
    45 ; ${RecFindOpen} "C:\Dir" $R0 $R1
       
    46 ;  DetailPrint "Dir: C:\Dir$R0"
       
    47 ; ${RecFindFirst}
       
    48 ;  DetailPrint "File: C:\Dir$R0\$R1"
       
    49 ;  StrCmp $R1 "a_file.txt" Found
       
    50 ; ${RecFindNext}
       
    51 ;  Found:
       
    52 ; ${RecFindClose}
       
    53 
       
    54 #################################################################################################
       
    55 
       
    56 !ifndef RecFind-Included
       
    57 !define RecFind-Included
       
    58 
       
    59 Var RecFindVar1
       
    60 Var RecFindVar2
       
    61 
       
    62 !macro RecFindOpen DirVar CurrentDirVar CurrentFileVar
       
    63 
       
    64  !define Local       "${__LINE__}"
       
    65  !define Dir         "${DirVar}"
       
    66  !define CurrentDir  "${CurrentDirVar}"
       
    67  !define CurrentFile "${CurrentFileVar}"
       
    68 
       
    69   !define RecFindOpenSet
       
    70 
       
    71  StrCpy $RecFindVar2 1
       
    72  Push ""
       
    73 
       
    74  "nextDir${Local}:"
       
    75  Pop "${CurrentDir}"
       
    76  IntOp $RecFindVar2 $RecFindVar2 - 1
       
    77 
       
    78 !macroend
       
    79 !define RecFindOpen "!insertmacro RecFindOpen"
       
    80 
       
    81 !macro RecFindFirst
       
    82 
       
    83  !ifndef RecFindOpenSet
       
    84   !error "Incorrect use of RecFind commands!"
       
    85  !else
       
    86   !define RecFindFirstSet
       
    87  !endif
       
    88 
       
    89  ClearErrors
       
    90  FindFirst $RecFindVar1 "${CurrentFile}" "${Dir}${CurrentDir}\*.*"
       
    91  IfErrors "Done${Local}"
       
    92 
       
    93   "checkFile${Local}:"
       
    94   StrCmp ${CurrentFile} .  "nextFile${Local}"
       
    95   StrCmp ${CurrentFile} .. "nextFile${Local}"
       
    96 
       
    97   IfFileExists "${Dir}${CurrentDir}\${CurrentFile}\*.*" 0 +4
       
    98    Push "${CurrentDir}\${CurrentFile}"
       
    99    IntOp $RecFindVar2 $RecFindVar2 + 1
       
   100     Goto "nextFile${Local}"
       
   101 
       
   102 !macroend
       
   103 !define RecFindFirst "!insertmacro RecFindFirst"
       
   104 
       
   105 !macro RecFindNext
       
   106 
       
   107  !ifndef RecFindOpenSet | RecFindFirstSet
       
   108   !error "Incorrect use of RecFind commands!"
       
   109  !else
       
   110   !define RecFindNextSet
       
   111  !endif
       
   112 
       
   113  "nextFile${Local}:"
       
   114 
       
   115  ClearErrors
       
   116  FindNext $RecFindVar1 "${CurrentFile}"
       
   117  IfErrors 0 "checkFile${Local}"
       
   118 
       
   119  StrCmp $RecFindVar2 0 0 "nextDir${Local}"
       
   120 !macroend
       
   121 !define RecFindNext "!insertmacro RecFindNext"
       
   122 
       
   123 !macro RecFindClose
       
   124 
       
   125  !ifndef RecFindOpenSet | RecFindFirstSet | RecFindNextSet
       
   126   !error "Incorrect use of RecFind commands!"
       
   127  !else
       
   128   !undef RecFindOpenSet
       
   129   !undef RecFindFirstSet
       
   130   !undef RecFindNextSet
       
   131  !endif
       
   132 
       
   133  "Done${Local}:"
       
   134  FindClose $RecFindVar1
       
   135 
       
   136  StrCmp $RecFindVar2 0 +4
       
   137   Pop $RecFindVar1
       
   138   IntOp $RecFindVar2 $RecFindVar2 - 1
       
   139   Goto -3
       
   140 
       
   141  !undef CurrentFile
       
   142  !undef CurrentDir
       
   143  !undef Dir
       
   144  !undef Local
       
   145 
       
   146 !macroend
       
   147 !define RecFindClose "!insertmacro RecFindClose"
       
   148 
       
   149 !endif