Jump to content

Help searching through directories


Recommended Posts

Hi all,

I have a list of files coming to a set of directories every day and I have to check for missing files, empty files and files successfully generated. I have listed the filename/extension in an excel spreadsheet for maintenance purposes and am trying t write a script to go through the excel sheet read each file and then search for that file name in the directories. I would then write the file names of files created, missing or empty file in three different text files for easier review.

At this point, I am stuck getting the error below and it does not do the search. Any help or suggestion would be really appreciated it. Please see the script below.

\C:\Users\Manuel\Desktop\RasReportAuto\RasReport.au3 (52) : ==> Error parsing function call.:

FileClose($search

FileClose(^ ERROR

SCRIPT ----------------------

#include <excel.au3>

$oExcel = _ExcelBookOpen(@ScriptDir & "\source.xlsx", 0)                        
Global $iLastRow = $oExcel.Cells.SpecialCells($xlCellTypeLastCell).Row  
For $iIndex = 1 To $iLastRow                                            
$sValueA = _ExcelReadCell($oExcel, "A" & $iIndex)
Dim $FolderName = @ScriptDir & "\"
Dim $FileCount = 0
ScanFolder($FolderName, $sValueA)

Next 
_ExcelBookClose($oExcel)

Func ScanFolder($SourceFolder, $xfile)
Local $Search
Local $File
Local $FileAttributes
Local $FullFilePath
$Search = FileFindFirstFile($SourceFolder & "\*.*")
While 1
If $Search = -1 Then
ExitLoop
EndIf
$File = FileFindNextFile($Search)
If @error Then ExitLoop
$FullFilePath = $SourceFolder & $File
$FileAttributes = FileGetAttrib($FullFilePath)
If StringInStr($FileAttributes,"D") = 0 Then
dosearch($fullfilepath , $xfile)
Else

endif
WEnd
FileClose($Search)
EndFunc
 

Func dosearch($path, $File)
MsgBox(0, "", $path & $file)

$search = FileFindFirstFile("*.*")  
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
$file = FileFindNextFile($search) 
If @error Then ExitLoop
consolewrite($file & @cr)

WEnd
FileClose($search

EndFunc

Just another special date with a different challenge

Link to comment
Share on other sites

First thing I see is that your have 2 "\" in your search.

1. $FolderName = @ScriptDir & "\"

2. $Search = FileFindFirstFile($SourceFolder & "\*.*")

$SourceFolder = @ScriptDir & "\\*.*")

Second thing is, In Func dosearch()

FileClose($Search

Needs to be FileClose($Search)

Third in Func dosearch():

$Search = FileFindFirstFile("*.*")

need to have a directory to start

Last:

What is dosearch Func actually accomplishing? You are sending the file to dosearch with Attribute "D" (Directory equaling 0) then ....

Edited by rogue5099
Link to comment
Share on other sites

rogue5099,

Thanks for replying. I made the corrections you suggested and was able to progress a bit more.

Now I am able to create two text files identifying files with no data(I tested a few text files that generated with only headers and not usable data and they all where less than one kilobyte in size) and files that are over 1kb (files with usable data). now I am stuck at identifying missing files.

what it did was create if statements saying if the file was <1kb add it to one text file, if it was > 1kb add it to another text file, else, add it to a text file with all the missing files.

the last one (the missing files) is not working. Can any one suggest me how to accomplish this?

Thanks in advance.

CODE:

#include <excel.au3>

$oExcel = _ExcelBookOpen(@ScriptDir & "\source.xlsx", 0)                        
Global $iLastRow = $oExcel.Cells.SpecialCells($xlCellTypeLastCell).Row  
For $iIndex = 1 To $iLastRow                                            
$sValueA = _ExcelReadCell($oExcel, "A" & $iIndex)
Dim $FolderName = @ScriptDir & "\"
Dim $FileCount = 0
ScanFolder($FolderName, $sValueA)

Next 
_ExcelBookClose($oExcel)

Func ScanFolder($SourceFolder, $xfile)
Local $Search
Local $File
Local $FileAttributes
Local $FullFilePath
$Search = FileFindFirstFile($SourceFolder & "*.*")
While 1
If $Search = -1 Then
ExitLoop
EndIf
$File = FileFindNextFile($Search)
If @error Then ExitLoop
$FullFilePath = $SourceFolder & $File
$FileAttributes = FileGetAttrib($FullFilePath)
If StringInStr($FileAttributes,"D") Then
dosearch($fullfilepath , $xfile)
Else
endif
WEnd
FileClose($Search)
EndFunc

Func dosearch($path, $xFile)
$search = FileFindFirstFile($path & "\*.*")  
If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
Exit
EndIf
While 1
$file = FileFindNextFile($search) 
If @error Then ExitLoop
if $path &"\" & $file = $path & "\" & $xfile then
    if FileGetSize($path & "\" & $file) < 1024 Then
    FileWriteLine(@ScriptDir & "\FileWithNotData.txt",$File)
Else
    if FileGetSize($path & "\" & $file) > 1024 Then
        FileWriteLine(@ScriptDir & "\FileGenerated.txt",$File)
    Else
        if FileExists($path & "\" & $xfile)= 0 Then
            FileWriteLine(@ScriptDir & "\FileDidNotGenerate.txt",$File)  ;--------This is where I am having problem.-------------
    EndIf
endif
endif

EndIf
WEnd
FileClose($search)
EndFunc

Just another special date with a different challenge

Link to comment
Share on other sites

Hi.

If FileExists($path & "\" & $xfile) Then ; file exists
    If $path & "\" & $File = $path & "\" & $xfile Then
        If FileGetSize($path & "\" & $File) < 1024 Then
            FileWriteLine(@ScriptDir & "\FileWithNotData.txt", $File)
        Else
            If FileGetSize($path & "\" & $File) > 1024 Then
                FileWriteLine(@ScriptDir & "\FileGenerated.txt", $File)
            EndIf
        EndIf
    Else
        ; else for checking $path & "\" & $File = $path & "\" & $xfile
    EndIf
Else ; file doesn't exist
    FileWriteLine(@ScriptDir & "\FileDidNotGenerate.txt", $File)
EndIf

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Thanks Rudi,

I tried your suggestion and got even closer to what I want to accomplish.

The problem now is that even thus I get the three text files (missing files, files with no data and files generated)

I see that files generated (files that exist and have data)also appear in the missingreports text file and appear more than once.

Finally, although I added a line to close all excel processes at the begining and the end of the script but still have to go to CTR + ALT + DEL to close excel processes because the script doesnt do it.

I hope that is a quick fix. Can anyone suggest me something?

Thanks in advance

CODE:

#include <excel.au3>
ProcessClose("excel.exe")
sleep(100)
FileDelete(@ScriptDir &  "\MissingReports.txt")
FileDelete(@ScriptDir &  "\FileWithNotData.txt")
FileDelete(@ScriptDir &  "\FileGenerated.txt")

$oExcel = _ExcelBookOpen(@ScriptDir & "\source.xls", 0)                        
Global $iLastRow = $oExcel.Cells.SpecialCells($xlCellTypeLastCell).Row  
For $iIndex = 1 To $iLastRow                                            
$sValueA = _ExcelReadCell($oExcel, "A" & $iIndex)
Dim $FolderName = @ScriptDir & "\"
Dim $FileCount = 0
ScanFolder($FolderName, $sValueA)

Next 
_ExcelBookClose($oExcel)

Func ScanFolder($SourceFolder, $xfile)

Local $Search
Local $File
Local $FileAttributes
Local $FullFilePath
$Search = FileFindFirstFile($SourceFolder & "*.*")
While 1
If $Search = -1 Then
ExitLoop
EndIf
$File = FileFindNextFile($Search)
If @error Then ExitLoop
$FullFilePath = $SourceFolder & $File
$FileAttributes = FileGetAttrib($FullFilePath)
If StringInStr($FileAttributes,"D") Then
dosearch($fullfilepath , $xfile)
Else
endif
WEnd
FileClose($Search)
EndFunc

Func dosearch($path, $xFile)
$search = FileFindFirstFile($path & "\*.*")  
If $search = -1 Then
;MsgBox(0, "Error", "No files/directories matched the search pattern")
;ProcessClose("excel.exe")
;Exit
EndIf
While 1
$file = FileFindNextFile($search) 
If @error Then ExitLoop

If FileExists($path & "\" & $xfile) Then ; file exists
    If $path & "\" & $File = $path & "\" & $xfile Then
        If round(FileGetSize($path & "\" & $File)/1024) < 2 Then
            FileWriteLine(@ScriptDir & "\FileWithNotData.txt", $File)
        Else
            If round(FileGetSize($path & "\" & $File)/1024) > 2 Then
                FileWriteLine(@ScriptDir & "\FileGenerated.txt", $File)
            Else
                
            EndIf
        EndIf
    Else
        ; else for checking $path & "\" & $File = $path & "\" & $xfile
    EndIf
Else ; file doesn't exist
    FileWriteLine(@ScriptDir & "\MissingReports.txt", $File)
EndIf

WEnd
FileClose($search)
EndFunc

Just another special date with a different challenge

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...