Jump to content

Recursive Searching....


Recommended Posts

Hello All,

I've been reviewing this excellent Recursive Search code found here: hxxp://www.autoitscript.com/forum/index.php?showtopic=42049

For some reason, when I get the array back, it has stripped out the '\' within the full path + file name.

So instead of

d:\myfolder\mysubfolder\myfile.txt

I get

d:myfoldermysubfoldermyfile.txt

is there anyway for me to get back that critical \ in the path, I want to take this array, open these files and edit them one by one with the full path.

thanks!

CODE

#cs ----------------------------------------------------------------------------

Syntax: File_Search($szRoot, [$Szmask = "*.*"[, $nFlag = 1[, $nOcc = 0]]])

Parameters: $szRoot = the folder to start searching from

$Szmask = The file mask (Default *.* returns all)

$nFlag (Optional) Default is 7 Can be added as follows

1 = Recursive Search

2 = Path and Filename

4 = Sorted array

$nOcc (Optional) - 1 = Return only the first matching file, anything else = Return all matching files (Default)

Requirements: Array.au3 for _ArraySort function

Return Values: On Success - Returns an array of the file names

On Failure - Returns 0

Modifications: Changed return value to an array

Now excepts an array or a '|' separated list to use multiple file masks

Accepts flags to return full path and/or sort the array

Note If sorting is not used then you do not have to #include array.au3

Example: $var = _FileSearch("C:\", "*.vbs|*.js") will return a sorted array of all the vbs and js files (including path) on C:\ drive

#ce ----------------------------------------------------------------------------

#include <Array.au3>

;Variables

Dim $dirArray[1]

$pathdata = "D:\myfolder\mysubfolder\"

$filename = "myfile.txt"

Func _FileSearch($szRoot, $Szmask = "*.*", $nFlag = 7, $nOcc = 0)

Local $hArray = $Szmask, $iRec, $iPath, $iSort

Switch $nFlag

Case 1

$iRec = 1

$iPath = 0

$iSort = 0

Case 2

$iRec = 0

$iPath = 1

$iSort = 0

Case 3

$iRec = 1

$iPath = 1

$iSort = 0

Case 4

$iRec = 0

$iPath = 0

$iSort = 1

Case 5

$iRec = 1

$iPath = 0

$iSort = 1

Case 6

$iRec = 0

$iPath = 1

$iSort = 1

Case Else

$iRec = 1

$iPath = 1

$iSort = 1

EndSwitch

If NOT IsArray($hArray) Then $hArray = StringSplit($hArray, '|')

Local $Hfile = 0, $F_List = ''

$szBuffer = ""

$szReturn = ""

$szPathlist = "*"

For $I = 1 To Ubound($hArray)-1

$szMask = $hArray[$I]

If NOT StringInStr ($Szmask, "\") Then

$szRoot &= ''

Else

$iTrim = StringInStr ($Szmask, "\",0,-1)

$szRoot &= StringLeft ($Szmask, $iTrim)

$Szmask = StringTrimLeft ($Szmask, $iTrim)

EndIf

If $iRec = 0 Then

$Hfile = FileFindFirstFile ($szRoot & $szMask)

If $Hfile >= 0 Then

$szBuffer = FileFindNextFile ($Hfile)

While NOT @Error

If $iPath = 1 Then $szReturn &= $szRoot

If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*"

$szBuffer = FileFindNextFile ($Hfile)

Wend

FileClose ($Hfile)

EndIf

Else

While 1

$Hfile = FileFindFirstFile ($szRoot & "*.*")

If $Hfile >= 0 Then

$szBuffer = FileFindNextFile ($Hfile)

While NOT @Error

If $szBuffer <> "." AND $szBuffer <> ".." AND StringInStr (FileGetAttrib ($szRoot & $szBuffer), "D") Then _

$szPathlist &= $szRoot & $szBuffer & "*"

$szBuffer = FileFindNextFile ($Hfile)

Wend

FileClose ($Hfile)

EndIf

If StringInStr ($szReturn, $Szmask) > 0 AND $nOcc = 1 Then

$szRoot = ''

ExitLoop

EndIf

$Hfile = FileFindFirstFile ($szRoot & $szMask)

If $Hfile >= 0 Then

$szBuffer = FileFindNextFile ($Hfile)

While NOT @Error

If $iPath = 1 Then $szReturn &= $szRoot

If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*"

$szBuffer = FileFindNextFile ($Hfile)

Wend

FileClose ($Hfile)

EndIf

If $szPathlist == "*" Then ExitLoop

$szPathlist = StringTrimLeft ($szPathlist, 1)

$szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\"

$szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1)

Wend

EndIf

Next

If $szReturn = "" Then

Return 0

Else

$szReturn = StringReplace($szReturn,'\', '')

$F_List = StringSplit (StringTrimRight ($szReturn, 1), "*")

If $iSort = 1 Then _ArraySort($F_List)

Return $F_List

EndIf

EndFunc ;<===> _FileSearch()

$DirArray = _FileSearch($pathdata, $filename, 3)

_ArrayDisplay($DirArray, "Files Located In....")

thanks!

Link to comment
Share on other sites

Well, I may have found my own answer- I knew I shouldn't have posted so quick :rolleyes:

Down, in the return section of the function, I commented out this line, and the array now returns with the appropriate \ within it.

I will leave this post so this may help the next "fast poster"

;$szReturn = StringReplace($szReturn,'\', '')

as always, thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...