environment: Visual Studio 2019, Windows Forms Application, C#
Hi! I would like to rename (batch) some files with #1, #2, #3 etc. I have folders like 1-file, 2-file and inside each folder i have around around 50 000 file with names: abc.png, def.png, ghi.png I want: abc#1.png, def#2.png, ghi#3.png
Something similar...
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowDialog();
string[] pngFiles = Directory.GetFiles(folderDlg.SelectedPath, "*.png");
string[] newFileName = new string[pngFiles.Length];
for (int i = 0; i < pngFiles .Length; i )
{
string filePath = System.IO.Path.GetDirectoryName(pngFiles [i]);
string fileExt = System.IO.Path.GetExtension(pngFiles[i]);
newFileName = pngFiles [i];
File.Move(pngFiles [i], filePath "\\" newFileName[1] 1 fileExt);`
}
But this code doesn't work. Error here...
Also, I am not sure i can implement the increase counter and include that number to the file preceding #
Please let me know
CodePudding user response:
You should use Directory.EnumerateFiles
to improve the performance: when you use Directory.GetFiles
, you must wait for the whole array of names to be returned before you can access the array, whereas Directory.EnumerateFiles
will return file by file.
Also use string interpolation and FileSystem.RenameFile
to gracefully rename the file.
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.ShowDialog();
if (folderDlg.ShowDialog() != DialogResult.OK)
{
return;
}
// Has different framework dependend implementations
// in order to handle unauthorized access to subfolders
RenamePngFiles(folderDlg.SelectedPath);
}
.NET Core/.NET
private void RenamePngFiles(string directoryPath)
{
int fileNameSuffixCounter = 1;
foreach (string originalFullFileName in Directory.EnumerateFiles(directoryPath, "*.png", new EnumerationOptions()))
{
// The new file name without path
var newFileName = $"{Path.GetFileNameWithoutExtension(originalFullFileName)}#{fileNameSuffixCounter }{Path.GetExtension(originalFullFileName)}";
FileSystem.RenameFile(originalFullFileName, newFileName);
}
}
.NET Framework
private void RenamePngFiles(string directoryPath)
{
int fileNameSuffixCounter = 1;
foreach (string originalFullFileName in EnumerateFiles(directoryPath, "*.png", SearchOption.AllDirectories))
{
// The new file name without path
var newFileName = $"{Path.GetFileNameWithoutExtension(originalFullFileName)}#{fileNameSuffixCounter }{Path.GetExtension(originalFullFileName)}";
FileSystem.RenameFile(originalFullFileName, newFileName);
}
}
private IEnumerable<string> EnumerateFiles(
string directoryPath,
string searchPattern,
SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
IEnumerable<string> filePaths = new List<string>();
try
{
filePaths = Directory.EnumerateFiles(directoryPath, searchPattern);
}
catch (UnauthorizedAccessException)
{
yield break;
}
foreach (string filePath in filePaths)
{
yield return filePath;
}
if (searchOption.Equals(SearchOption.TopDirectoryOnly))
{
yield break;
}
foreach (string subdirectoryPath in Directory.EnumerateDirectories(directoryPath))
{
foreach (string filePath in EnumerateFiles(subdirectoryPath, searchPattern))
{
yield return filePath;
}
}
}