FindIt: Simple search for file / directory: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated by user: Afrow UK (talk, contrib).)
(→‎The Function: Have to push the dir to search in before nextDir, otherwise worst case scenario: opcode errors may result, best case: search broken)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{|align=right
{{PageAuthor|Afrow UK}}
|<small>Author: [[{{ns:2}}:Afrow UK|Afrow UK]] ([[{{ns:3}}:Afrow UK|talk]], [[{{ns:-1}}:Contributions/Afrow UK|contrib]])</small>
 
|}
<br style="clear:both;">
== Description ==
== Description ==
A lot of new people found it hard to understand how to use the other file search functions, so I decided to post my version (which is a modified version of [[user:Kichik|Kichik]]'s). It doesn't require a callback function. It simply finds a file or directory, or it doesn't.
A lot of new people found it hard to understand how to use the other file search functions, so I decided to post my version (which is a modified version of [[user:Kichik|Kichik]]'s). It doesn't require a callback function. It simply finds a file or directory, or it doesn't.
Line 39: Line 37:
  StrCpy $R6 -1
  StrCpy $R6 -1
  StrCpy $R3 1
  StrCpy $R3 1
Push $R1


  nextDir:
  nextDir:
Line 85: Line 85:
-Stu
-Stu


[[{{ns:14}}:Disk, Path & File Functions]]
[[Category:Disk, Path & File Functions]]

Latest revision as of 02:02, 13 February 2009

Author: Afrow UK (talk, contrib)


Description

A lot of new people found it hard to understand how to use the other file search functions, so I decided to post my version (which is a modified version of Kichik's). It doesn't require a callback function. It simply finds a file or directory, or it doesn't.

The only downside to using this function though is that if the file that you are locating does not have a unique name, then you could come up with another file or directory path instead!

Usage

${FindIt} "C:\look_in_here" "find_this.txt" "$R0"
; At this point; $R0 is path to "find_this.txt" or -1 (not found)

Note: Don't put a leading back-stroke (\) on end of "C:\look_in_here" path, else you will get a path like:

"C:\look_in_here\\find_this.txt"

The Function

!macro FindIt In For Result
Push "${In}"
Push "${For}"
 Call FindIt
Pop "${Result}"
!macroend
!define FindIt "!insertmacro FindIt"
 
Function FindIt
Exch $R0
Exch
Exch $R1
Push $R2
Push $R3
Push $R4
Push $R5
Push $R6
 
 StrCpy $R6 -1
 StrCpy $R3 1
 
 Push $R1
 
 nextDir:
  Pop $R1
  IntOp $R3 $R3 - 1
  ClearErrors
   FindFirst $R5 $R2 "$R1\*.*"
 
 nextFile:
  StrCmp $R2 "." gotoNextFile
  StrCmp $R2 ".." gotoNextFile
 
  StrCmp $R2 $R0 0 isDir
   StrCpy $R6 "$R1\$R2"
   loop:
    StrCmp $R3 0 done
     Pop $R1
     IntOp $R3 $R3 - 1
     Goto loop
 
 isDir:
 
  IfFileExists "$R1\$R2\*.*" 0 gotoNextFile
  IntOp $R3 $R3 + 1
  Push "$R1\$R2"
 
 gotoNextFile:
  FindNext $R5 $R2
  IfErrors 0 nextFile
 
 done:
  FindClose $R5
  StrCmp $R3 0 0 nextDir
  StrCpy $R0 $R6
 
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

-Stu