I am using the below code, to rename all the files in a folder with a timestamp. But it is throwing an exception.
Please suggest any solutions.
using System.IO;
public void Main()
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\\Users\\AShubhankar\\Desktop\\archive_feb");
//if the director exists then proceed
if (directoryInfo.Exists)
{
var fileList = directoryInfo.GetFiles();
foreach (FileInfo fleInfo in fileList)
{
var newFileName = GetNewFileName(fleInfo);
//copies the new file names
fleInfo.CopyTo(newFileName, true);
//delete the old/original files
fleInfo.Delete();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
MessageBox.Show("Directory not found");
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
// Function to get the new file name
private static string GetNewFileName(FileInfo fleInfo)
{
var shortDate = DateTime.Now.ToShortDateString().Replace("/", string.Empty);
var format = string.Format("{0}_{1}", shortDate);
var extension = ".txt";
return Path.Combine(fleInfo.DirectoryName,
string.Concat(fleInfo.Name.Split('.')[0], "_", format, extension));
}
Exception thrown: