It is currently 21 April 2025, 13:45 Advanced search

Count files in a folder

Questions and answers on how to best use Instant Developer

Count files in a folder

Postby john w » 16 January 2024, 20:50

I would like to count the number of files in my /temp folder. I cannot locate the syntax to perform the count.

Any suggestions will be appreciated.
Regards

John W
User avatar
john w
 
Posts: 834
Joined: 24 October 2012, 16:29
Location: Apple Valley, Minnesota USA

Re: Count files in a folder

Postby ljwilson » 17 January 2024, 13:51

The only way I know how to do it in inde is use ReadDirectory to loop through all files and increment a counter variable each time.

...jack
ljwilson
 
Posts: 783
Joined: 26 November 2013, 14:15

Re: Count files in a folder

Postby t.simoncini » 17 January 2024, 16:16

ljwilson wrote:The only way I know how to do it in inde is use ReadDirectory to loop through all files and increment a counter variable each time.

From what I know, that's the official way to do it. That's how Windows Explorer does it.
If I remember correctly, the file system does not have the information on how many files are inside it.
t.simoncini
 
Posts: 1585
Joined: 5 March 2012, 14:00

Re: Count files in a folder

Postby john w » 17 January 2024, 17:25

I agree that is a correct method. However, I cannot determine the way to loop through the files when it comes to reading the directory. I seem to be missing something with control flows of While, For, and For-Each. That is what to test the flow against.
Regards

John W
User avatar
john w
 
Posts: 834
Joined: 24 October 2012, 16:29
Location: Apple Valley, Minnesota USA

Re: Count files in a folder

Postby ljwilson » 17 January 2024, 17:42

Look at my delete temp files code from that example app. It does it.

Excerpt:
2024-01-17_12-40-37.png
2024-01-17_12-40-37.png (62.63 KiB) Viewed 1224 times


...jack
ljwilson
 
Posts: 783
Joined: 26 November 2013, 14:15

Re: Count files in a folder

Postby john w » 18 January 2024, 15:52

I use this to delete files as well. However, if you use the While with just a counter, I am not sure it works. This is what I see the basic count logic looking like. What is the condition I would use?
-----------
count = 0
While (what condition)
count = count +1
Regards

John W
User avatar
john w
 
Posts: 834
Joined: 24 October 2012, 16:29
Location: Apple Valley, Minnesota USA

Re: Count files in a folder

Postby ljwilson » 18 January 2024, 16:48

The logic is everytime you run readdirectory with the same wildcard it gets the next file that matches the wildcard until if doesn't find any more.

Read up on readdirectory in the help. The picture I posted above is the while logic.

...jack
ljwilson
 
Posts: 783
Joined: 26 November 2013, 14:15

Re: Count files in a folder

Postby john w » 18 January 2024, 17:49

That remains my question, what is the wild card you would use for a pure count function. If you contrive something, it seems you could end up an endless loop. You have to something to count and it has to have an end.

I did read up on ReadDirectoy but I must have missed seeing how it would count.

This is such a basic question, I am probably explaining the question poorly.

In .net I would use a For - Each and iterate the files in the directory with a counter in place. In InDe I cannot see that is possible.
Regards

John W
User avatar
john w
 
Posts: 834
Joined: 24 October 2012, 16:29
Location: Apple Valley, Minnesota USA

Re: Count files in a folder

Postby ljwilson » 18 January 2024, 19:28

I must not be answering it very well either!

Conceptually, lets say you have 5 files in the temp folder
you set your counter variable to 0.
readdirectory is going to walk through those 5 file in a loop. Each invocation of readdirectory gives you a different filename.
for each iteration of the loop you increment your counter variable by 1
when the loop exits since no more filenames are returned, your count holds the variable.

Just code it and test it with the debugger and convince yourself you are getting a different file name for each iteration of the loop and the counter increments inside the loop each time.

...jack
ljwilson
 
Posts: 783
Joined: 26 November 2013, 14:15

Re: Count files in a folder

Postby john w » 18 January 2024, 19:32

Jack

I will give it a go.
Regards

John W
User avatar
john w
 
Posts: 834
Joined: 24 October 2012, 16:29
Location: Apple Valley, Minnesota USA


Return to Tips & Tricks

Who is online

Users browsing this forum: No registered users and 32 guests

cron