Dir Stat to CSV: mudanças entre as edições
Ir para navegação
Ir para pesquisar
(Criou página com '<syntaxhighlight lang=c#> using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace GetTreeDateAndSize.Tree { public class FileData...') |
Sem resumo de edição |
||
Linha 1: | Linha 1: | ||
=C#= | |||
<syntaxhighlight lang=c#> | <syntaxhighlight lang=c#> | ||
using System; | using System; | ||
Linha 4: | Linha 5: | ||
using System.IO; | using System.IO; | ||
using System.Linq; | using System.Linq; | ||
using System.Security.AccessControl; | |||
using System.Security.Principal; | |||
namespace GetTreeDateAndSize.Tree | namespace GetTreeDateAndSize.Tree | ||
Linha 68: | Linha 71: | ||
public class Tree | public class Tree | ||
{ | { | ||
public List<string> GetDirACL(string directory,bool explicitACL, bool inheritedACL) | |||
{ | |||
List<string> myACLs = new List<string>(); | |||
try | |||
{ | |||
DirectoryInfo myDir = new DirectoryInfo(directory); | |||
DirectorySecurity acls = myDir.GetAccessControl(AccessControlSections.All); | |||
AuthorizationRuleCollection rules = acls.GetAccessRules(explicitACL, inheritedACL, typeof(NTAccount)); | |||
foreach (AuthorizationRule rule in rules) | |||
{ | |||
myACLs.Add(rule.IdentityReference.ToString()); | |||
} | |||
} | |||
catch (Exception e) | |||
{ | |||
Console.WriteLine(e); | |||
throw; | |||
} | |||
return myACLs; | |||
} | |||
public IEnumerable<string> GetDirs(string directory) | public IEnumerable<string> GetDirs(string directory) | ||
{ | { | ||
Linha 203: | Linha 229: | ||
myData.SetWriteTime(this.GetLastDateTime(myData.GetWriteTime(),this.GetLastAccessDateTime(fileDatas))); | myData.SetWriteTime(this.GetLastDateTime(myData.GetWriteTime(),this.GetLastAccessDateTime(fileDatas))); | ||
myData.SetCreationTime(this.GetLastDateTime(myData.GetCreationTime(),this.GetLastAccessDateTime(fileDatas))); | myData.SetCreationTime(this.GetLastDateTime(myData.GetCreationTime(),this.GetLastAccessDateTime(fileDatas))); | ||
} | |||
string aclsInherited = ""; | |||
int aclInherited_count = 0; | |||
foreach (string acl in this.GetDirACL(directory,false,true)) | |||
{ | |||
if (aclInherited_count == 0) | |||
{ | |||
aclsInherited = acl; | |||
aclInherited_count = 1; | |||
} | |||
else | |||
{ | |||
aclsInherited = aclsInherited + "," + acl; | |||
} | |||
} | |||
string aclsExplicit = ""; | |||
int aclExplicit_count = 0; | |||
foreach (string acl in this.GetDirACL(directory,true,false)) | |||
{ | |||
if (aclExplicit_count == 0) | |||
{ | |||
aclsExplicit = acl; | |||
aclExplicit_count = 1; | |||
} | |||
else | |||
{ | |||
aclsExplicit = aclsExplicit + "," + acl; | |||
} | |||
} | } | ||
Console.WriteLine(directory + ";" + | Console.WriteLine(directory + ";" + | ||
Linha 208: | Linha 265: | ||
myData.GetAccessTime() + ";" + | myData.GetAccessTime() + ";" + | ||
myData.GetWriteTime() + ";" + | myData.GetWriteTime() + ";" + | ||
myData.GetCreationTime()); | myData.GetCreationTime() + ";" + | ||
aclsInherited + ";" + | |||
aclsExplicit); | |||
} | } | ||
catch (Exception e) | catch (Exception e) |
Edição atual tal como às 19h14min de 5 de abril de 2021
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
namespace GetTreeDateAndSize.Tree
{
public class FileData
{
public string dirPath;
public string name;
public double size;
public DateTime creationTime;
public DateTime accessTime;
public DateTime writeTime;
public void SetDirPath(string dirPath)
{
this.dirPath = dirPath;
}
public void SetName(string name)
{
this.name = name;
}
public void SetSize(double size)
{
this.size = size;
}
public void SetCreationTime(DateTime criationTime)
{
this.creationTime = criationTime;
}
public void SetAccessTime(DateTime accessTime)
{
this.accessTime = accessTime;
}
public void SetWriteTime(DateTime writeTime)
{
this.writeTime = writeTime;
}
public string GetDirPath()
{
return this.dirPath;
}
public string GetName()
{
return this.name;
}
public double GetSize()
{
return this.size;
}
public DateTime GetCreationTime()
{
return this.creationTime;
}
public DateTime GetAccessTime()
{
return accessTime;
}
public DateTime GetWriteTime()
{
return writeTime;
}
}
public class Tree
{
public List<string> GetDirACL(string directory,bool explicitACL, bool inheritedACL)
{
List<string> myACLs = new List<string>();
try
{
DirectoryInfo myDir = new DirectoryInfo(directory);
DirectorySecurity acls = myDir.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acls.GetAccessRules(explicitACL, inheritedACL, typeof(NTAccount));
foreach (AuthorizationRule rule in rules)
{
myACLs.Add(rule.IdentityReference.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return myACLs;
}
public IEnumerable<string> GetDirs(string directory)
{
IEnumerable<string> directories = new List<string>();
try
{
directories = Directory.EnumerateDirectories(directory, "*.*");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return directories;
}
public double GetDirTotalSize(List<FileData> fileDatas)
{
double totalSize = 0;
foreach (FileData fileData in fileDatas)
{
totalSize = totalSize + fileData.GetSize();
}
return totalSize;
}
public DateTime GetLastDateTime(DateTime t1, DateTime t2)
{
if (DateTime.Compare(t1, t2) > 0)
{
return t1;
}
else
{
return t2;
}
}
public DateTime GetLastAccessDateTime(List<FileData> fileDatas)
{
DateTime lastData = new DateTime(1900, 1, 1, 1, 0, 0);
foreach (FileData fileData in fileDatas)
{
lastData = this.GetLastDateTime(lastData, fileData.GetAccessTime());
}
return lastData;
}
public DateTime GetLastWriteDateTime(List<FileData> fileDatas)
{
DateTime lastData = new DateTime(1900, 1, 1, 1, 0, 0);
foreach (FileData fileData in fileDatas)
{
lastData = this.GetLastDateTime(lastData, fileData.GetWriteTime());
}
return lastData;
}
public DateTime GetLastCreateDateTime(List<FileData> fileDatas)
{
DateTime lastData = new DateTime(1900, 1, 1, 1, 0, 0);
foreach (FileData fileData in fileDatas)
{
lastData = this.GetLastDateTime(lastData, fileData.GetCreationTime());
}
return lastData;
}
public List<FileData> GetDirDetails(string directory)
{
List<FileData> fileDatas = new List<FileData>();
try
{
IEnumerable<string> myFiles = Directory.EnumerateFiles(directory, "*.*");
foreach (string myFile in myFiles)
{
FileInfo myFileInfo = new FileInfo(myFile);
FileData fileData = new FileData();
fileData.SetName(myFileInfo.Name);
fileData.SetSize(myFileInfo.Length);
fileData.SetDirPath(myFileInfo.DirectoryName);
fileData.SetAccessTime(myFileInfo.LastAccessTimeUtc);
fileData.SetWriteTime(myFileInfo.LastWriteTimeUtc);
fileData.SetCreationTime(myFileInfo.CreationTimeUtc);
fileDatas.Add(fileData);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return fileDatas;
}
public FileData GetDirStatCSV(string directory)
{
IEnumerable<string> directories = new List<string>();
FileData myData =new FileData();
myData.SetSize(0);
DateTime oldDate = new DateTime(1900, 1, 1, 1, 0, 0);
myData.SetCreationTime(oldDate);
myData.SetAccessTime(oldDate);
myData.SetWriteTime(oldDate);
try
{
directories = this.GetDirs(directory);
if (directories.Count() != 0)
{
foreach (string MyDir in directories)
{
FileData mySubDir = this.GetDirStatCSV(MyDir);
myData.SetSize(myData.GetSize() + mySubDir.GetSize());
myData.SetAccessTime(this.GetLastDateTime(myData.GetAccessTime(),mySubDir.GetAccessTime()));
myData.SetWriteTime(this.GetLastDateTime(myData.GetWriteTime(),mySubDir.GetWriteTime()));
myData.SetCreationTime(this.GetLastDateTime(myData.GetCreationTime(),mySubDir.GetCreationTime()));
}
List<FileData> fileDatas = this.GetDirDetails(directory);
myData.SetSize(myData.GetSize() + this.GetDirTotalSize(fileDatas));
myData.SetAccessTime(this.GetLastDateTime(myData.GetAccessTime(),this.GetLastAccessDateTime(fileDatas)));
myData.SetWriteTime(this.GetLastDateTime(myData.GetWriteTime(),this.GetLastAccessDateTime(fileDatas)));
myData.SetCreationTime(this.GetLastDateTime(myData.GetCreationTime(),this.GetLastAccessDateTime(fileDatas)));
}
else
{
List<FileData> fileDatas = this.GetDirDetails(directory);
myData.SetSize(myData.GetSize() + this.GetDirTotalSize(fileDatas));
myData.SetAccessTime(this.GetLastDateTime(myData.GetAccessTime(),this.GetLastAccessDateTime(fileDatas)));
myData.SetWriteTime(this.GetLastDateTime(myData.GetWriteTime(),this.GetLastAccessDateTime(fileDatas)));
myData.SetCreationTime(this.GetLastDateTime(myData.GetCreationTime(),this.GetLastAccessDateTime(fileDatas)));
}
string aclsInherited = "";
int aclInherited_count = 0;
foreach (string acl in this.GetDirACL(directory,false,true))
{
if (aclInherited_count == 0)
{
aclsInherited = acl;
aclInherited_count = 1;
}
else
{
aclsInherited = aclsInherited + "," + acl;
}
}
string aclsExplicit = "";
int aclExplicit_count = 0;
foreach (string acl in this.GetDirACL(directory,true,false))
{
if (aclExplicit_count == 0)
{
aclsExplicit = acl;
aclExplicit_count = 1;
}
else
{
aclsExplicit = aclsExplicit + "," + acl;
}
}
Console.WriteLine(directory + ";" +
myData.GetSize() + ";" +
myData.GetAccessTime() + ";" +
myData.GetWriteTime() + ";" +
myData.GetCreationTime() + ";" +
aclsInherited + ";" +
aclsExplicit);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return myData;
}
public void PrintTreeDir(string targetDirectory)
{
IEnumerable<string> directories = new List<string>();
try
{
directories = this.GetDirs(targetDirectory);
Console.WriteLine(targetDirectory);
if (directories.Count() != 0)
{
foreach (string dirObject in directories)
{
this.PrintTreeDir(dirObject);
}
this.PrintTreeFiles(targetDirectory);
}
else
{
this.PrintTreeFiles(targetDirectory);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void PrintTreeFiles(string targetDirectory)
{
try
{
var myFiles = Directory.EnumerateFiles(targetDirectory, "*.*");
foreach (var myFile in myFiles)
{
FileInfo fileData = new FileInfo(myFile);
Console.WriteLine(fileData.FullName + " " + fileData.LastAccessTimeUtc + " " + fileData.Length);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}