Advertisemen
Due to my work being done in C# to automate processes, I needed to do a repetitive task of renaming lots of directories from nnn xxxxxxx.. to xxxxxxxxx, where n represents a digit and x a letter. It's so simple to do. I'm really starting to like the rapid development C# allows for!
Program.cs
using System;
using System.Linq;
namespace FolderRenamer
{
class Program
{
static void Main(string[] args)
{
Str s = new Str();
String dir = "C:\\...\\";
String[] Files = System.IO.Directory.GetDirectories(dir);
for (int i=0;i<Files.Count();i++)
System.IO.Directory.Move(Files[i],dir + s.Convert(s.Cull(Files[i])));
}
}
}
Str.cs
using System;
using System.Linq;
namespace FolderRenamer
{
class Str
{
public String Convert(String s)
{
return s.Substring(4);
}
public static string Reverse(string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public String Cull(String s)
{
String n = "";
for (int i = s.Length - 1; i >= 0 && s[i] != '\\'; i--)
n += s[i];
return Reverse(n);
}
}
}
Advertisemen
Tidak ada komentar:
Posting Komentar