简体   繁体   中英

How can I fix my code to move files that already exist to another directory?

This is my first C# script and first non-SQL based script in general.I'm really proud of it (and I couldn't have done it this quickly without help from this community, thanks!), but I know it's going to be all kinds of messy.

The script loops through all files in a single directory, removes special characters from the file names, and renames the files to a standard, user-friendly, name. The script is looking for a specific set of files in the directory. If it finds a file that isn't supposed to be in the directory, it moves the file to a safe folder and renames it. If the folder

I'm working with 4 files that have dynamic names that will include numbers and special characters. The renaming process happens in two steps:

  1. Remove special characters and numbers from the name. Ex: From "EOY 12.21.2018 - 12.28.2018 PRF.xls" to "EOYPRF.xls"

  2. Rename the file to clearly label what the file is. Ex: From "EOYPRF.xls" to "EOY_CompanyName.xls"

There may be files added to this directory by accident, and since they are payroll files, they are highly confidential and cannot be moved unless they need to be moved (only if they are one of the 4 files), so I move them to a subdirectory in the same directory the files are stored in and rename them.

I am also trying to account for if my script or process messes up midway. This script is part of a larger automation process run in SSIS, so there are many failure points. It may be possible that the script fails and leaves one or all of the 4 files in the directory. If this is the case, I need to move the file out of the main directory before the user adds new, unaltered master files to be processed. If the directory contains files of the same final name ("EOY_CompanyName.xls") then it will not work properly.

I'm testing the script by placing the three scenario in the directory.

  1. 2 files that are not in any way associated with the 4 master files.
  2. 4 unaltered master files formatted with numbers and special characters: "EOY 12.21.2018 - 12.28.2018 PRF.xls"
  3. 4 master files already in their final state (simulating a failure before the files are moved to their final directory). Ex: "EOY_CompanyName.xls"

The problem I'm facing is in the rare scenario where there are both unaltered master files and final master files in the directory, the script runs up until the first unaltered file, removes the special characters, then fails at the final renaming step because a file already exists with the same name (Scenario 3 from the 3 points above). It'll then continue to run the script and will move one of the master files into the unexpected file directory and stop processing any other files for some reason. I really need some help from someone with experience.

I've tried so many things, but I think it's a matter of the order in which the files are processed. I have two files named "a.xls" and "b.xls" which are placeholders for unexpected files. They are the first two files in the directory and always get processed first. The 3rd file in the directory is the file named above in its unaltered form ("EOY 12.21.2018 - 12.28.2018 PRF.xls"). It gets renamed and moved into the unexpected files folder, but really it should be passed over to move the master files containing the final name ("EOY_CompanyName.xls") into the unexpected folder. I want to make sure that the script only processes new files whenever it's run, so I want to move any already processed files that failed to get moved via the script into another directory.

public void Main()
    {
        ///Define paths and vars
        string fileDirectory_Source = Dts.Variables["User::PayrollSourceFilePath"].Value.ToString();
        string fileDirectory_Dest = Dts.Variables["User::PayrollDestFilePath"].Value.ToString();
        string errorText = Dts.Variables["User::errorText"].Value.ToString();
        DirectoryInfo dirInfo_Source = new DirectoryInfo(fileDirectory_Source);
        DirectoryInfo dirInfo_Dest = new DirectoryInfo(fileDirectory_Dest);
        string finalFileName = "";

        List<string> files = new List<string>(new string[]
            {
            fileDirectory_Source + "EOY_PRF.xls",
            fileDirectory_Source + "EOY_SU.xls",
            fileDirectory_Source + "FS_PRF.xls",
            fileDirectory_Source + "FS_SU.xls"
            });

        Dictionary<string, string> fileNameChanges = new Dictionary<string, string>();
            fileNameChanges.Add("EOYReportPRF.xls", "EOY_PRF.xls");
            fileNameChanges.Add("PayrollEOY.xls", "EOY_SU.xls");
            fileNameChanges.Add("PRFFundingStatement.xls", "FS_PRF.xls");
            fileNameChanges.Add("SUFundingStatement.xls", "FS_SU.xls");

        ///Determine if all files present
        int count = dirInfo_Source.GetFiles().Length;
        int i = 0;

        ///Loop through directory to standardize file names
        try
        {
            foreach (FileInfo fi in dirInfo_Source.EnumerateFiles())
            {

                string cleanFileName = Regex.Replace(Path.GetFileNameWithoutExtension(fi.Name), "[0-9]|[.,/ -]", "").TrimEnd() + fi.Extension;
                File.Move(fileDirectory_Source + Path.GetFileName(fi.Name), fileDirectory_Source + cleanFileName);
                ///Move unexpectd files in source directory
                if (!fileNameChanges.ContainsKey(cleanFileName))
                    {
                        errorText = errorText + "Unexpected File: " + cleanFileName.ToString() + " moved into the Unexpected File folder.\r\n";

                        File.Move(dirInfo_Source + cleanFileName, dirInfo_Source + "Unexpected Files\\" + Path.GetFileNameWithoutExtension(cleanFileName) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + fi.Extension);

                }
                if (fileNameChanges.ContainsKey(cleanFileName))
                    {
                        ///Final Friendly File Name from Dict
                        var friendlyName = fileNameChanges[cleanFileName];
                        ///Handle errors produced by files that already exist
                        if (files.Contains(fileDirectory_Source + friendlyName))//File.Exists(fileDirectory_Source + friendlyName))
                            {
                                MessageBox.Show("File.Exists(dirInfo_Source + friendlyName)" + File.Exists(dirInfo_Source + friendlyName).ToString() + "   cleanFileName   " + cleanFileName);
                                errorText = errorText + "File already exists: " + friendlyName.ToString() + " moved into the Unexpected File folder.\r\n";
                                File.Move(dirInfo_Source + friendlyName, dirInfo_Source + "Unexpected Files\\" + Path.GetFileNameWithoutExtension(friendlyName) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(friendlyName));
                                return;
                            }
                        ///Rename files to friendly name
                        File.Move(dirInfo_Source + cleanFileName, dirInfo_Source + friendlyName);
                        finalFileName = friendlyName.ToString();
                    }
                ///Count valid PR files
                if (files.Contains(dirInfo_Source + finalFileName))
                    {
                    i++;
                    }
            }
            ///Pass number of files in source folder to SSIS
            Dts.Variables["User::FilesInSourceDir"].Value = i;
        }
        catch (Exception ex)
        {
            errorText = errorText + ("\r\nError at Name Standardization step: " + ex.Message.ToString()) + $"Filename: {finalFileName}\r\n";
        }

        ///Search for missing files and store paths
        try
        {
            if (i != 4)
            {
                var errors = files.Where(x => !File.Exists(x)).Select(x => x);
                if (errors.Any())
                    errorText = (errorText + $" Missing neccessary files in PR Shared drive. Currently {i} valid files in directory.\r\n\n" + "Files missing\r\n" + string.Join(Environment.NewLine, errors) + "\r\n");
            }
        }
        catch (Exception ex)
        {
            errorText = errorText + ("Error at Finding Missing PR Files step: " + ex.Message.ToString()) + "\r\n\n";
            throw;
        }

        ///Loop through directory to move files to encrypted location
        try
        {
            if (i == 4)
                foreach (FileInfo fi in dirInfo_Source.EnumerateFiles())
                {
                    fi.MoveTo(fileDirectory_Dest + Path.GetFileName(fi.FullName));
                }
        }
        catch (Exception ex)
        {
            errorText = errorText + ("Error at Move Files to Encrypted Directory step: " + ex.Message.ToString()) + "\r\n";
        }
        Dts.TaskResult = (int)ScriptResults.Success;
        Dts.Variables["User::errorText"].Value = errorText;
    }

    #region ScriptResults declaration
    /// <summary>
    /// This enum provides a convenient shorthand within the scope of this class for setting the
    /// result of the script.
    /// 
    /// This code was generated automatically.
    /// </summary>
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion

}

}

I would ideally like to move all files that are in the folder before the files need to be cleaned and renamed so I dont receive errors or commit records to the database that already exist.

If you made it this far, thank you for your time and I appreciate you taking the hour it probably took to read this. You are a hero.

As I understand you want to move out any of the "4 short names" if they already exist before doing anything else. I would go with below, please note, I did not run the code.. I hope I understood you correct

///Loop through directory to standardize file names
            try
            {
                //Cleanup source folder 
                foreach (string fileShortName in files)
                {
                    if (File.Exists(fileDirectory_Source + fileShortName))
                    {
                        //Time to move the file, its old
                        errorText = errorText + "Old File: " + fileShortName + " moved into the Old File folder.\r\n";

                        File.Move(dirInfo_Source + fileShortName, dirInfo_Source + "Old Files\\" + Path.GetFileNameWithoutExtension(fileShortName) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + Path.GetExtension(fileShortName));
                    }
                }

                foreach (FileInfo fi in dirInfo_Source.GetFiles())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM