Jump to content

Count number of files in folder..?


Recommended Posts

Is there a command in AutoIt, which will let a script count the number of files in a specific folder, and return a result, which can be used in other functions?

I.e. something like:

$Number = FileCount ("C:\windows\fonts")

MsgBox(0,"Number of fonts", $Number)

Any help will be greatly appreciated!

Link to comment
Share on other sites

Look at _FileListToArray in the help file. You can retrieve an array of files/folders with the number being in $array[0].

(from the help file)

#include <File.au3>
#include <Array.au3>

Local $FileList = _FileListToArray(@DesktopDir)
If @error = 1 Then
MsgBox(0, "", "No Folders Found.")
Exit
EndIf
If @error = 4 Then
MsgBox(0, "", "No Files Found.")
Exit
EndIf
_ArrayDisplay($FileList, "$FileList")

So in this example $FileList[0] is the number of files/folders on your desktop. Keep in mind it counts hidden files/folder as well. You can set the third argument of _FileListToArray to only return files or folders as well.

Edit: formatting

Edited by digiworks
Link to comment
Share on other sites

Try this Function

ConsoleWrite(FileCount(@TempDir)&@CRLF)
;ShellExecute(@TempDir)

Func FileCount($sDir); Shows the filenames of all files in the current directory
If StringRight($sDir,1)<>'' Then $sDir&=''
;ConsoleWrite($sDir&@CRLF)
If Not FileExists($sDir) Then Return SetError(1,0,-1)
$search = FileFindFirstFile($sDir&"*.*")

; Check if the search was successful
If $search = -1 Then
MsgBox(0, "Error", "The Director is Empty")
Exit
EndIf
Local $nCount=0
While 1
FileFindNextFile($search)
If @error Then ExitLoop
$nCount+=1
WEnd

; Close the search handle
FileClose($search)
Return $nCount
EndFunc

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

@ eXceLlence

OK, this function count the number of files in the temporary dir, if I'm not much mistaken.

How do I set it to count the files in i.e. C:UsersMigDesktop ?

Is this possible..?

@ digiworks

I do not seem to find any refrence to "_FileListToArray" anywhere in the help system.

I search for "_" I get results, but if I search for as much as "_F" there is no results found... *puzzled*

Link to comment
Share on other sites

Go to the index of the help file and start typing filelisttoarray, it will show up, don't search for it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@ eXceLlence

OK, this function count the number of files in the temporary dir, if I'm not much mistaken.

How do I set it to count the files in i.e. C:UsersMigDesktop ?

Is this possible..?

@ digiworks

I do not seem to find any refrence to "_FileListToArray" anywhere in the help system.

I search for "_" I get results, but if I search for as much as "_F" there is no results found... *puzzled*

If you can't find it in the help file [look in User Defined Function Reference -> File management]

It's in the web help at http://www.autoitscript.com/autoit3/docs/libfunctions/_FileListToArray.htm

William

Link to comment
Share on other sites

OK, I've gotten this far:

#include <File.au3>
#include <Array.au3>
Local $FileList = _FileListToArray("C:UsersMigPicturesLotteriet")
If @error = 1 Then
    MsgBox(0, "", "No Folders Found.")
    Exit
EndIf
If @error = 4 Then
    MsgBox(0, "", "No Files Found.")
    Exit
EndIf
_ArrayDisplay($FileList, "$FileList")

Here I get a nice messagebox, telling me which files are in the selected folder, but I need a number of files (and I cannot find anything resembling "FileCount"..

Link to comment
Share on other sites

MsgBox(64, "# of files", "Number of files found = " & $FileList[0])

Read the help file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

If you want only the number of files using DirGetSize

$patch = "C:\Windows\System32\"
$test = DirGetSize($patch, 1)
MsgBox(4096,'Test', "Number of files found: " & $test[1])
Link to comment
Share on other sites

@ eXceLlence

OK, this function count the number of files in the temporary dir, if I'm not much mistaken.

How do I set it to count the files in i.e. C:UsersMigDesktop ?

Is this possible..?

Place 'C:UsersMigDesktop' in place of '@TempDir' in the same function

Well BTW the solution provided by Belini is much easy and efficient

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Here is a function that you can use to count the number of files in any directory. File counts can be further narrowed down by using a wildcard value, for example Dir_GetFileCount(@DesktopDir, "a*.txt") would only count txt files located on the desktop that start with the letter a.

MsgBox(1,'','Number of files on the desktop ' & Dir_GetFileCount(@DesktopDir))

Func Dir_GetFileCount($sTargetDir, $sWildPattern = "*.*")
If Not FileExists($sTargetDir) Then Return SetError(1)
If $sWildPattern = Default Then $sWildPattern = "*.*"
If StringRight($sTargetDir,1) <> "\" Then $sTargetDir &= '\'
Local $hFound , $sNext, $iCount = 0
$hFound = FileFindFirstFile($sTargetDir & $sWildPattern)
If $hfound = -1 Then
FileClose($hFound)
Return $iCount
EndIf
While True
$sNext = FileFindNextFile($hFound)
If @error Then
ExitLoop
ElseIf Not @extended Then
$iCount += 1
EndIf
WEnd
FileClose($hFound)
Return $iCount
EndFunc
Link to comment
Share on other sites

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