I am utlizing a scripting object in my ssis to combine two text files into one final file, and then I want to delete the original files. To do this I am utilizing the FileSystemInfo namespace and associating the file names, then utilizing the DELETE functionality.
The creation of the final file works perfectly...unfortunately, my base files do not delete, and I do not get a failure message or indictator.
Here is my code:
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.Imports System
Imports System.Data
Imports System.Math
Imports System.IO
Imports System.IO.File
Imports System.IO.FileSystemInfo
Imports Microsoft.SqlServer.Dts.RuntimePublic Class ScriptMain
' The execution engine calls this method when the task executes.
' To access the object model, use the Dts object. Connections, variables, events,
' and logging features are available as static members of the Dts class.
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'
' To open Code and Text Editor Help, press F1.
' To open Object Browser, press Ctrl+Alt+J.Public Sub Main()
Dim strCurrentMonth As String
Dim strCurrentYear As String
Dim strWriteFileName As String
Dim strReadHeaderFileName As String
Dim strReadBodyFileName As String'Utilizing a case statement, determine the monthname & year and set the appropriate variables
Select Case Month(Now())
Case 1
strCurrentMonth = "January"
Case 2
strCurrentMonth = "February"
Case 3
strCurrentMonth = "March"
Case 4
strCurrentMonth = "April"
Case 5
strCurrentMonth = "May"
Case 6
strCurrentMonth = "June"
Case 7
strCurrentMonth = "July"
Case 8
strCurrentMonth = "August"
Case 9
strCurrentMonth = "September"
Case 10
strCurrentMonth = "October"
Case 11
strCurrentMonth = "November"
Case 12
strCurrentMonth = "December"
End SelectstrCurrentYear = Year(Now()).ToString
'Set variables with file names (reader files and write file) for ease in readability and to
'set final (write file) with appropriate nameing convention utilized by Matria HealthCare.strWriteFileName = "\\CUPSRV05\SHARED\IS\Public\Data Export\Matria\Files TO Matria\cup_ref_cup_" & strCurrentMonth & strCurrentYear & "_ftp_ReferralFormat.txt"
strReadHeaderFileName = "\\CUPSRV05\SHARED\IS\Public\Data Export\Matria\Files TO Matria\Matria_Referral_Control.txt"
strReadBodyFileName = "\\CUPSRV05\SHARED\IS\Public\Data Export\Matria\Files TO Matria\Matria_Referral.txt"
'create stream reader/writer objects
Dim sr As New StreamReader(strReadHeaderFileName)
Dim sr2 As New StreamReader(strReadBodyFileName)
Dim sw As New StreamWriter(strWriteFileName)'feed the header record into the final file
Do Until sr.Peek = -1
'write the header record
sw.WriteLine(sr.ReadLine)
Loop'close the read stream for the header record file
sr.Close()'Feed the body records into the final file
Do Until sr2.Peek = -1
'write all base records
sw.WriteLine(sr2.ReadLine)
Loop'close the read stream for the body records
sr2.Close()'close the write stream for the final distribution file
sw.Close()'dispose of all stream objects
sr.Dispose()
sr2.Dispose()
sw.Dispose()Dim EligBaseFile As New FileInfo("strReadBodyFileName")
Dim EligHeaderFile As New FileInfo("strReadHeaderFileName")EligBaseFile.Delete() <--These do not delete or through an error
EligHeaderFile.Delete()'final statement for SSIS package to determine script result
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
I would appreciate any light you can shed on this. Thanks!
I have also posted this in the Visual Basic Language forum. But, again, any help/guidance would be appreciated.
|||hi,
My issue going beyond of yours because of I am not be able even to read my file...
Do Until sFitxer.Peek = -1
sFitxer3.WriteLine(sFitxer.ReadLine)
Loop
--
Dim line As String
Do
line = sFitxer.ReadLine
sFitxer3.WriteLine(line)
Loop Until line Is Nothing
Neither of them works.
any help will be welcomed.
|||Dim EligBaseFile As New FileInfo("strReadBodyFileName")
Dim EligHeaderFile As New FileInfo("strReadHeaderFileName")
EligBaseFile.Delete() <--These do not delete or through an error
EligHeaderFile.Delete()
The problem is on the first two lines. You are passing strReadBodyFileName and strReadHeaderFileName as string values rather than variables. Remove the quotes around them.
|||
You could always simplify the script you have as well:
File.WriteAllText(varForCombinedFile, File.ReadAllText(strReadHeaderFileName))
File.AppendAllText(varForCombinedFile, File.ReadAllText(strReadBodyFileName))
File.Delete(strReadHeaderFileName)
File.Delete(strReadBodyFileName)
Hope this helps.
|||Also you can use String.Format("{0:MMMM}", DateTime.Now) to derive the Long month name instead of the Select Case.
No comments:
Post a Comment