572 lines
22 KiB
C#
572 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.IO
|
|
{
|
|
/// <summary>
|
|
/// Count files and/or directories
|
|
/// </summary>
|
|
class Count
|
|
{
|
|
#region Variablen
|
|
protected static long NumberOfFiles { set; get; } //Anzahl der Dateien
|
|
protected static long NumberOfDirectories { set; get; } //Anzahl der Verzeichnisse
|
|
#endregion
|
|
|
|
#region Konstruktor
|
|
/// <summary>
|
|
/// Count files or Directories in a directory.
|
|
/// </summary>
|
|
public Count()
|
|
{
|
|
}
|
|
#endregion
|
|
|
|
#region File
|
|
/// <summary>
|
|
/// Counts all files in a directory.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <returns>Number of found files.</returns>
|
|
public long File(string directoryName)
|
|
{
|
|
//Zählt alle Dateien in einem Verzeichnis
|
|
long numberOfFiles = 0;
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
numberOfFiles++;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfFiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all files in a directory that correspond to a particular pattern sequence.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <returns>Number of found files.</returns>
|
|
public long File(string directoryName, string searchPattern)
|
|
{
|
|
//Zählt alle Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfFiles = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern))
|
|
{
|
|
numberOfFiles++;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfFiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all files in a directory that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <param name="daysOld">Days the file must be old. 0=All files are counted.</param>
|
|
/// <returns>Number of found files.</returns>
|
|
public long File(string directoryName, string searchPattern, int daysOld)
|
|
{
|
|
//Zählt alle Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfFiles = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern) && DaysOld(fileName, daysOld))
|
|
{
|
|
numberOfFiles++;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfFiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all files in a directory and all subdirectories that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <param name="daysOld">Days the file must be old. 0=All files are counted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <returns>Number of found files.</returns>
|
|
public long File(string directoryName, string searchPattern, int daysOld, bool recursive)
|
|
{
|
|
//Löscht alle Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfFiles = 0;
|
|
long tempNumberOfFiles = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern) && DaysOld(fileName, daysOld))
|
|
{
|
|
numberOfFiles++;
|
|
}
|
|
}
|
|
|
|
if (recursive)
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
tempNumberOfFiles = File(dirName, searchPattern, daysOld, recursive);
|
|
|
|
numberOfFiles = numberOfFiles + tempNumberOfFiles;
|
|
tempNumberOfFiles = 0;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfFiles;
|
|
}
|
|
#endregion
|
|
|
|
#region Directory
|
|
/// <summary>
|
|
/// Counts all directories in a directory.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <returns>Number of found directories.</returns>
|
|
public long Directory(string directoryName)
|
|
{
|
|
//Zählt das angegebene Verzeichnis
|
|
long numberOfDirectories = 0;
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
numberOfDirectories++;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all directories in a directory that correspond to a particular pattern sequence.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <returns>Number of found directories.</returns>
|
|
public long Directory(string directoryName, string searchPattern)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfDirectories = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern))
|
|
{
|
|
numberOfDirectories++;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all directories in a directory that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All files are counted.</param>
|
|
/// <returns>Number of found directories.</returns>
|
|
public long Directory(string directoryName, string searchPattern, int daysOld)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfDirectories = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern) && DaysOld(dirName, daysOld))
|
|
{
|
|
numberOfDirectories++;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all directories and all subdirectories in a directory that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All files are counted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <returns>Number of found directories.</returns>
|
|
public long Directory(string directoryName, string searchPattern, int daysOld, bool recursive)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen
|
|
//die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfDirectories = 0;
|
|
long tempNumberOfDirectories = 0;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern) && DaysOld(dirName, daysOld))
|
|
{
|
|
numberOfDirectories++;
|
|
}
|
|
}
|
|
|
|
if (recursive)
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
tempNumberOfDirectories = Directory(dirName, searchPattern, daysOld, recursive);
|
|
|
|
numberOfDirectories = numberOfDirectories + tempNumberOfDirectories;
|
|
tempNumberOfDirectories = 0;
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return numberOfDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Counts all directories and all subdirectories in a directory and the directory itself that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All files are counted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <param name="countItself">True=Add the directory itself to the counter, false=The directory itself is not added to the counter.</param>
|
|
/// <returns></returns>
|
|
public long Directory(string directoryName, string searchPattern, int daysOld, bool recursive, bool countItself)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen
|
|
//die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
long numberOfDirectories = 0;
|
|
|
|
numberOfDirectories = Directory(directoryName, searchPattern, daysOld, recursive);
|
|
|
|
if (countItself)
|
|
{
|
|
numberOfDirectories++;
|
|
}
|
|
|
|
return numberOfDirectories;
|
|
}
|
|
#endregion
|
|
|
|
#region Subroutines
|
|
/// <summary>
|
|
/// Checks whether the search pattern is included in the file name.
|
|
/// </summary>
|
|
/// <param name="fileName">A valid file name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <returns>The search pattern is included in the file name=true, otherwise=false.</returns>
|
|
private bool SearchPatternFile(string fileName, string searchPattern)
|
|
{
|
|
string tempFileName = "";
|
|
string tempExtension = "";
|
|
string tempPattern = "";
|
|
string tempPattern1 = "";
|
|
bool match = false;
|
|
try
|
|
{
|
|
if (searchPattern == "")
|
|
{
|
|
match = true;
|
|
}
|
|
else if (searchPattern.StartsWith(".") | searchPattern.StartsWith("*."))
|
|
{
|
|
//Wenn mit '*', dann '*' entfernen
|
|
if (searchPattern.StartsWith("*"))
|
|
{
|
|
searchPattern = searchPattern.Replace("*", "");
|
|
}
|
|
|
|
//Nach Datei Erweiterung suchen
|
|
if (Path.GetExtension(fileName).ToLower() == searchPattern.ToLower())
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.Contains(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Anfang Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.StartsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.Contains("."))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
tempPattern = tempPattern.Remove(tempPattern.IndexOf("."));
|
|
tempExtension = Path.GetExtension(fileName).ToLower();
|
|
string test = searchPattern.Remove(0, searchPattern.IndexOf("."));
|
|
|
|
if (tempFileName.EndsWith(tempPattern) & searchPattern.Remove(0, searchPattern.IndexOf(".")) == tempExtension)
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*"))
|
|
{
|
|
//Nach Ende Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.EndsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.Contains("*"))
|
|
{
|
|
//Nach Anfang und Ende Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Remove(searchPattern.IndexOf("*"));
|
|
tempPattern1 = searchPattern.Remove(0, searchPattern.IndexOf("*") + 1);
|
|
|
|
if (tempFileName.StartsWith(tempPattern) & tempFileName.EndsWith(tempPattern1))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
match = false;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
match = false;
|
|
}
|
|
|
|
return match;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the search pattern is included in the directory name.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <returns>The search pattern is included in the directory name=true, otherwise=false.</returns>
|
|
private bool SearchPatternDirectory(string directoryName, string searchPattern)
|
|
{
|
|
string tempDirectoryName = "";
|
|
string tempPattern = "";
|
|
string tempPattern1 = "";
|
|
bool match = false;
|
|
try
|
|
{
|
|
if (searchPattern == "")
|
|
{
|
|
match = true;
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.Contains(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Anfang Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.StartsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*"))
|
|
{
|
|
//Nach Ende Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.EndsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.Contains("*"))
|
|
{
|
|
//Nach Anfang und Ende Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Remove(searchPattern.IndexOf("*"));
|
|
tempPattern1 = searchPattern.Remove(0, searchPattern.IndexOf("*") + 1);
|
|
|
|
if (tempDirectoryName.StartsWith(tempPattern) & tempDirectoryName.EndsWith(tempPattern1))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
match = false;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
match = false;
|
|
}
|
|
|
|
return match;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks how old the file is.
|
|
/// </summary>
|
|
/// <param name="fileName">A valid file or directory name.</param>
|
|
/// <param name="daysOld">Days how old the element must be. 0=All.</param>
|
|
/// <returns>True=Is older as 'daysOld', otherwies=false.</returns>
|
|
private bool DaysOld(string fileName, int daysOld)
|
|
{
|
|
int calcDays = 0;
|
|
bool ok = false;
|
|
|
|
if (daysOld <= 0)
|
|
{
|
|
ok = true;
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
//Das Alter (letzter Zugriff) der Datei errechnen
|
|
DateTime dt = System.IO.File.GetLastAccessTime(fileName);
|
|
TimeSpan sp = DateTime.Now - dt;
|
|
calcDays = sp.Days;
|
|
|
|
if (calcDays > daysOld)
|
|
{
|
|
//Wenn die Datei älter als 'daysOld'-Tage ist
|
|
ok = true;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
ok = false;
|
|
}
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
#endregion
|
|
|
|
#region Event Handler
|
|
// Der Delegat muß die gleiche Signatur aufweisen wie die Eventhandler-Methode.
|
|
public delegate void EventDelegate(int result, string status);
|
|
|
|
// Das Event-Objekt ist vom Typ dieses Delegaten.
|
|
public event EventDelegate DelStatus;
|
|
|
|
public void OnEvent(int result, string status)
|
|
{
|
|
// Prüft ob das Event überhaupt einen Abonnenten hat.
|
|
if (DelStatus != null)
|
|
{
|
|
DelStatus(result, status);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|