LarsBNygaard Posted July 16, 2012 Posted July 16, 2012 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!
digiworks Posted July 16, 2012 Posted July 16, 2012 (edited) 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 July 16, 2012 by digiworks
PhoenixXL Posted July 16, 2012 Posted July 16, 2012 Try this FunctionConsoleWrite(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.
LarsBNygaard Posted July 16, 2012 Author Posted July 16, 2012 @ eXceLlenceOK, 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..?@ digiworksI 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*
BrewManNH Posted July 16, 2012 Posted July 16, 2012 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 GudeHow 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
saywell Posted July 16, 2012 Posted July 16, 2012 @ 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
LarsBNygaard Posted July 16, 2012 Author Posted July 16, 2012 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"..
BrewManNH Posted July 16, 2012 Posted July 16, 2012 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 GudeHow 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
Belini Posted July 16, 2012 Posted July 16, 2012 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]) BrewManNH and pixelsearch 2 My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
PhoenixXL Posted July 16, 2012 Posted July 16, 2012 (edited) @ eXceLlenceOK, 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 functionWell BTW the solution provided by Belini is much easy and efficient Edited July 16, 2012 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.
Quual Posted July 16, 2012 Posted July 16, 2012 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
Belini Posted July 17, 2012 Posted July 17, 2012 This function only searches into folders and subfolders does not seek. My Codes: Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now