简体   繁体   English

SSIS脚本任务,用于根据文件扩展名移动文件

[英]SSIS Script Task for moving files based on their file extension

I have the following code contained within a Script Task in SSIS 2012. 我在SSIS 2012的脚本任务中包含以下代码。

public void Main()
    {
        string inputDir = (string) Dts.Variables["User::InputDirectory"].Value;
        string CSVFolder = (string) Dts.Variables["User::CSVFolder"].Value;
        string XMLFolder = (string) Dts.Variables["User::XMLFolder"].Value;
        string XLSXFolder = (string) Dts.Variables["User::XLSXFolder"].Value;
        bool isXMLFolderEmpty = (bool) Dts.Variables["User::isXMLFolderEmpty"].Value;
        bool isCSVFolderEmpty = (bool)Dts.Variables["User::isCSVFolderEmpty"].Value;
        bool isXLSXFolderEmpty = (bool)Dts.Variables["User::isXLSXFolderEmpty"].Value;

        string[] fileNames = Directory.GetFiles(@inputDir);
        if (fileNames.Length > 0)
        {
            foreach (string inputFile in fileNames)
            {
                string FileExtension = Path.GetExtension(inputFile);

                if (FileExtension == ".csv")
                {
                    File.Move(inputDir + "\\" + inputFile, CSVFolder + "\\" + inputFile);
                    isCSVFolderEmpty = false;
                }
                else if (FileExtension == ".xlsx")
                {
                    File.Move(inputDir + "\\" + inputFile, XLSXFolder + "\\" + inputFile);
                    isXLSXFolderEmpty = false;
                }
                else if (FileExtension == ".xml")
                {
                    File.Move(inputDir + "\\" + inputFile, XMLFolder + "\\" + inputFile);
                    isXMLFolderEmpty = false;
                }
            }
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }

However when I execute the Script task I am getting the following error: DTS Script Task has encountered an exception in User code: Exception has been thrown by the target of invocation. 但是,当我执行脚本任务时,出现以下错误:DTS脚本任务在用户代码中遇到异常:调用目标已引发异常。

Can anybody point out what is going wrong? 有人可以指出出什么问题了吗? All of variable names are correct. 所有变量名都是正确的。

File.Move is incorrect File.Move不正确

You might also be interested in the Path.Combine as it will more gracefully handle building paths than your blind string concatenation route. 您可能还对Path.Combine感兴趣,因为它比盲目字符串连接路径更能优雅地处理构建路径。

Directory.GetFile indicates Directory.GetFile指示

Returns the names of files (including their paths) in the specified directory 返回指定目录中的文件名(包括它们的路径)

so inputFile is already going to be in the form of C:\\ssisdata\\so_35605920\\Foo.csv so your initial parameter to FileMove is simply inputFile 因此inputFile已经采用C:\\ssisdata\\so_35605920\\Foo.csv的形式,因此FileMove的初始参数就是inputFile

So really the challenge becomes changing the folder path out of the inputFile variable to the targeted one (CSV, XML, or XLSX). 因此,真正的挑战在于将文件夹路径从inputFile变量更改为目标变量(CSV,XML或XLSX)。 Lazy hack would be to call the string.replace method specifying the inputDir and using the new directory. 懒惰的黑客将是调用string.replace方法,以指定inputDir并使用新目录。 I'd probably go with something a bit more elegant in case there's weirdness with UNC or relative paths. 如果UNC或相对路径有些怪异,我可能会更优雅一些。

Path.GetFileName will give me the file and extension. Path.GetFileName将给我文件和扩展名。 So using that + Path.Combine would yield the correct final path for our Move operation 因此,使用该+ Path.Combine将为我们的Move操作产生正确的最终路径

Path.Combine(CSVFolder, Path.GetFileName(inputFile));

Thus 从而

File.Move(inputFile, Path.Combine(CSVFolder, Path.GetFileName(inputFile)));

Visual studio is rather unhappy at the moment so pardon the lack of precision in the suggested code but if there are typos, you have the references to books online and the logic behind it. Visual Studio目前不满意,请原谅建议的代码缺乏精确性,但是如果有错别字,您可以在线参考书籍及其背后的逻辑。

Also, try/catch blocks are exceptionally helpful in defensive coding as will be testing to ensure the folders exist, there's no process with a lock on the file, etc. 此外,try / catch块在防御性编码中非常有用,因为它将进行测试以确保文件夹存在,没有进程锁定文件等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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