Skip to content

Instantly share code, notes, and snippets.

@nodew
Created April 9, 2023 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodew/9a80e217349ac31297785da166ffba2c to your computer and use it in GitHub Desktop.
Save nodew/9a80e217349ac31297785da166ffba2c to your computer and use it in GitHub Desktop.
using System.Text.RegularExpressions;
namespace ImageHelper.Shared;
public class ImageGrouper
{
private string _parentFolder;
private SearchOption _option;
private readonly List<string> _imageExtensions = new List<string> { ".jpg", ".jpeg", ".png", ".gif" };
public ImageGrouper(string parentFolder, SearchOption option = SearchOption.TopDirectoryOnly)
{
_parentFolder = parentFolder;
_option = option;
}
public void GroupImagesByCreateTime()
{
var imageFiles = GetAllImages();
foreach (var imageFile in imageFiles)
{
var createTime = GetImageCreationTime(imageFile);
var year = createTime.Year;
var month = createTime.Month;
var newFolder = Path.Combine(_parentFolder, year.ToString(), month.ToString().PadLeft(2, '0'));
MoveFileToNewFolder(newFolder, imageFile);
}
}
public void GroupImagesByFileName()
{
var imageFiles = GetAllImages();
foreach (var imageFile in imageFiles)
{
var yearAndMonth = ExtractYearAndMonthFromFileName(imageFile);
if (yearAndMonth != null)
{
var newFolder = Path.Combine(_parentFolder, yearAndMonth.Item1, yearAndMonth.Item2);
MoveFileToNewFolder(imageFile, newFolder);
}
}
}
public void GroupScreenshots()
{
var imageFiles = GetAllImages();
foreach (var imageFile in imageFiles)
{
if (IsScreenshotImage(imageFile))
{
var newFolder = Path.Combine(_parentFolder, "Screenshots");
MoveFileToNewFolder(imageFile, newFolder);
}
}
}
private void MoveFileToNewFolder(string imageFile, string newFolder)
{
if (!Directory.Exists(newFolder))
{
Directory.CreateDirectory(newFolder);
}
var newFileName = Path.Combine(newFolder, Path.GetFileName(imageFile));
if (!string.Equals(imageFile, newFileName))
{
File.Move(imageFile, newFileName);
}
}
private IEnumerable<string> GetAllImages()
{
return Directory.GetFiles(_parentFolder, "*", _option)
.Where(file => _imageExtensions.Any(ext => file.EndsWith(ext)));
}
private Tuple<string, string>? ExtractYearAndMonthFromFileName(string imageFile)
{
string? date = null;
var fileName = Path.GetFileName(imageFile);
var regex1 = new Regex(@"^(\d{8})_\d*_(iOS|Android)");
var regex2 = new Regex(@"^IMG_(\d{8})_\d*");
var match1 = regex1.Match(fileName);
var match2 = regex2.Match(fileName);
if (match1.Success)
{
date = match1.Groups[1].Value;
}
else if (match2.Success)
{
date = match2.Groups[1].Value;
}
if (!string.IsNullOrEmpty(date))
{
var year = date[0..4];
var month = date[4..6];
return new Tuple<string, string>(year, month);
}
return null;
}
private DateTime GetImageCreationTime(string imageFile)
{
var fileInfo = new FileInfo(imageFile);
return fileInfo.CreationTime;
}
private bool IsScreenshotImage(string imageFile)
{
var fileName = Path.GetFileName(imageFile);
var regex = new Regex(@"^Screenshot");
return regex.IsMatch(fileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment