File is locked by SSIS script task that copies the file to a new location
I have a script task that copies a file to a new location in one control flow task and then after so dataflow tasks i return to the control flow and run a second script task that deletes the files from the original data source location. The last script task errors out with message that the files are in use by other process. Through testing I have isolated the other process to be the first script task that copied the files to a new loacation. How do I get the first task to relaease any file locks it created with the copyfile function when the task completes?
July 25th, 2012 4:19pm

1) Add a little pause with Thread.Sleep 2) Or create a WHILE loop to check if the file is still locked. Here is an example of that: http://microsoft-ssis.blogspot.com/2011/05/wait-until-file-is-unlocked.html Just wondering Is there a reason not to use the File System Task? You could replace the copy and delete by a move...Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2012 4:57pm

The script task is use more because of the ability to inspect the contents of the files then to move them. I am doing most of the testing in visual studio, but I have confirmed the same results with SQL agent. The file locking does not seem to time out, but persists until the package completes and exits. I am wondering, if I need to end the script task differently that is doing the file copy? The scripting language is VB.net douglas hawkins
July 25th, 2012 5:30pm

could you post (part of) your code?Please mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2012 5:35pm

'****************************************************** 'Retrieve and copy each new files to import directories '****************************************************** For Each objDirectory In My.Computer.FileSystem.GetDirectories(FolderPath, FileIO.SearchOption.SearchTopLevelOnly, "reload") DirectoryInfo = New FileInfo(objDirectory) 'MsgBox("Directory Info :" & DirectoryInfo.FullName) For Each objFile In My.Computer.FileSystem.GetFiles(DirectoryInfo.FullName, fileOption, "*.csv") '****************************************************** 'Write to VB variables '****************************************************** fileInfo = New FileInfo(objFile) 'MsgBox(fileInfo.FullName) FileName = fileInfo.FullName Filesize = CInt(fileInfo.Length) '****************************************************** 'Write VB variables to SSIS variables '****************************************************** Dts.Variables("FileName").Value = FileName 'MsgBox("File Size: " & fileInfo.Length) '****************************************************** 'Get Latest files '****************************************************** If fileInfo.CreationTime > DateAdd(DateInterval.Year, -10, LastImportedFileDate) Then 'MsgBox("File length" & fileInfo.Length.ToString) '****************************************************** 'Ignore small files sizes '****************************************************** If fileInfo.Length > 1999 Then 'Filter out files less then 2K in size, Write the file name to a log. i = i + 1 'read the first line of the file 'calculate the length to determing format Dim objReader As New System.IO.StreamReader(fileInfo.FullName.ToString) sFirstLine = objReader.ReadLine 'MsgBox("header row length" & sFirstLine.Length.ToString) 'MsgBox("First Char: " & sFirstLine.Chars(0)) '******************************************************************* 'Test for valid header row, if error ignor file and append text file '******************************************************************* If sFirstLine.Chars(0) = "D" Then '******************************************************************* 'Test each file header for current format. Paste files with expected 'formats in appropriate file share. Place unexpected file formats 'in xxx file share. Note: this is plant 1 or 10 as needed. '******************************************************************* 'FlatFile _150_Chars' If sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Chill Flow" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_150_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_150") 'FlatFile _218_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Chill Flow,12 Inch TC #1,12 Inch TC #2,Mold TC A,Mold TC B,Mold TC C,Mold TC D" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_218_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_218") 'FlatFile _361_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Vib 1Min Avg X,Vib 1Min Max X,Vib 1Min Avg Y,Vib 1Min Max Y,Vib 1Min Avg Z,Vib 1Min Max Z,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,GLD One Min Max Susc,GLD One Min Max Melt" Then Microsoft.VisualBasic.FileIO.FileSystem.copyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_361_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_361") 'FlatFile _412_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Z-Axis One Min Avg Vib,Z-Axis One Min Max Vib,Y-Axis One Min Avg Vib,Y-Axis One Min Max Vib,X-Axis One Min Avg Vib,X-Axis One Min Max Vib,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,Chill Plate Inlet Temp,Chill Plate Exit Temp" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_412_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_412") 'FlatFile _413_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Z-Axis One Min Avg Vib,Z-Axis One Min Max Vib,Y-Axis One Min Avg Vib,Y-Axis One Min Max Vib,X-Axis One Min Avg Vib,X-Axis3 One Min Max Vib,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,Chill Plate Inlet Temp,Chill Plate Exit Temp" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_413_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_413") 'FlatFile _429_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Vib 1Min Avg X,Vib 1Min Max X,Vib 1Min Avg Y,Vib 1Min Max Y,Vib 1Min Avg Z,Vib 1Min Max Z,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,12 Inch TC #1,12 Inch TC #2,Mold TC A,Mold TC B,Mold TC C,Mold TC D,GLD One Min Max Susc,GLD One Min Max Melt" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_429_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_429") 'FlatFile _454_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Z-Axis One Min Avg Vib,Z-Axis One Min Max Vib,Y-Axis One Min Avg Vib,Y-Axis One Min Max Vib,X-Axis One Min Avg Vib,X-Axis One Min Max Vib,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,Chill Plate Inlet Temp,Chill Plate Exit Temp,GLD One Min Max Susc,GLD One Min Max Melt" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_454_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_454") 'FlatFile _456_Chars' ElseIf sFirstLine = "Date,Time,Furnace,Caster,Item_No,Mold_No,MoldSheet,1 Inch TC #1,1 Inch TC #2,Ctrl TC A,Ctrl TC B,Ram Position,Ram Velocity,Susc KW,Melt Vac,Susc Std Dev,Z-Axis One Min Avg Vib,Z-Axis One Min Max Vib,Y-Axis One Min Avg Vib,Y-Axis One Min Max Vib,X-Axis One Min Avg Vib,X-Axis3 One Min Max Vib,CJ Temp,Melt Temp,Secondary LAND,Melt Power,Temposonic,Automation,Chill Flow,Chill Plate Inlet Temp,Chill Plate Exit Temp,GLD One Min Max Susc,GLD One Min Max Melt" Then Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_456_Chars\" & fileInfo.Name, True) 'MsgBox("FlatFile_456") 'MsgBox("Flat File Destination folder" & DestinationFolder & "\FlatFile_456_Chars\" & fileInfo.Name) Else Microsoft.VisualBasic.FileIO.FileSystem.CopyFile(fileInfo.FullName, DestinationFolder & "\FlatFile_xxx_Chars\" & fileInfo.Name, True) MsgBox("FlatFile_xxx") errorHandling(DestinationFolder, FileName, CStr(Filesize)) End If 'parce the file into storage containers Else errorHandling(DestinationFolder, FileName, CStr(Filesize)) End If 'starts with a "D" 'MsgBox(FolderPath) Else : streamError(DestinationFolder, FileName, Filesize.ToString) End If 'small files size Else : i = i j = j + 1 End If 'Date Next Next 'MsgBox("count of New Files:" & i) 'MsgBox("count of total Files:" & j) 'Dts.TaskResult = (int)ScriptResults.Success Dts.TaskResult = Dts.Results.Success Exit Sub errorhandle: '**** Lable for flow control to handle errors. Needs to be after Exit Sub statement in Main() routine***** errorHandling(DestinationFolder, FileName, CStr(Filesize)) Resume Next End Sub douglas hawkins
July 25th, 2012 5:44pm

close all objects at the end. For example objectreader.closePlease mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter
Free Windows Admin Tool Kit Click here and download it now
July 25th, 2012 6:26pm

close all objects at the end. For example objectreader.closePlease mark the post as answered if it answers your question | My SSIS Blog: http://microsoft-ssis.blogspot.com | Twitter
July 25th, 2012 6:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics