简体   繁体   中英

SSIS Script Task move all files not working

I'm using a Script Task (C#) within SSIS to move all .txt files from one folder to another. However, when I execute this, nothing happens. The Script Task actually reports success but the text files do not move. I'm using variables for all the paths - they all start with 2 backslashes and end with 1 back slash.

All I want to do is move all text files from the source destination to the destination folder.

What am I missing?

public void Main()
        {
            DirectoryInfo di = new DirectoryInfo(Dts.Variables["IN_Folder"].Value.ToString());

            string destinationFolder = Dts.Variables["User::IN_Folder_Processing"].Value.ToString();
            string sourceFolder = Dts.Variables["User::IN_Folder"].Value.ToString();


            FileInfo[] fi = di.GetFiles("*.txt");
            String filename = fi[0].Name;

            string sourceFileName = filename;
            string destinationFile = destinationFolder + sourceFileName;
            string sourceFile =sourceFolder + sourceFileName;

            if (File.Exists(destinationFile))
                File.Delete(destinationFile);
            // To move a file or folder to a new location:
            System.IO.File.Move(sourceFile, destinationFile);

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

Like I mentioned in the comments, there seems to be no need to use a Script Task to do this; a far better option would be a Foreach Loop Container and a FileSystem Task.

Firstly create the Foreach Loop Container, open the editor and go to the Collection Pane. Change Enumerator to Foreach File Enumerator. I assume you are using a variable for the directory, so click ... for Expressions and select Directory for the Property and your variable for the expression.

As you specifically want to deal with txt files, change Files to *.txt . What option you use for Retreive File Name depends on how your determining the destination. I'm going to assume you have another variable with the directory target, so select Name and extention .

Go to the Variable Mappings pane and select your filename variable, or create a new one. Leave the Index as 0 . This will store the Name and extension of the file you are going to move.

Create a new variable in your package, called OriginalFilePath , or something easily identifiable. Set the value to string and then change the Scope to your Foreach Loop Container. Now open the expression pane for the variable and set the expression to something like:

@[User::SourceDirectoryVariable] + "\\" + @[User::FileNameVariable]

Obviously change the variable names to what they need to be. Now create a second variable (same settings), however, using your destination directory variable instead of the source ( @[User::DestinationDirectoryVariable] + "\\\\" + @[User::FileNameVariable] ).

Now, in your Control Flow, create a File System Task within your Foreach Loop Container. Change Operation to Move File. Then fill in the rest of the pane as needed ( IsSourceVariable will be True, and then select your variable). Do the same for the destination, and then you should be good to go.

Any problems, please do comment with the error.

You should avoid manually concatenating file paths using the string + operator. This leaves a lot of room for error. Use System.IO.Path.Combine() . This ensures all leading and trialing slashes are formatted properly.

There's also no need to rebuild the file paths manually using all those additional variables. Something like this will work just fine so long as your input variable directories are correct:

public void Main()
    {
        DirectoryInfo di = new DirectoryInfo(Dts.Variables["IN_Folder"].Value.ToString());
        string destinationFolder = Dts.Variables["User::IN_Folder_Processing"].Value.ToString();
        FileInfo[] fi = di.GetFiles("*.txt");

        foreach (FileInfo f in fi)
        {
            FileInfo destinationFile = new FileInfo(Path.Combine(destinationFolder, f.Name));
            if (destinationFile.Exists)
                destinationFile.Delete();
            f.MoveTo(destinationFile.FullName);
        }

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

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