Atul Shubhankar
Atul Shubhankar

Reputation: 43

SSIS renaming files in a directory using script task

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

Upvotes: 4

Views: 6407

Answers (1)

Hadi
Hadi

Reputation: 37368

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

Upvotes: 2

Related Questions