Problem deleting a created file in Script task

  • Hello all,

    This seems pretty straight forward but I can't figure out why it's happening for the life of me. I have a package which has a script task. All I want to do is create a file then delete that same file. The file gets created just fine. The problem i'm having is when I attempt to delete the file. I get an exception that states that "the file is being used by another process." When the file gets created, I'm not sure why the code is maintaing a hold on the file which of course is preventing me from deleting the same file I just created. Can someone shed some light as to why this is happening and maybe an alternative solution? Below is my code.

    Note: I've also tried the File.create(filename) and File.delete(filename) and I get the same result.

    Public Sub Main()

    Try

    Dim MyFileInfo As FileInfo = New FileInfo(Dts.Variables("FileLocation").Value & Dts.Variables("Filename").Value)

    MyFileInfo.Create()

    MyFileInfo.Delete()

    Dts.TaskResult = ScriptResults.Success

    Catch ex As Exception

    Dts.TaskResult = ScriptResults.Failure

    MessageBox.Show(ex.GetType.FullName)

    End Try

    End Sub

  • Not sure why you would want to do that, but try to put delay between the create and delete. you are right its not releasing the handle to the file in the background and moved on to the next step.

    HTH

  • You will need to close the file stream before attempting the delete.

    Try something like this:

    Dim folder as string = CStr(Dts.Variables("FileLocation").Value)

    Dim fileName as string = CStr(Dts.Variables("Filename").Value)

    Dim filePath As String = System.IO.Path.Combine(folder, fileName)

    Dim fs As System.IO.FileStream = System.IO.File.Create(filePath)

    fs.Close()

    System.IO.File.Create(filePath)

  • stricknyn (5/26/2009)


    Hello all,

    This seems pretty straight forward but I can't figure out why it's happening for the life of me. I have a package which has a script task. All I want to do is create a file then delete that same file. The file gets created just fine. The problem i'm having is when I attempt to delete the file. I get an exception that states that "the file is being used by another process." When the file gets created, I'm not sure why the code is maintaing a hold on the file which of course is preventing me from deleting the same file I just created. Can someone shed some light as to why this is happening and maybe an alternative solution? Below is my code.

    Note: I've also tried the File.create(filename) and File.delete(filename) and I get the same result.

    Public Sub Main()

    Try

    Dim MyFileInfo As FileInfo = New FileInfo(Dts.Variables("FileLocation").Value & Dts.Variables("Filename").Value)

    MyFileInfo.Create()

    MyFileInfo.Delete()

    Dts.TaskResult = ScriptResults.Success

    Catch ex As Exception

    Dts.TaskResult = ScriptResults.Failure

    MessageBox.Show(ex.GetType.FullName)

    End Try

    End Sub

    Blimey what a frustrating few minutes I just had trying to solve this! The following solution worked for me - needed to release the MyFileInfo handle and introduce a delay:

    Public Sub Main()

    Try

    Dim MyFileInfo As FileInfo = New FileInfo("c:\windows\temp\temp.tmp")

    MyFileInfo.Create()

    MyFileInfo = Nothing

    Delay(5)

    File.Delete("c:\windows\temp\temp.tmp")

    Dts.TaskResult = Dts.Results.Success

    Catch ex As Exception

    Dts.TaskResult = Dts.Results.Failure

    MsgBox(ex.GetType.FullName & ": " & ex.Message)

    End Try

    End Sub

    Sub Delay(ByVal dblSecs As Double)

    Dim dblWaitTil As Date

    dblWaitTil = Now.AddSeconds(dblSecs)

    Do Until Now > dblWaitTil

    DoEvents() ' Allow windows messages to be processed

    Loop

    End Sub

    You'll also need an imports to make the DoEvents work.

    Why are you doing this??

    Phil

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Hi,

    I'm doing this to ensure that the value entered in the package by users is a valid filename. Creating the file in the try catch block will catch the exception if the filename isn't valid when it tries to create the file. I can then handle the exception appropriately.

    I know I can write code to validate the variable directly, but I think the best way to validate if a file name is valid is to create the file. After successfully creating it, I just need to delete it, which of course was where I was having the issue.

    Thanks,

    Strick

  • Ahhh, makes sense.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Note that creating the file does not only validate the full file path (folder + filename). It also does other things like checking that the account the code is running under has the necessary permissions.

  • Yeah I absoulutely agree with that. That was more or less why I chose the option to create the file. My plan was to have mutiple catch blocks to get the main 2 or 3 possible exceptions and the general one to catch the rest.

    Strick

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply