Jump to content

Any Speedy method to count files in dir


 Share

Recommended Posts

Helo guys,

I have a GUI which actually Recursive search in the target DIR . i want to show that how much files are done (mean to say how much files are checked). i tried to count the DIRfiles and then set it up with percentage formula to show the progress bar. now the problem is that if the any dir have huge data the script held while its count all the files, so is there any other method or any other speedy method to count dir files ?

Thanks in advance.

Edited by Digisoul

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

I would suggest using _FileListToArray(). The first value in the array will be the total number of files found.

Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt
Link to comment
Share on other sites

I would suggest using _FileListToArray(). The first value in the array will be the total number of files found.

Thankyou for your reply sir, but _FileListToArray(). only get the files of Root Dir, in my case user can select the whole Drive ?

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

Faster than _FileListToArray() if you just want the total number of files:

$iFcount = 0
$dir = @SystemDir
$fileSpec = "*.*"

$search = FileFindFirstFile($dir & "\" & $fileSpec)

; Check if the search was successful
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 StringInStr(FileGetAttrib($dir & "\" & $file), "D") Then ContinueLoop; skip directories
    $iFcount += 1
WEnd

; Close the search handle
FileClose($search)

MsgBox(0, "", $iFcount & " files in " & $dir)
Link to comment
Share on other sites

Faster than _FileListToArray() if you just want the total number of files:

$iFcount = 0
$dir = @SystemDir
$fileSpec = "*.*"

$search = FileFindFirstFile($dir & "\" & $fileSpec)

; Check if the search was successful
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 StringInStr(FileGetAttrib($dir & "\" & $file), "D") Then ContinueLoop; skip directories
    $iFcount += 1
WEnd

; Close the search handle
FileClose($search)

MsgBox(0, "", $iFcount & " files in " & $dir)

Mr ResNullius 1st thanks for your reply, your method is very handy, but actually its not fullfill my requierments, my Problem is, shows the progress bar with in avg of 100% during search which indicates that how much files are checked.

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

what about a DIR command in CMD ??

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

If you only need a count...

DirGetSize ( "path" [, flag] )

Parameters

dest dir The directory path to get the size from, e.g. "C:\Windows".

flag [optional] this flag determines the behaviour and result of the function,

and can be a combination of the following:

0 = (default)

1 = Extended mode is On -> returns an array that contains extended information (see Remarks).

2 = Don't get the size of files in subdirectories (recursive mode is Off)

Return Value

Success: Returns >= 0 the sizes

Failure: Returns -1 and sets @error to 1 if the path doesn't exist.

Remarks

If the script is paused then this function is paused too and will only continue when the script continues!

If you use the extended mode then the array returned from this function is a single dimension array containing the following elements:

$array[0] = Size

$array[1] = Files count

$array[2] = Dirs Count

Edited by mlowery
Link to comment
Share on other sites

If you only need a count...

DirGetSize ( "path" [, flag] )

Parameters

dest dir The directory path to get the size from, e.g. "C:\Windows".

flag [optional] this flag determines the behaviour and result of the function,

and can be a combination of the following:

0 = (default)

1 = Extended mode is On -> returns an array that contains extended information (see Remarks).

2 = Don't get the size of files in subdirectories (recursive mode is Off)

Well, no matter how long you've been using AutoIt, you can learn something new from the documentation every day. :mellow: Edited by ResNullius
Link to comment
Share on other sites

@Digisoul,

This isn't a recursive example, but it does show how you can use the file count info from DirGetSize() along with a search function to get a progress indicator as the search walks the list of files

$dir = @SystemDir
$a = DirGetSize($dir, 3)
$iTtlFcount = $a[1]
$iFcount = 0
$fileSpec = "*.*"
$search = FileFindFirstFile($dir & "\" & $fileSpec)
$sFileName = ""
ProgressOn("File Check", "Now checking files: " & $sFileName)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $ProgressTotal = ($iFcount / $iTtlFcount) * 100
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    If StringInStr(FileGetAttrib($dir & "\" & $file), "D") Then ContinueLoop; skip directories
    ProgressSet($ProgressTotal, Round($ProgressTotal) & " % done " & @CRLF & $dir & "\" & $file)
    $iFcount += 1
WEnd

; Close the search handle
FileClose($search)
MsgBox(4096+64, "Complete!", $iFcount & " of " & $iTtlFcount & " files in " & $dir & " checked.")
ProgressOff()
Link to comment
Share on other sites

Example using COM:

Dim $Path = @UserProfileDir

Global $tFileCount = DirGetSize($Path, 1)
$tFileCount = $tFileCount[1]

Global $Percent = 0, $Count = 0
Global $fso = ObjCreate("Scripting.FileSystemObject")

ProgressOn("Please wait...", "File count")

_CountFiles($Path)

ProgressSet(100, "Done")
Sleep(1000)

ProgressOff()

Func _CountFiles($sPath = @ScriptDir)
    Local $objFolder, $colSubFolder, $objSubFolder, $colFiles, $objFile
    
    $objFolder = $fso.GetFolder($sPath)
    $colSubFolder = $objFolder.SubFolders
    
    $colFiles = $objFolder.Files
    
    For $objFile In $colFiles
        $Count += 1
        $Percent = Round($Count / $tFileCount * 100, 2)
        ProgressSet($Percent, $Percent & "% percent")
    Next
    
    For $objSubFolder In $colSubFolder
        _CountFiles($objSubFolder.Path)
    Next
EndFunc

:mellow:

Link to comment
Share on other sites

@Digisoul,

This isn't a recursive example, but it does show how you can use the file count info from DirGetSize() along with a search function to get a progress indicator as the search walks the list of files

$dir = @SystemDir
$a = DirGetSize($dir, 3)
$iTtlFcount = $a[1]
$iFcount = 0
$fileSpec = "*.*"
$search = FileFindFirstFile($dir & "\" & $fileSpec)
$sFileName = ""
ProgressOn("File Check", "Now checking files: " & $sFileName)

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $ProgressTotal = ($iFcount / $iTtlFcount) * 100
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    If StringInStr(FileGetAttrib($dir & "\" & $file), "D") Then ContinueLoop; skip directories
    ProgressSet($ProgressTotal, Round($ProgressTotal) & " % done " & @CRLF & $dir & "\" & $file)
    $iFcount += 1
WEnd

; Close the search handle
FileClose($search)
MsgBox(4096+64, "Complete!", $iFcount & " of " & $iTtlFcount & " files in " & $dir & " checked.")
ProgressOff()
Thank you very much sir thats exactly what i need.

73 108 111 118 101 65 117 116 111 105 116

Link to comment
Share on other sites

Example using COM:

Dim $Path = @UserProfileDir

Global $tFileCount = DirGetSize($Path, 1)
$tFileCount = $tFileCount[1]

Global $Percent = 0, $Count = 0
Global $fso = ObjCreate("Scripting.FileSystemObject")

ProgressOn("Please wait...", "File count")

_CountFiles($Path)

ProgressSet(100, "Done")
Sleep(1000)

ProgressOff()

Func _CountFiles($sPath = @ScriptDir)
    Local $objFolder, $colSubFolder, $objSubFolder, $colFiles, $objFile
    
    $objFolder = $fso.GetFolder($sPath)
    $colSubFolder = $objFolder.SubFolders
    
    $colFiles = $objFolder.Files
    
    For $objFile In $colFiles
        $Count += 1
        $Percent = Round($Count / $tFileCount * 100, 2)
        ProgressSet($Percent, $Percent & "% percent")
    Next
    
    For $objSubFolder In $colSubFolder
        _CountFiles($objSubFolder.Path)
    Next
EndFunc

:mellow:

Thats Prefect example sir. thank you very much for your time and example sir. Thanks all guys for helping me. :(

73 108 111 118 101 65 117 116 111 105 116

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