Quantcast
Viewing all articles
Browse latest Browse all 24688

Deleting files from different folders (multiple locations) which are older than 10 days

Hi,

I am very new to MSBI and learning it. I have been trying to create a SSIS package which helps me to delete the text files which are older than 30 days from different folders. For this initially, I have tried to create a package which helps me to delete text files which are older than 30 days only from one folder, but I was facing some issues while running the package. I have used, foreach loop container, script task and file system task in it. I have done the coding using Micrososft Visual Studio 2008.

  1. Initially I have created 3 variables which are shown in the below screen shot

I have created three Variables:

Name :File , DataType : String, Value:

Name: FileDate, Data Type: DataTime, Value:

Name: FileSource, Data Type: String, Value: path of the folder

 2. After that I have taken foreach loop container and have given the below settings

In Collection tab:

Enumerator: Foreach File Enumerator

Folder: path of the folder

Files : *.txt

Retrieve File Name: Fully Qualified

In Variable Mappings:

Variable: User::File

Index  : 0

 3. I have dragged a script task into the ForEach loop Container and have given the following settings

ReadOnlyVariables: User::FileSource

ReadWriteVariables: User::File,User::FileDate

In the scipt task i have presented this in the code section

added Namespace System.IO

        public void Main()
        {
            // Create a variables 'container' to store variables            
            Variables vars = null;

            // Lock SSIS variables          
            Dts.VariableDispenser.LockForRead("User::File");
            Dts.VariableDispenser.LockForWrite("User::FileDate");

            // Add variables from the VariableDispenser to the variables 'container'
            Dts.VariableDispenser.GetVariables(ref vars);

            // Variable for file information            
            FileInfo fileInfo;

            // Fill fileInfo variable with file information            
            fileInfo = new FileInfo(vars["User::File"].Value.ToString());

            // Choose between creation date and lastmodified date            
            // Fill SSIS variable with last modified date            
            vars["User::FileDate"].Value = fileInfo.CreationTime;

            // Release the locks            
            vars.Unlock();

            // TODO: Add your code here
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

4. After doing this, I have linked Script task to a file system task inside the ForEach loop Container. The settings given in the foreach loop is given below

In General Tab:

Operation : Delete File

IsSourcePathVariable: True

SourceVarilable: User::File

 5. I have given a condition for the files deletion in the precedent constraint editor:

Evaluate operation:   Expression and Constraint

Value                 :   Success

Expression          :     DateDiff("dd",@[User::FileDate],@[System::StartTime])>30, 

CheckedLogical AND. All constraint must evaluate to true.

After giving all the conditons it fails :(

 6. When I am executing the code I am getting the below error:

Error: 0xC001405D at Script Task: A deadlock was detected while trying to lock variables "User::File" for read access and variables "User::FileDate" for read/write access. A lock cannot be acquired after 16 attempts. The locks timed out.
Error: 0x1 at Script Task: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: A deadlock was detected while trying to lock variables "User::File" for read access and variables "User::FileDate" for read/write access. A lock cannot be acquired after 16 attempts. The locks timed out.
 ---> System.Runtime.InteropServices.COMException (0xC001404D): A deadlock was detected while trying to lock variables "User::File" for read access and variables "User::FileDate" for read/write access. A lock cannot be acquired after 16 attempts. The locks timed out.

   at Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSVariableDispenser100.GetVariables(IDTSVariables100& ppVariables)
   at Microsoft.SqlServer.Dts.Runtime.VariableDispenser.GetVariables(Variables& variables)
   --- End of inner exception stack trace ---
   at Microsoft.SqlServer.Dts.Runtime.VariableDispenser.GetVariables(Variables& variables)
   at ST_7927eea4463a495eaa6e3a951b0450e7.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
Task failed: Script Task
Warning: 0x80019002 at Delete Archived Files: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (3) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Warning: 0x80019002 at Package: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (3) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.

  • I am littel bitconfused with the connection Manager, What should i mention in the connection manager, Is it required for this?

I need some help on this and correct my mistakes which i have done.

Also suggest, how to delete text files which are older than 30 days from multiple folders.


Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>