SSIS script is aborting sometimes with Directory Not Empty error message.

ViVe_72 5 Reputation points
2023-06-15T23:01:37.24+00:00

Recently we have been getting an error with our SSIS package. Error happens sometimes, not always. The package has been running without any error since begining. SSIS Script Task Error - Failed to compiled scripts contained in the package. Open the package in SSIS Designer and resolve the compilation errors. The directory is not empty.

The script task deletes files in a directory. It is not deleting the folder.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,620 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Yitzhak Khabinsky 25,296 Reputation points
    2023-06-16T00:40:04.0233333+00:00

    Hi @ViVe_72,

    You didn't explain your logic, and didn't provide samples of SSIS variables values.

    I made few general improvements:

    1. Introduced Path.Combine()
    2. Introduced Select Case statement.
    3. Added error handling via Try/Catch statement with error logging.
    Sub Main
    	Dim InputFilePath As String = Dts.Variables.Item("InputFilePath").Value.ToString
    
    	CreateALLFile(InputFilePath, "EALLEB.V1", "E?????EB.V*", "EB")
    
    	CreateALLFile(InputFilePath, "EALLEJ.V1", "E?????EJ.V*", "EJ")
    	
    	Dts.TaskResult = ScriptResults.Success
    
    End Sub
    
    Private Sub CreateALLFile(ByVal folderPath As String, ByVal delfile As String, ByVal srhfile As String, ByVal fileflag As String)
    
    	Dim localFile As String
    	Dim fileContent As String()
    
    	Try
    
    		'Dim outputFilePath As String = folderPath + delfile
    		Dim outputFilePath As String = Path.Combine(folderPath, delfile)
    
    		For Each localFile In Directory.EnumerateFiles(folderPath, delfile)
    			File.Delete(localFile)
    		Next
    
    		For Each localFile In Directory.EnumerateFiles(folderPath, srhfile)
    			fileContent = File.ReadAllLines(localFile)
    			File.AppendAllLines(outputFilePath, fileContent)
    			Dts.Variables.Item("FileExists").Value = True
    
    			'		If fileflag = "EB" Then
    			'			Dts.Variables.Item("ebfile").Value = True
    			'		End If
    			'
    			'		If fileflag = "EJ" Then
    			'			Dts.Variables.Item("ejfile").Value = True
    			'		End If
    
    			Select Case fileflag
    				Case "EB"
    					Dts.Variables.Item("ebfile").Value = True
    				Case "EJ"
    					Dts.Variables.Item("ejfile").Value = True
    				Case Else
    					'???
    			End Select
    		Next
    	Catch ex As Exception
    		Dts.Events.FireError(-1, "CreateALLFile()", ex.Message, "", -1)
    		Dts.TaskResult = ScriptResults.Failure
    	End Try
    End Sub