Skip to content

Instantly share code, notes, and snippets.

@fagnercarvalho
Last active October 10, 2015 03:23
Show Gist options
  • Select an option

  • Save fagnercarvalho/47a9ac50198acb6e8cb0 to your computer and use it in GitHub Desktop.

Select an option

Save fagnercarvalho/47a9ac50198acb6e8cb0 to your computer and use it in GitHub Desktop.
Rename a bunch of files in a specific directory using a shift to increase or decrease their order. The files need to be in the following format: 00_file.txt
using System;
using System.IO;
var directory = @"C:\directory\";
var shift = 3;
foreach(var file in Directory.GetFiles(directory)) {
var fileName = Path.GetFileName(file);
var currentNumber = Int64.Parse(fileName.Substring(0, 2));
var newNumber = currentNumber + shift;
var newNumberAsString = string.Empty;
if (newNumber < 10) {
newNumberAsString = "0";
}
newNumberAsString = newNumberAsString + newNumber.ToString();
File.Move(file, string.Format("{0}{1}{2}", directory, newNumberAsString, fileName.Substring(2)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment