File Count from a Folder

File count from a folder

System.IO.Directory myDir = GetMyDirectoryForTheExample();
int count = myDir.GetFiles().Length;

How can I count files in folder

You don't need VBA for this, standard Excel functions can compute these counts.

Column B is used to extract the filename from the path:

=MID(A1,FIND("*",SUBSTITUTE(A1,"\","*",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))

Column C is then used to extract the path:

=LEFT(A1,LEN(A1)-LEN(B1))

Finally, Column D can count the number of files that reside in the same directory:

=COUNTIF(C:C,$C1)

If you really do need to do this in VBA, then here's a couple of functions that will extract the filename or directory given a full path:

' Returns the file name given a full file path
Function BaseName(FilePath)
BaseName = Mid(FilePath, InStrRev(FilePath, "\") + 1)
End Function

' Returns the directory path given a full file path
Function DirName(FilePath)
DirName = Mid(FilePath, 1, Len(FilePath) - Len(BaseName(FilePath)))
End Function

Python count files in a directory and all its subdirectories

IIUC, you can just do

sum(len(files) for _, _, files in os.walk('path/to/folder'))

or perhaps, to avoid the len for probably slightly better performance:

sum(1 for _, _, files in os.walk('folder_test') for f in files)

How to count the number of files in a sub folder with a specific name using C#?

    public static void CountFiles(string path)
{
int xFileCount = 0;
int yFileCount = 0;
int zFileCount = 0;

var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

foreach(string file in files)
{
string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
if (folder == "FOLDER_X")
xFileCount++;
if (folder == "FOLDER_Y")
yFileCount++;
if (folder == "FOLDER_Z")
zFileCount++;
}

Console.WriteLine("X Files : {0}", xFileCount);
Console.WriteLine("Y Files : {0}", yFileCount);
Console.WriteLine("Z Files : {0}", zFileCount);
}

Few tips:

  • If you want to search for a specific type of files (say, for example, only text files) then you can pass search pattern to Directory.GetFiles(), such as ".txt" instead of ".*".
  • If you want to make this more generic, instead of just hardcoding your folder names you could pass that as a parameter.

So I would really use this function, and call it with whatever folder name you want:

    public static int CountFiles(string path, string folderToSearch)
{
int fileCount = 0;

var files = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);

foreach (string file in files)
{
string folder = new DirectoryInfo(Path.GetDirectoryName(file)).Name;
if (folder == folderToSearch)
fileCount++;
}

return fileCount;
}

Then call it like so:

    static void Main(string[] args)
{
int xFiles = CountFiles("path goes here", "FOLDER_X");
int yFiles = CountFiles("path goes here", "FOLDER_Y");
int zFiles = CountFiles("path goes here", "FOLDER_Z");
}

EDIT: Made a small change to how you get the immediate directory name.

EDIT2: Edited to incorporate the suggestion by @MickyD.

EDIT3: This would pass your most recent requirement.

    public static int CountFiles2(string path, string folderToSearch)
{
int fileCount = 0;
var dirs = Directory.EnumerateDirectories(path, folderToSearch, SearchOption.AllDirectories).ToList();

foreach (string dir in dirs)
{
var files = Directory.EnumerateFiles(dir, "*.*", SearchOption.AllDirectories);
if (files != null)
fileCount += files.Count();
}

return fileCount;
}

recursively count files in folder and output to file

I think it would be best to get an array of resulting objects where you can store both the directory path and the number of files it contains. That way, you can afterwards show it in the console and also save it to a structured CSV file you can open in Excel.

This is for PowerShell 2:

# to keep the property order in PS version < 3.0, create an 
# Ordered Dictionary to store the properties first
$dict = New-Object System.Collections.Specialized.OrderedDictionary

# now loop over the folders
$result = Get-ChildItem -Path 'E:\' -Recurse -Force -ErrorAction SilentlyContinue |
Where-Object { $_.PSIsContainer } |
ForEach-Object {
# add the results in the temporary ordered dictionary
$dict.Add('Directory', $_.FullName)
$dict.Add('Files', @(Get-ChildItem -Path $_.FullName -Force -ErrorAction SilentlyContinue |
Where-Object { !$_.PSIsContainer }).Count)
# and output a PSObject to be collected in array '$result'
New-Object PSObject -Property $dict
$dict.Clear()
}

# output on screen
$result | Format-Table -AutoSize

#output to CSV file
$result | Export-Csv -Path 'D:\Test\FileCount.csv' -NoTypeInformation

The -Force switch makes sure you also count items that otherwise can't be accessed by the user, such as hidden or system files.

How to count the number of files in a folder in external storage?

Try this,and replace yourfolder with your actual folder name

File dir = new File(Environment.getExternalStorageDirectory() + "/yourfolder");
File[] files = dir.listFiles();
if (files != null) {
int numberOfFiles = files.length;
}


Related Topics



Leave a reply



Submit