4

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:

Image of the error description

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • What is the exception? – Jeroen Heier Aug 25 '18 at 07:36
  • Why would you say 'throwing an exception'..... and not actually post the exception? Have you looked at the exception text yet? You might solve your own problem – Nick.Mc Aug 25 '18 at 09:55
  • I have added the snap. You can now, assess the problem. I am not able to figure it out! – Atul Shubhankar Aug 26 '18 at 09:30
  • @AtulShubhankar welcome to Stack overflow, i think that it is better to start by reading the [Tour Page](https://stackoverflow.com/tour),to learn more about the site rules (asking, answering, votes, accepting answers, ...). and to get the "informed" badge that let the other users know that you have read the rules. So you will get more help. Good Luck – Hadi Aug 26 '18 at 11:54
  • This is the SSIS exception error. Put your checkpoint in the c# code and step through it (F11). You will get a better error. – KeithL Aug 27 '18 at 13:46

1 Answers1

2

First of all, when using the @ before a string, you should not use \\ because the @ symbol is used to mark the string as a verbatim string literal. So anything in the string that would normally be interpreted as an escape sequence is ignored.

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\AShubhankar\Desktop\archive_feb");

Or you can use:

DirectoryInfo directoryInfo = new DirectoryInfo("C:\\Users\\AShubhankar\\Desktop\\archive_feb");    

Second, there is no need to Copy the file then delete the old one, just use the Move function to rename the file.


Also you can use the following code (simpler version):

public void Main()
{
    string strdirectory = @"C:\Users\AShubhankar\Desktop\archive_feb";
    //if the director exists then proceed
    if (Directory.Exists(strdirectory))
    {
        string[] fileList = Directory.GetFiles(strdirectory,"*.*",SearchOption.AllDirectories);

        foreach (string strfile in fileList)
        {
            string newFileName = GetNewFileName(strfile);
            //rename the new file
           File.Move(strfile, newFileName);

        }
        Dts.TaskResult = (int)ScriptResults.Success;
    }
    else
    {
        MessageBox.Show("Directory not found");
        Dts.TaskResult = (int)ScriptResults.Failure;
    }
}

// Function to get the new file name        
public string GetNewFileName(string strfile)
{
    string shortDate = DateTime.Now.ToString("yyyyMMdd_HHmmss");
    string extension = ".txt";
    return string.Concat(Path.GetDirectoryName(strfile),Path.GetFileNameWithoutExtension(strfile), "_", shortDate, extension);
}

References

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • 1
    @AtulShubhankar if this answer solved your problem you have to accept it. Just click on the mark below the voting arrows – Hadi Aug 27 '18 at 07:07
  • 1
    I was about to mention this exact issue, but of course you killed it with documentation! – KeithL Aug 27 '18 at 13:49