Overview
I wanted to create a small console app that would point at a given directory, evaluate the last accessed date of each file within that directory, and if the last access date was below a certain threshold, archive that file. The archive folder structure should match that of the source directory. I also decided to output the source folder structure to a log file, with an "ARCHIVED" flag against all those files that were archived.
Request
If you wouldn't mind reviewing my code for inefficiencies, mistakes, duplications, and anything else you think could be improved upon.
Note
I have included a decompress function which is not utilised in the script, but I needed for testing purposes.
Code
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Archiving
{
class Program
{
static void Main(string[] args)
{
string sourcePath = @"e:\source\";
string destPath = @"e:\archive";
string logPath = @"e:\log files\";
string currentDate = DateTime.Now.ToShortDateString().Replace("/", "");
using (StreamWriter streamWriter = File.CreateText(logPath + currentDate + ".txt"))
{
try
{
string[] directories = Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories);
foreach (string directory in directories)
{
streamWriter.WriteLine(directory);
string[] dirFiles = Directory.GetFiles(directory, "*.*", SearchOption.TopDirectoryOnly);
foreach (string file in dirFiles)
{
FileInfo fileInfo = new FileInfo(file);
if (FileExpiry(fileInfo))
{
string trimmedDirPath = fileInfo.DirectoryName.Substring(sourcePath.Length);
string archiveDirPath = Path.Combine(destPath, trimmedDirPath);
// if directory doesn't exist in archive, create
if (!Directory.Exists(archiveDirPath))
Directory.CreateDirectory(archiveDirPath);
// compress file
Compress(fileInfo);
// if file exists, append datetime to file name
if (File.Exists(Path.Combine(archiveDirPath, fileInfo.Name + ".gz")))
File.Move(fileInfo.FullName + ".gz", Path.Combine(archiveDirPath, currentDate + fileInfo.Name + ".gz"));
else
File.Move(fileInfo.FullName + ".gz", Path.Combine(archiveDirPath, fileInfo.Name + ".gz"));
// remove original file
File.Delete(fileInfo.FullName);
// append 'ARCHIVED' to files that have expired
streamWriter.WriteLine(" " + Path.GetFileName(file) + " - Archived");
}
else
{
streamWriter.WriteLine(" " + Path.GetFileName(file));
}
}
}
}
catch (DirectoryNotFoundException ex)
{
streamWriter.WriteLine(ex.Message);
}
catch (IOException ex)
{
streamWriter.WriteLine(ex.Message);
}
catch (Exception ex)
{
streamWriter.WriteLine(ex.Message);
}
}
}
public static bool FileExpiry(FileInfo fileInfo)
{
if (fileInfo.LastAccessTime < DateTime.Now.Date.AddDays(-5))
return true;
else
return false;
}
public static void Compress(FileInfo fileToCompress)
{
using (FileStream fileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream,
CompressionMode.Compress))
{
fileStream.CopyTo(compressionStream);
}
}
}
}
}
public static void Decompress(FileInfo fileToDecompress)
{
using (FileStream fileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(fileStream,
CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
}
}
}