Hello!
Is there a command that shows the total number of files in a folder?
a.txt
b.txt
c.txt
would return: 3
Thanks!
Count files in folder
- marcoagpinto
- Addict
- Posts: 939
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Count files in folder
Here might be 3 ways: viewtopic.php?f=5&t=47987
ExamineDirectory(), PowerShell, ExplorerListGadget
I don't know how up-to-date they are.
ExamineDirectory(), PowerShell, ExplorerListGadget
I don't know how up-to-date they are.
-
- User
- Posts: 34
- Joined: Sun Nov 23, 2014 1:18 pm
Re: Count files in folder
I use this little code
Code: Select all
Procedure.i CountFiles(path.s, pattern.s = "*.*", recursive.i = 0)
Protected dir_handle
Protected name.s
Protected counter
If Right(path, 1) <> "\"
path = path + "\"
EndIf
dir_handle = ExamineDirectory(#PB_Any, path, pattern)
If dir_handle = 0
ProcedureReturn 0
EndIf
While NextDirectoryEntry(dir_handle)
name = DirectoryEntryName(dir_handle)
If ReplaceString(name, ".", "") <> "" And DirectoryEntryType(dir_handle) = #PB_DirectoryEntry_Directory And recursive = 1
counter + CountFiles(path + name + "\", pattern, recursive)
Else
counter + 1
EndIf
Wend
FinishDirectory(dir_handle)
ProcedureReturn counter
EndProcedure
Debug CountFiles("C:\", "*.*", 1)
Re: Count files in folder
Windows command (very fast but need some code after)marcoagpinto wrote:Is there a command that shows the total number of files in a folder?
Code: Select all
RunProgram("cmd", "/k dir | findstr fichier", "C:\Windows", #PB_Program_Wait)
Code: Select all
58 fichier(s) 14 619 079 octets
Replace /k with /c and use #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide to use it in code.
- marcoagpinto
- Addict
- Posts: 939
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Count files in folder
Thank you, my friends!
Re: Count files in folder
Here's a better way with "dir", that doesn't need to worry about languages at all.Marc56us wrote:Replace "fichier" with your langage
Returns -1 if the dir doesn't exist, or the number of files in it (but not sub-folders).
Code: Select all
Procedure CountFilesInFolder(dir$)
If FileSize(dir$)<>-2
n=-1
Else
p=RunProgram(GetEnvironmentVariable("comspec"),"/c dir /b /a-d "+Chr(34)+dir$+Chr(34),"",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
While ProgramRunning(p)
If AvailableProgramOutput(p)
o$+ReadProgramString(p)+#CRLF$
EndIf
Wend
CloseProgram(p)
n=CountString(o$,#CRLF$)
EndIf
ProcedureReturn n
EndProcedure
Debug CountFilesInFolder("C:\Windows")
Debug CountFilesInFolder("C:\Program Files\Internet Explorer\")
Last edited by Dude on Thu Oct 11, 2018 1:43 pm, edited 1 time in total.
Re: Count files in folder
Sorry Mr. Dude.. code fails if the are spaces in the foldername.Dude wrote:Here's a better way with "dir", that doesn't need to worry about languages at all.Marc56us wrote:Replace "fichier" with your langage
Returns -1 if the dir doesn't exist, or the number of files in it (but not sub-folders).
using Win10 Pro and PB 5.70 beta 2 (x86)
- It was too lonely at the top.
System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
System : PB 6.10 Beta 9 (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Re: Count files in folder
Fixed (see edited post). Thanks!blueb wrote:code fails if the are spaces in the foldername