Skip to content

Instantly share code, notes, and snippets.

@hexbinoct
Created January 9, 2018 06:29
Show Gist options
  • Save hexbinoct/c59fdc6cf193bd3da6fb91a23abd4c61 to your computer and use it in GitHub Desktop.
Save hexbinoct/c59fdc6cf193bd3da6fb91a23abd4c61 to your computer and use it in GitHub Desktop.
renaming file names
//code for renaming (padding) 5 parts file names:
static void Main(string[] args)
{
//list_none_5_parts(); return;
/* where ever this is , we have lots of files here, in the format
10-001-99-001, the 3rd section, which is 99, can have any number there
but whats imp is that it shud be composed of 3 digit length*/
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("files found {0}", files.Length);
int count = 0;
for (int i = 0; i < files.Length; i++)
{
string filename = files[i];
string name = Path.GetFileNameWithoutExtension(filename);
string ext = Path.GetExtension(filename);
string[] parts = name.Split('-');
if (parts.Length != 5)
continue;
parts[2] = parts[2].PadLeft(3, '0');
string newname = string.Join("-", parts);
string rename = Path.GetDirectoryName(filename) + "\\" + newname + ext;
if (!File.Exists(rename))
File.Move(filename, rename);
else
continue;
count++;
}
Console.WriteLine("files changed {0}", count);
}
//--------------------------------------------------------
//listing of files that dont have 4 dashes in their names:
static void list_none_5_parts()
{
string[] files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory);
int count = 0;
for (int i = 0; i < files.Length; i++)
{
string filename = files[i];
string name = Path.GetFileNameWithoutExtension(filename);
if (name.Split('-').Length != 5)
{
Console.WriteLine(name);
count++;
}
}
Console.WriteLine("total files {0}", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment