Recent

Author Topic: [SOLVED] Get the number of folders in folder without subdirectories.  (Read 895 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
Hi,
I have this kinds of structure

Code: Pascal  [Select][+][-]
  1. C:\35\123\0\0\1
  2. C:\35\123\0\0\2
  3. C:\35\123\0\0\3
  4. C:\35\444\0\0\1
  5. C:\35\444\0\0\2
  6. C:\35\444\0\0\3
  7. C:\35\444\0\0\4
  8. C:\35\444\0\1\1
  9. C:\35\444\0\1\2
  10. C:\35\444\0\1\3
  11. C:\35\444\0\2\1

Let's assume I want to get number of folders in  C:\35\ . There will be 2 folders (C:\35\123\ and C:\35\444\). I don't want to find anything deeper than C:\35.
How can I do that?  :|. I've tried some solutions but with no luck. :|

I want to be able to get folders count in path that I provide. Only folders, no files.

Thanks in advance :)





« Last Edit: October 22, 2019, 07:10:50 pm by TomTom »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Get the number of folders in folder without subdirectories.
« Reply #1 on: October 22, 2019, 07:08:42 pm »
Well, this is a kind of string manipulation game.  :D

Depends on the logic you use, you may need to use these functions:
- RPos
- PathDelim
- AppendPathDelim
- ChompPathDelim
- RightStr
- LeftStr

I recently writing code that has similar problem like yours. I found the code in the link below which after learning how it works, I now have better understanding how to 'jump' between different depth of the folder.

https://wiki.freepascal.org/paszlib#Zipping_a_whole_directory_tree_storing_only_a_relative_path

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Get the number of folders in folder without subdirectories.
« Reply #2 on: October 22, 2019, 07:10:32 pm »
I guess I always find solution right after asking a question...
FindAllDirectories is what I wanted :P

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Get the number of folders in folder without subdirectories.
« Reply #3 on: October 22, 2019, 07:11:48 pm »
You can use the function FindAllDirectories() or a TFileSearcher object. This last will allow you to simply respond to its OnDirectoryFound by incrementing a counter, without more ado.

Both are in the unit FileUtil of the LazUtils package.

ETA: Oh, already found it, huh?. Damn my slow-typing :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Get the number of folders in folder without subdirectories.
« Reply #4 on: October 22, 2019, 07:56:12 pm »
Thanks Handoko :) This could come in handy :)
I'm writing program that should help me creating reports for my employer :). I want to be able to get info about how many files and folders I have on different levels of depth of path :)
So for example I have sth like this:
C:\35\123\0\1\2 where:
35 is number of State Archive
123 is number of Collection in that archive
0 is continuation number of Collection in that archive
1 is series number
2 is a signature (don't know if its correct english word for this) of Collection (it contains files of scanned documents)

There can be multiple signatures in multiple series in ... etc
I've managed to get the file counts on different depth levels  of path and now I'm trying to get i.e number of series in specific collection or number of signatures in specific serie etc.

This will save me a lot of trouble, well atleast  if  I finish this app :) 
Well, this is a kind of string manipulation game.  :D

Depends on the logic you use, you may need to use these functions:
- RPos
- PathDelim
- AppendPathDelim
- ChompPathDelim
- RightStr
- LeftStr

I recently writing code that has similar problem like yours. I found the code in the link below which after learning how it works, I now have better understanding how to 'jump' between different depth of the folder.

https://wiki.freepascal.org/paszlib#Zipping_a_whole_directory_tree_storing_only_a_relative_path

 

TinyPortal © 2005-2018