Skip to content

Instantly share code, notes, and snippets.

@apsun
Created October 19, 2015 23:15
Show Gist options
  • Select an option

  • Save apsun/c5fe6e00903140a778f4 to your computer and use it in GitHub Desktop.

Select an option

Save apsun/c5fe6e00903140a778f4 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace RenameDate
{
class Program
{
public static char PromptUntilValid(string question, params char[] validChars)
{
for (int i = 0; i < validChars.Length; ++i)
{
validChars[i] = char.ToUpperInvariant(validChars[i]);
}
while (true)
{
Console.Write(question);
char c = char.ToUpperInvariant(Console.ReadKey().KeyChar);
Console.WriteLine();
if (Array.IndexOf(validChars, c) >= 0) return c;
Console.WriteLine("Invalid input!");
}
}
public static void CreateDirIfNotExist(string dirPath)
{
if (Directory.Exists(dirPath)) return;
Directory.CreateDirectory(dirPath);
}
public static void EmptyDirectory(string dirPath)
{
if (!Directory.Exists(dirPath)) return;
Directory.Delete(dirPath, true);
Directory.CreateDirectory(dirPath);
}
public static FileInfo[] MoveContents(string srcDirPath, string destDirPath)
{
return MoveContents(srcDirPath, destDirPath, "*", SearchOption.TopDirectoryOnly, false);
}
public static FileInfo[] MoveContents(string srcDirPath, string destDirPath, string searchPattern,
SearchOption searchOption, bool preserveHeirarchy)
{
CreateDirIfNotExist(destDirPath);
var srcDirInfo = new DirectoryInfo(srcDirPath);
FileInfo[] fileInfos = srcDirInfo.GetFiles(searchPattern, searchOption);
bool useRelPaths = preserveHeirarchy && (searchOption == SearchOption.AllDirectories);
foreach (FileInfo f in fileInfos)
{
string relativeDestPath = useRelPaths ? GetRelativePath(srcDirPath, f.FullName) : f.Name;
f.MoveTo(Path.Combine(destDirPath, relativeDestPath));
}
return fileInfos;
}
public static string GetRelativePath(string baseDir, string fullPath)
{
string baseDirNormalized = Path.GetFullPath(baseDir + Path.DirectorySeparatorChar);
string baseDirNormalizedUpper = baseDirNormalized.ToUpperInvariant();
string fullPathNormalized = Path.GetFullPath(fullPath);
string fullPathNormalizedUpper = fullPathNormalized.ToUpperInvariant();
if (fullPathNormalizedUpper.IndexOf(baseDirNormalizedUpper, StringComparison.Ordinal) != 0)
{
throw new ArgumentException();
}
return fullPathNormalized.Remove(0, baseDirNormalized.Length);
}
static void Main()
{
const bool debug = true;
const string prefix = @"IMG_";
const string suffix = ".jpg";
const string format = "0000";
const string srcPath = @".\src\";
string destPath = @".\dest\";
string tempPath = @".\temp\";
if (debug)
Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
bool flag = false;
var listOfFiles = new List<FileInfo>();
if (!Directory.Exists(srcPath))
{
Console.WriteLine("Source folder does not exist!");
return;
}
if (Directory.Exists(destPath))
{
char res = PromptUntilValid("Destination folder exists. " +
"Delete/Combine/New? (D/C/N): ", 'D', 'C', 'N');
switch (res)
{
case 'D':
EmptyDirectory(destPath);
Debug.Print("Emptied destPath");
break;
case 'C':
int h = 0;
do
{
tempPath = @".\temp_" + h + @"\";
++h;
} while (Directory.Exists(tempPath));
Debug.Print("Created new tempPath: " + tempPath);
listOfFiles.AddRange(MoveContents(destPath, tempPath));
Debug.Print("Moved files in destPath to tempPath");
flag = true;
break;
case 'N':
int j = 0;
do
{
destPath = @".\dest_" + j + @"\";
++j;
} while (Directory.Exists(destPath));
Directory.CreateDirectory(destPath);
Debug.Print("Created new destPath: " + destPath);
break;
}
}
else
{
Directory.CreateDirectory(destPath);
}
var dir = new DirectoryInfo(srcPath);
listOfFiles.AddRange(dir.GetFiles());
var sorted = listOfFiles.OrderBy(o => o.LastWriteTimeUtc);
int i = 0;
foreach (FileInfo f in sorted)
{
string mid = f.LastWriteTime.ToString("yyyyMMdd_hhmmss");
bool success;
do
{
try
{
string name = prefix + mid + suffix;
Console.WriteLine(name);
f.CopyTo(destPath + name);
success = true;
}
catch
{
success = false;
mid = mid.Substring(0, mid.Length - 1) + (int.Parse(mid[mid.Length - 1].ToString()) + 1);
}
} while (!success);
Debug.Print("Copied " + f.Name);
}
if (flag) Directory.Delete(tempPath, true);
Debug.Print("Finished! Press ENTER to exit.");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment