Fractured

search for a file in 2 different directories

20 posts in this topic

#1 ·  Posted

So...I looked at _FileListToArrayRec and I realized I do not want to do a recursive search. The directory structure would of where i would be searching has over 200000 files and around 75 gigs of data. SO my question is can I use a "If..Then...Else" style setup to search one directory, if its not found there switch to a second directory, and if not found there error to file not found?

Or would there be a quicker way? Ive been playing with the code from my earlier Drawing Vault project and have been trying different ideas...Im at home now so I dont have access to the directories to test, but am looking for ideas for tomorrow....

Thanks in advance if anyone wants to throw some ideas at me!

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

If you have only two potential directories, a simple If..then...elseif seems easiest:

Local $firstDir = @WindowsDir & "\AppPatch"
Local $secondDir = @WindowsDir & "\Boot"

    If FileExists($firstDir & "\MyFile.txt") Then
        ;do Thing 1
    ElseIf FileExists($secondDir & "\MyFile.txt") Then
        ;do Thing 2
    Else
        ;error out
    EndIf

If you believe it will grow to encompass multiple directories, then an array of locations seems more suitable.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted

Sounds good. Just got to work and ill try the if..then,,elseif. I dont believe it will grow to more directories, the company has been pretty rigid on where they put documentation. Alas, they just have alot of it!

Share this post


Link to post
Share on other sites

#4 ·  Posted

So it seems that is working except for the search is messing up the user input...if I enter 1062...it seems to only want to use the 6 and none of the other numbers...

But I was wondering what the "" ? "" actually does in this line :

Case $sdATPSearch
    GUICtrlSetData($sdATPList, GUICtrlRead($sdATPSearch) = "" ? "" : _ATPS(GUICtrlRead($sdATPSearch)))

I read the GUICtrlSetData info and the GUICtrlRead info but the "" ? "" is not really explained....

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Fractured,

That is a Ternary operator - look it up in the Help file for more details. Personally I always add parentheses to make the syntax a little clearer:

;                            If this statement is true          Use this     If not then use this
 GUICtrlSetData($sdATPList, ( (GUICtrlRead($sdATPSearch) = "") ? ("")       : (_ATPS(GUICtrlRead($sdATPSearch))) ) )

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Quick question. Once you do a ArraytoString, can you use StringinStr to display only the files with your string in them? Im thinking not because of the formating once the array is returned, but i figured id still pose the question. 

 

Thanks!

 

Share this post


Link to post
Share on other sites

#7 ·  Posted

@Fractured If you already have the array, you can save yourself some headache by searching through it, rather than outputting to a String and then doing your search. Something like this with a second array, perhaps:

#include <Array.au3>

Local $aArray = ["May2015.txt", "March2015.txt", "January2016.txt", "January2017.txt", "December2015.txt"]
Local $aFinal[0]

    For $sEntry In $aArray
        If StringInStr($sEntry, "2015.txt") Then _ArrayAdd($aFinal, $sEntry)
    Next

    _ArrayDisplay($aFinal)

 


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#8 ·  Posted

Nice! Thanks!

Share this post


Link to post
Share on other sites

#9 ·  Posted

Im at a complete loss now....I know the macro is finding the files by the _DisplayArray test lines I have...but since it is only finding the file in one source folder it puts up the "Path not found or invalid" msgbox for the other source folder then ends....

The 1st problem is the file could be in either or both source folders.....Im not really sure what to use at this point to get past this area of code...

The 2nd problem is I need to merge $aFileLista and $aFileListb. To make it simpler for myself I figured I could convert them to strings then just c = a & b...but I cant get passed the 1st problem to see if this would actually work!

Here is my code so far.....

;#INDEX# =============================================================================================================================
; Title .........: Doc Rel/Rev Arc Search
; AutoIt Version : 3.3.10.2
; Language ......: English
; Author(s) .....: Charles Wright
; Modifiers .....:
; Forum link ....:
; Description ...: Searches for documents relevant to Test
; =====================================================================================================================================


; #Includes# ==========================================================================================================================
; Needed .au3 Files for the script/program to run
; =====================================================================================================================================
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Array.au3>
#include <File.au3>
; End Includes ========================================================================================================================


; Variables for File Locations ========================================================================================================
;======================================================================================================================================
Global $g_sFilePatha = "S:\Drawing Vault\DOC REL\"
Global $g_sFilePathb = "S:\Drawing Vault\DWG VAULT\rev arc\"
; End Variables========================================================================================================================


; Region ### START Koda GUI section ### Form=c:\program files\autoit3\scite\forms\filesearch.kxf ======================================
;======================================================================================================================================
$Search = GUICreate("Doc Rel / Rev Arc Search v1.2", 840, 138, 1655, 291)                       ;Main Window
GUISetBkColor(0x99B4D1)
$sdDWG = GUICtrlCreateInput("", 24, 16, 121, 21)                                    ;User Input
$sdSearch = GUICtrlCreateButton("Search", 48, 40, 75, 25)                           ;Search Button
GUICtrlSetBkColor(-1, 0xFFFBF0)
$sdReturned = GUICtrlCreateList("", 152, 16, 665, 84)                               ;List Window for Array
$sdSelect = GUICtrlCreateButton("Select", 440, 104, 75, 25)
GUICtrlSetBkColor(-1, 0xFFFBF0)
GUISetState(@SW_SHOW)
;#EndRegion ### END Koda GUI section ###===============================================================================================


; #Main# ==============================================================================================================================
; Main Script / Form Control
;======================================================================================================================================
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

         Case $GUI_EVENT_CLOSE
            Exit

         Case $sdSearch
            GUICtrlSetData($sdReturned, GUICtrlRead($sdDWG) = "" ? "" : _DRS(GUICtrlRead($sdDWG)))

         Case $sdSelect
            Local $sResult = GUICtrlRead($sdReturned)
            ShellExecute($sResult)

    EndSwitch
 WEnd
 ; End Main ===========================================================================================================================


; #Function# ==========================================================================================================================
; Doc Rel Search Function
;======================================================================================================================================
Func _DRS($sdSearch = "ATP")
   GUICtrlSetData($sdReturned,"")                                                                                   ;Sets Data from the string array

   Local $sSourceFoldera = $g_sFilePatha
   Local $sSourceFolderb = $g_sFilePathb

      If FileExists($sSourceFoldera) = 0 OR FileExists($sSourceFolderb) = 0 Then
         MsgBox(4096, "File Path Error", "Folder" & " does not exist.")
         Return ""
      EndIf

   Local $sSearchMask = $sdSearch & "*.*"

      Local $aFileLista = _FileListToArrayRec($sSourceFoldera, $sSearchMask & "*|*.cdr ; *.bak", 1, 1, 1, 2)
      _ArrayDisplay($aFileLista)

      Local $aFileListb = _FileListToArrayRec($sSourceFolderb, $sSearchMask & "*|*.cdr ; *.bak", 1, 1, 1, 2)
      _ArrayDisplay($aFileListb)
; End ATP Search Fuction ==============================================================================================================


; #Error Codes# =======================================================================================================================
;======================================================================================================================================
      Switch @error
         Case 1
            MsgBox(4096, "File List To Array Error", "Path not found or invalid")
            Return ""
         Case 2
            MsgBox(4096, "File List To Array Error", "Invalid Include parameter")
            Return ""
         Case 3
            MsgBox(4096, "File List To Array Error", "Invalid Exclude parameter")
            Return ""
         Case 4
            MsgBox(4096, "File List To Array Error", "Invalid Exclude_Folders parameter")
            Return ""
         Case 5
            MsgBox(4096, "File List To Array Error", "Invalid $iReturn parameter")
            Return ""
         Case 6
            MsgBox(4096, "File List To Array Error", "Invalid $iRecur parameter")
            Return ""
         Case 7
            MsgBox(4096, "File List To Array Error", "Invalid $iSort parameter")
            Return ""
         Case 8
            MsgBox(4096, "File List To Array Error", "Invalid $iReturnPath parameter")
            Return ""
         Case 9
            MsgBox(4096, "File List To Array Error", "No files/folders found")
            Return ""
         EndSwitch
;End Error Codes ======================================================================================================================

   Local $sFileLista = _ArrayToString($aFileLista, "|", 1)
      MsgBox(4096, "Test 1", $sFileLista)
   Local $sFileListb = _ArrayToString($aFileLista, "|", 1)
      MsgBox(4096, "Test 2", $sFileListb)
   Local $sFileListc = $sFileLista & $sFileListb
      MsgBox(4096, "Test 3", $sFileListc)
   Return $sFileListc

   ;Return _ArrayToString($aFileLista, "|", 1)                                                                          ;Returns Array data as a string

 EndFunc

Abuse away!! 

Share this post


Link to post
Share on other sites

#10 ·  Posted

@Fractured I am going through your script a little at a time as I have a few minutes. To fix the first issue of the MsgBox, and this is just my personal preference, rather than something like this:

If FileExists($sSourceFoldera) = 0 OR FileExists($sSourceFolderb) = 0 Then

I would write it like this:

If Not (FileExists($sSourceFoldera)) And Not (FileExists($sSourceFolderb)) Then ;only barfs if both are non-existent

Or, with an Or:

If Not (FileExists($sSourceFoldera)) OR Not (FileExists($sSourceFolderb)) Then

 

Secondly, both directories you are searching are children of the main "S:\Drawing Vault" folder. Rather than trying to do a _FileListToArrayRec on both individually and then combine, why not just move up one level, pull your array from the Drawing Vault folder?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#11 ·  Posted

I was going to do that but figured it was faster to drill down to the 2 directories themselves instead of do a bulk search. The Drawing Vault folder + its kiddos has a hugh amount of files...really hugh...

Ill switch over to the "And Not" condition and see how that goes.

Thanks! 

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

Ok, that makes more sense if the main folder is monstrous. I missed that if you mentioned earlier.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Forgot to mention that...

Switched to the new condition statement and it definitly see's the file and dosent barf...

Now at the bottom when I try to assign $sFilelist = _ArrayToString....the Test boxes show -1, which im assuming is the error code that they are not Arrays...although in the _ArrayDisplay tests it shows as am array....

That seems to be the last hurdle.....Im not clear on the _ArrayAdd command, hence the attempt to String the Arrays and then add the Strings...

Still reading through the help file and forum!!

Edited by Fractured

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Weird...If it finds the file in only say Rev Arc....it returns the -1's......If it finds the file in Doc Rel and Rev Arc it returns the _ArrayDisplay tests correctly and adds the strings into the one big string..which is good since it shows its working! So I need it to behave if the file is only in one of the source folders....

 

***No File = No Array*** = error code = could it be that simple???? 

Edited by Fractured

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

The @error code returned is for _ArrayDisplay so you would need to use something like:

;#INDEX# =============================================================================================================================
; Title .........: Doc Rel/Rev Arc Search
; AutoIt Version : 3.3.10.2
; Language ......: English
; Author(s) .....: Charles Wright
; Modifiers .....:
; Forum link ....:
; Description ...: Searches for documents relevant to Test
; =====================================================================================================================================


; #Includes# ==========================================================================================================================
; Needed .au3 Files for the script/program to run
; =====================================================================================================================================
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <Array.au3>
#include <File.au3>
; End Includes ========================================================================================================================


; Variables for File Locations ========================================================================================================
;======================================================================================================================================
Global $g_sFilePatha = "E:\Drawing Vault\DOC REL\"
Global $g_sFilePathb = "E:\Drawing Vault\DWG VAULT\rev arc\"
; End Variables========================================================================================================================


; Region ### START Koda GUI section ### Form=c:\program files\autoit3\scite\forms\filesearch.kxf ======================================
;======================================================================================================================================
$Search = GUICreate("Doc Rel / Rev Arc Search v1.2", 840, 138, 1655, 291)                       ;Main Window
GUISetBkColor(0x99B4D1)
$sdDWG = GUICtrlCreateInput("", 24, 16, 121, 21)                                    ;User Input
$sdSearch = GUICtrlCreateButton("Search", 48, 40, 75, 25)                           ;Search Button
GUICtrlSetBkColor(-1, 0xFFFBF0)
$sdReturned = GUICtrlCreateList("", 152, 16, 665, 84)                               ;List Window for Array
$sdSelect = GUICtrlCreateButton("Select", 440, 104, 75, 25)
GUICtrlSetBkColor(-1, 0xFFFBF0)
GUISetState(@SW_SHOW)
;#EndRegion ### END Koda GUI section ###===============================================================================================


; #Main# ==============================================================================================================================
; Main Script / Form Control
;======================================================================================================================================
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

         Case $GUI_EVENT_CLOSE
            Exit

         Case $sdSearch
            GUICtrlSetData($sdReturned, GUICtrlRead($sdDWG) = "" ? "" : _DRS(GUICtrlRead($sdDWG)))

         Case $sdSelect
            Local $sResult = GUICtrlRead($sdReturned)
            ShellExecute($sResult)

    EndSwitch
 WEnd
 ; End Main ===========================================================================================================================


; #Function# ==========================================================================================================================
; Doc Rel Search Function
;======================================================================================================================================
Func _DRS($sdSearch = "ATP")
    Local $aFileList[0], $aFileLista, $aFileListb
   GUICtrlSetData($sdReturned,"")                                                                                   ;Sets Data from the string array

   Local $sSourceFoldera = $g_sFilePatha
   Local $sSourceFolderb = $g_sFilePathb

      If FileExists($sSourceFoldera) = 0 OR FileExists($sSourceFolderb) = 0 Then
         MsgBox(4096, "File Path Error", "Folder" & " does not exist.")
         Return ""
      EndIf

   Local $sSearchMask = $sdSearch & "*.*"

    $aFileLista = _FileListToArrayRec($sSourceFoldera, $sSearchMask & "*|*.cdr ; *.bak", 1, 1, 1, 2)
        If _ErrorCodes(@error) Then Local $aFileLista[0]
        _ArrayDisplay($aFileLista)

    Local $aFileListb = _FileListToArrayRec($sSourceFolderb, $sSearchMask & "*|*.cdr ; *.bak", 1, 1, 1, 2)
        If _ErrorCodes(@error) Then Local $aFileListb[0]
        _ArrayDisplay($aFileListb)
; End ATP Search Fuction ==============================================================================================================

    _ArrayConcatenate($aFileList, $aFileLista, 1)
    _ArrayConcatenate($aFileList, $aFileListb, 1)
    Return _ArrayToString($aFileList, "|")  ;Returns Array data as a string
EndFunc

Func _ErrorCodes($iErrorCode = 0)
; #Error Codes# =======================================================================================================================
;======================================================================================================================================
    Switch $iErrorCode
        Case 1
            MsgBox(4096, "File List To Array Error", "Path not found or invalid")
            Return 1
        Case 2
            MsgBox(4096, "File List To Array Error", "Invalid Include parameter")
            Return 1
        Case 3
            MsgBox(4096, "File List To Array Error", "Invalid Exclude parameter")
            Return 1
        Case 4
            MsgBox(4096, "File List To Array Error", "Invalid Exclude_Folders parameter")
            Return 1
        Case 5
            MsgBox(4096, "File List To Array Error", "Invalid $iReturn parameter")
            Return 1
        Case 6
            MsgBox(4096, "File List To Array Error", "Invalid $iRecur parameter")
            Return 1
        Case 7
            MsgBox(4096, "File List To Array Error", "Invalid $iSort parameter")
            Return 1
        Case 8
            MsgBox(4096, "File List To Array Error", "Invalid $iReturnPath parameter")
            Return 1
        Case 9
            MsgBox(4096, "File List To Array Error", "No files/folders found")
            Return 1
        Case Else
            Return 0
    EndSwitch
;End Error Codes ======================================================================================================================
EndFunc

 

Edited by Subz

Share this post


Link to post
Share on other sites

#16 ·  Posted

Above are complex solutions I would say

  • use PowerShell for finding files (even in 200.000 files its not that bad)
get-childitem -Recurse C:\*.* -Filter *.xls | sort LastWriteTime -descending | format-table LastWriteTime, Length, Name, Directory -AutoSize | Out-String -Width 1024  >> result.txt

And in your questions its not completely clear if it are allways the same 2 filenames or that they can differ.

  • If it are 2 fixed files then bookmark them in your explorer.
     
  • Schedule every n hours a script that puts all files in a txt file then you can quickly look them up

Share this post


Link to post
Share on other sites

#17 ·  Posted

Subz - Going to give your scripting a run through today and see what she does...

 

junkew - so, I work in a product test enviroment where our engineers/drafters store all the technical drawings, test procedures, etc in a directory called the Drawing Vault. It runs well over 500,000 files. What I am making are macros/apps to streamline the test depts ability to find the files we need faster, without having to sift through all the non-needed files. 

The files names for the products already in production/past production are pretty fixed named except for the Revision levels. But new drawings/specs are being added for new builds and test runs. So our file searches can be for any file in the Vault.  I am trying to narrow the searches to the directories that we as test use daily so to make the searches faster. Eventually I will try to make a Search Dashboard that can put all the different searches into one GUI or make something to handle all the directories we use. Our Engineers make it difficult by making arbitraty directories to place files that we need but not telling us where in the vault they are!! 

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Subz- If the file is found in both directories - script works as advertised. If the file is found in Doc Rel (SourceFoldera) but not in Rev Arc (SourceFolderb)  - script works as advertised.

If the files is not found in Doc Rel (SourceFoldera) I getthis error :

"I:\Work\Macro\AutoIT Scripts\Mine\DRS\DRSv1.4.au3" (106) : ==> "ReDim" used without an array variable.:
Func _ErrorCodes($iErrorCode = 0)
Func _ErrorCodes($iErrorCode = 0)^ ERROR

****Just did multiple searches and it seems if the file is not in Doc Rel (SourceFoldera), which is the first folder searched, it gives the msgbox "Path not found or invalid" and then gives the Error above. ****

I did find a typo on my part for the ReDIm $aFileListb (forgot to type the b ) but a rerun gave the same error....

Edited by Fractured

Share this post


Link to post
Share on other sites

#19 ·  Posted

Please retry code above

Share this post


Link to post
Share on other sites

#20 ·  Posted

You Rock Subz!! Script runs great! Now to read over it fully and comment the heck out of it for later understanding!! 

Thanks again!

Share this post


Link to post
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