Sign in to follow this  
Followers 0
OldMike

Need to get count of files in a folder

12 posts in this topic

Need to simply count the number of files in a folder. Found postings re: FileListToArray, but it is not listed in the on line documentation of functions.

Mike

Share this post


Link to post
Share on other sites



You should have been able to find it in the online help ... but here it is: quoting from the AutoIT help file.

_FileListToArray

--------------------------------------------------------------------------------

Lists files and\or folders in a specified path (Similar to using Dir with the /B Switch)

#Include <File.au3>

_FileListToArray($sPath [, $sFilter = "*" [, $iFlag = 0]])

Parameters

$sPath Path to generate filelist for.

$sFilter [optional] the filter to use, default is *. Search the Autoit3 helpfile for the word "WildCards" For details.

$iFlag [optional] specifies whether to return files folders or both

$iFlag=0(Default) Return both files and folders

$iFlag=1 Return files only

$iFlag=2 Return Folders only

Return Value

Success: an Array, see remarks

Failure: 0

@Error: 1 = Path not found or invalid

2 = Invalid $sFilter

3 = Invalid $iFlag

4 = No File(s) Found

Remarks

The array returned is one-dimensional and is made up as follows:

$array[0] = Number of Files\Folders returned

$array[1] = 1st File\Folder

$array[2] = 2nd File\Folder

$array[3] = 3rd File\Folder

$array[n] = nth File\Folder

Related

Example

#Include <File.au3>

#Include <Array.au3>

$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")


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Try searching the help file for "_FileListToArray" (note leading "_")

HTH,

whim

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Nevermind - just seen my post now :)

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Thanks; just what I need.

So, there are functions documented in the editor Help that are not listed in the on line documentation list of functions???? Seems simple to copy one into the other.

Mike

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I have a hunch the OP did not take a look at the "User Defined Functions" part of the help file. Those functions are not native to AutoIt and _FileListToArray() is one of them under "File Management"

EDIT: I'm not sure if it is actually included in the official On-line docs for AutoIt but it is in my on-line docs and that link is in my signature although I should probaly modify my signature to show it is not the normal AutoIt On-line.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I have four files to check, all are in D:, named P1, P2, P3, P4. I need to check the number of PDF files in each. It seems this should work, runs without errors, but returns null.

#include<file.au3>

#Include<array.au3>

Global $fcnt[1]

For $n = 1 to 4

$path = _Pathmake("D:","\P"&$n,"\*.pdf","")

$fcnt[0] = _FileListToArray($path)

MsgBox(0,"","Number of files in P" & $n & " is: "&$fcnt[0])

Next

Share this post


Link to post
Share on other sites

First, you're not reading the files in the path, you're trying to read the files in a path name that contains the return result of your function _Pathmake, which is probably equivalent to _FileListToArray(0) where the 0 is the path the function is trying to read from. Second, use $fcnt = _FileListToArray($path) without the [0] element of the array. _FileListToArray returns an array of file names and puts them into your array, and the [0] element holds the number of files it finds, you just need to use the array name to have everything put into the array correctly.


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

Share this post


Link to post
Share on other sites

I removed [0] from the $fcnt = _FileListToArray($path), then got an error message on the line with msgbox unless I deleted it there too. The error message is "Subscript used on nonarray variable." When I do remove it on that line too, I am back to a null result.

I don't understand your comment about the path; could you suggest how that line should read? I am trying to read from:

D:\P1

D:\P2

D:\P3

D:\p4

Thanks for your patience.

Mike

Share this post


Link to post
Share on other sites

OK, got it going now. Doesn't square with the documentation, but it works:

#include<file.au3>

#Include<array.au3>

Global $fcnt

For $n = 1 to 4

$path = "D:\P" & $n

$fcnt = _FileListToArray($path,"*.pdf",1)

MsgBox(0,"","Number of files in P" & $n & " is: "&$fcnt[0])

Next

Share this post


Link to post
Share on other sites

OldMike,

Doesn't square with the documentation

Yes it does - why do you think it does not? :)

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

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
Sign in to follow this  
Followers 0