117 lines
5.2 KiB
C#
117 lines
5.2 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FSI.Lib.Guis.Pdf.Mgt
|
|
{
|
|
public class Cmds
|
|
{
|
|
public static Task DelDirectoriesAsync(string path)
|
|
{
|
|
return Task.Factory.StartNew(() =>
|
|
{
|
|
Action<string> DelPath = null;
|
|
DelPath = p =>
|
|
{
|
|
Directory.EnumerateFiles(p).ToList().ForEach(System.IO.File.Delete); // Dateien im Verzeichnis löschen
|
|
Directory.EnumerateDirectories(p).ToList().ForEach(DelPath);
|
|
Directory.EnumerateDirectories(p).ToList().ForEach(Directory.Delete); // Verzeichnis löschen
|
|
};
|
|
|
|
if (Directory.Exists(path))
|
|
{
|
|
string[] directories = Directory.GetDirectories(path); // Alle Unterverzeichniss auslesen
|
|
|
|
foreach (string directory in directories)
|
|
{
|
|
if (!(directory.Replace(path, "")).StartsWith(".")) // Überprüfen ob Verzeichnis mit "." startet
|
|
{
|
|
DelPath(directory); // Unterverzeichnisse inkl. Dateien löschen
|
|
Directory.Delete(directory); // Verzeichnis löschen
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public static Task DelFilesAsync(string path)
|
|
{
|
|
return Task.Factory.StartNew(() =>
|
|
{
|
|
var files = Directory.GetFiles(path);
|
|
|
|
foreach (string file in files)
|
|
{
|
|
FileInfo fileInfo = new FileInfo(file);
|
|
|
|
const int STR_LENGTH = 27; // min. Dateinamen Länge (Zahlen inkl. Bindestriche)
|
|
|
|
if (!fileInfo.Name.StartsWith(".")) // Überprüfen ob Datei mit "." startet
|
|
{
|
|
if (fileInfo.Extension != ".pdf" || fileInfo.Name.Length <= STR_LENGTH) // Überprüfen ob es sich um eine PDF-Datei handelt
|
|
{
|
|
System.IO.File.Delete(fileInfo.FullName); // ... wenn nicht Datei löschen
|
|
}
|
|
else
|
|
{
|
|
string[] fileStrings = fileInfo.Name.Substring(0, STR_LENGTH - 1).Split('-'); // Zahlenblock splitten für Überprüfung
|
|
|
|
if (fileStrings.Length == 3) // 3 Zahlenblöcke vorhanden?
|
|
{
|
|
if (fileStrings[0].Length != 10 || !Int64.TryParse(fileStrings[0], out _) || fileStrings[1].Length != 10 || !Int64.TryParse(fileStrings[1], out _) || fileStrings[2].Length != 4 || !Int64.TryParse(fileStrings[2], out _)) // Länge der Zahlenblöcke überprüfen
|
|
{
|
|
System.IO.File.Delete(fileInfo.FullName); // ..., wenn Länge nicht passt, Datei löschen
|
|
}
|
|
}
|
|
else
|
|
{
|
|
System.IO.File.Delete(fileInfo.FullName); // ..., wenn nicht Datei löschen
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public static Task CreatePdfShortcutsAsync(string path)
|
|
{
|
|
return Task.Factory.StartNew(() =>
|
|
{
|
|
var files = Directory.GetFiles(path, "*.pdf"); // alle PDF-Dateien einlesen
|
|
|
|
foreach (var file in files)
|
|
{
|
|
FileInfo fileInfo = new FileInfo(file);
|
|
|
|
string[] fileStrings = System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name).Split(' '); // Datei-Namen für Verzeichnis Bezeichnung aufteilen
|
|
|
|
string tmpPath = path + fileStrings[1] + " " + fileStrings[2]; // Verzeichnis Bezeichnung zusammenstellen
|
|
|
|
if (Convert.ToBoolean(ConfigurationManager.AppSettings["SubDir"])) // mit/ohne Unterverzeichnis
|
|
{
|
|
tmpPath = path + fileStrings[1] + @"\" + fileStrings[2]; // mit Unterverzeichnis
|
|
}
|
|
else
|
|
{
|
|
tmpPath = path + fileStrings[1] + " " + fileStrings[2]; // ohne Unterverzeichnis
|
|
}
|
|
|
|
Directory.CreateDirectory(tmpPath); // Verzeichnis erstellen
|
|
|
|
// Shortcut erstellen
|
|
if (Convert.ToBoolean(ConfigurationManager.AppSettings["WithNo"]))
|
|
{
|
|
LnkParser.ShortCut.Create(System.IO.Path.GetFileNameWithoutExtension(tmpPath + "\\" + fileInfo.Name), tmpPath, fileInfo.FullName, System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName));
|
|
}
|
|
else
|
|
{
|
|
LnkParser.ShortCut.Create(System.IO.Path.GetFileNameWithoutExtension(fileInfo.Name.Replace(fileStrings[0], "")), tmpPath, fileInfo.FullName, System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|