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

From NSIS Wiki
Jump to navigationJump to search
m (Updated author and download links, and changed format of some pages.)
(→‎The Function: Have to push the dir to search in before nextDir, otherwise worst case scenario: opcode errors may result, best case: search broken)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{PageAuthor|Afrow UK}}
== 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 find's 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.


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!
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!
Line 10: Line 12:
</highlight-nsis>
</highlight-nsis>


'''Note:''' Don't have a leading back-stroke (\) on end of "C:\look_in_here" path, else you will get a path like:
'''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"
"C:\look_in_here\\find_this.txt"


== The Function ==
== The Function ==
Line 32: Line 34:
Push $R5
Push $R5
Push $R6
Push $R6
Push $R1


  StrCpy $R6 -1
  StrCpy $R6 -1
  StrCpy $R3 1
  StrCpy $R3 1
Push $R1


  nextDir:
  nextDir:
Line 56: Line 59:


  isDir:
  isDir:
   IfFileExists "$R1\$R2\*.*" 0 gotoNextFile
   IfFileExists "$R1\$R2\*.*" 0 gotoNextFile
   IntOp $R3 $R3 + 1
   IntOp $R3 $R3 + 1
Line 81: Line 85:
-Stu
-Stu


Page author: [[User:Afrow UK|Afrow UK]]
[[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