Skip to content

Instantly share code, notes, and snippets.

@giltesa
Last active May 25, 2017 20:06
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 giltesa/cb8dcd07d2e7a2f8c4e570d79c934c5e to your computer and use it in GitHub Desktop.
Save giltesa/cb8dcd07d2e7a2f8c4e570d79c934c5e to your computer and use it in GitHub Desktop.
/*
* giltesa.com
* http://www.forocoches.com/foro/showthread.php?t=5646307#post263939450
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace RenameSaves
{
class RenameSaves
{
static Regex regex = new Regex(@"\w+[_]\w+\.(jpe?g|png|gif|bmp)"); //17016_T4_01_DIG_3256.jpg
static DirectoryInfo[] outDirsInfo;
static void Main(string[] args)
{
String exeName = System.AppDomain.CurrentDomain.FriendlyName.Replace(".vshost", "");
String workingPath = System.Reflection.Assembly.GetEntryAssembly().Location.Replace(exeName, "");
String inPath = Path.Combine(workingPath, "INPUT");
String outPath = Path.Combine(workingPath, "OUTPUT");
DirectoryInfo inPathInfo = new DirectoryInfo(inPath);
DirectoryInfo outPathInfo = new DirectoryInfo(outPath);
if( !inPathInfo.Exists || !outPathInfo.Exists )
throw new DirectoryNotFoundException("INPUT or OUTPUT directories do not exist.");
outDirsInfo = outPathInfo.GetDirectories();
DirectoryCopy(inPath, outPath, true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo inDirInf = new DirectoryInfo(sourceDirName);
if( !inDirInf.Exists )
throw new DirectoryNotFoundException("INPUT directories do not exist.");
FileInfo[] inFiles = inDirInf.GetFiles();
foreach( FileInfo inFile in inFiles)
{
if (regex.IsMatch(inFile.Name))
{
string parentPath = Directory.GetParent(destDirName).ToString();
string authorID = inFile.Name.Split(new string[] { "_" }, StringSplitOptions.None)[2];
string authorName = null;
string dirName = Path.GetFileName(destDirName);
foreach( DirectoryInfo outDirInfo in outDirsInfo )
{
if( outDirInfo.Name.Contains(authorID) )
{
authorName = outDirInfo.Name;
break;
}
}
if( authorName != null )
{
string tempPath = Path.Combine(parentPath, authorName, dirName);
if (!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath);
tempPath = Path.Combine(tempPath, inFile.Name);
inFile.CopyTo(tempPath, true);
}
}
}
if( copySubDirs )
{
DirectoryInfo[] inDirs = inDirInf.GetDirectories();
foreach ( DirectoryInfo subdir in inDirs )
DirectoryCopy(subdir.FullName, Path.Combine(destDirName, subdir.Name), copySubDirs);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment