Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save miroslavradojevic/eed0421ae52022ba17614ac6134efb1b to your computer and use it in GitHub Desktop.
Save miroslavradojevic/eed0421ae52022ba17614ac6134efb1b to your computer and use it in GitHub Desktop.
Rename figure files in given directory, excluding dot and '/': ./fig/dir/file_param_1.1_param2.2.pdf into ./figdirfile_param1_1_param2_2.pdf For compiling latex project it was necessary to have all the files in one directory and retaining their unique name, at the same time replacing the '.' from file name.
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 1)
{
if (File.Exists(args[0]))
{
string parentDir = Directory.GetParent(args[0]).FullName;
StringBuilder fileContents = new StringBuilder();
using (StreamReader reader = new StreamReader(args[0]))
{
fileContents.Append(reader.ReadToEnd());
}
var tt = Regex.Matches(fileContents.ToString(), @"\{./fig/.*\}");
string fileContentsOut = fileContents.ToString();
int count = 0;
foreach (var t in tt)
{
string figPath = t.ToString().Replace("{", "").Replace("}", "");
Regex rgx = new Regex("[^a-zA-Z0-9]");
string figPathOut = rgx.Replace(figPath, "");
fileContentsOut = fileContentsOut.Replace(figPath, figPathOut);
figPath += (figPath.EndsWith(".pdf"))?"":".pdf";
figPathOut += (figPathOut.EndsWith(".pdf"))?"":".pdf";
var tryiti = figPath.TrimStart('.').Replace('/', Path.DirectorySeparatorChar);
string figSource = parentDir + tryiti;// Path.Combine(parentDir, tryiti);
string figDest = Path.Combine(parentDir, figPathOut);
if (File.Exists(figSource))
{
File.Copy(figSource, figDest, false);
count++;
}
else
{
Console.WriteLine("! " + figSource);
}
}
Console.WriteLine(count + " files.");
File.WriteAllText(args[0] + ".out", fileContentsOut);
}
}
Console.WriteLine("Press any key to stop...");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment