SSIS - The process cannot access the file because it is being used by another process
Asked Answered
D

11

13

I have following Dataflow:

enter image description here

Control:

enter image description here

I just wanted to copy all the data from flatfiles in sourcefolder to sql database and after copying move those files to folder named Done.

But when i run this, i get error:

[File System Task] Error: An error occurred with the following error message: "The process cannot access the file because it is being used by another process.".

Data gets copied to sqlserver , but file does not moves.

My process tab is as follows:

enter image description here

Dialytic answered 18/12, 2013 at 13:13 Comment(1)
Is there any chance you also have that file open for reading? See also File in use by another processPhytophagous
B
5

This may be too obvious, but have you tried inserting a short delay to give the dataflow time to let go of the file? For example, insert an Execute SQL Task with contents like:

-- Wait for 1 second
WAITFOR DELAY '00:00:01'

Alternatively, you could handle the Failure Path and retry, perhaps after a delay.

Blowzed answered 18/12, 2013 at 14:25 Comment(0)
P
11

If the messages cites your ".ispac" file, you have an unclosed debug. Enter Task Manager and close the Debug Host.

Petrology answered 6/7, 2017 at 8:50 Comment(1)
The Debug Host's Image Name in Windows Task Manager is "DtsDebugHost.exe" and Description is "SSIS Debug Host"Mislay
R
8

My solution:

  1. Go to Task Manager
  2. Details Tab.
  3. Locate the process “DtsDebugHost.exe“.
  4. Kill this process. There might be multiple instances of this process. Kill all of them.
  5. Reexecute SSIS package
Racket answered 7/10, 2020 at 8:33 Comment(1)
That's a good one, sir! In my case it was dtexec.exe process I had to kill PS> get-process dtexec.exe | stop-process -forceCharisecharisma
S
7

I found this link by accident and posting this to help others that get here as well.

When using a Script task make sure you drop connection with Close() or use connection inside a USING().

The connection is held after the task is complete and until the whole package is complete unless you do either of the above.

Strepitous answered 12/5, 2017 at 16:12 Comment(0)
B
5

This may be too obvious, but have you tried inserting a short delay to give the dataflow time to let go of the file? For example, insert an Execute SQL Task with contents like:

-- Wait for 1 second
WAITFOR DELAY '00:00:01'

Alternatively, you could handle the Failure Path and retry, perhaps after a delay.

Blowzed answered 18/12, 2013 at 14:25 Comment(0)
S
3

If you are using an Excel connection, use the below code (C#) in a Script task to close all Excel processes, before you attempt to move/rename the file.

System.Diagnostics.Process[] proc=System.Diagnostics.Process.GetProcessesByName("Excel");
foreach (System.Diagnostics.Process p in proc)
{
    if (!string.IsNullOrEmpty(p.ProcessName))
    {
        try
        {
            p.Kill();
        }
        catch { }
    }
}
Stall answered 2/12, 2017 at 10:24 Comment(0)
C
2

If you are using Excel connection from SSIS connection Manager. There is a property RetainSameConnection. It is true by default. Make it False and you will be all set.

Calumniation answered 13/5, 2014 at 18:27 Comment(0)
A
1

for me this error has nothing to do with SSIS and occurred after end tasking not responding BIDS

one of my excel files was get locked by BIDS debugger(DtsDebugHost.exe) which closing BIDS does not helps.

finally using http://filehippo.com/download_unlocker/ shows me actual locker and also simply remove it.

please note that BIDS needs to be restarted after unlocking DtsDebugHost.exe

Ardene answered 15/5, 2014 at 12:33 Comment(0)
U
1

I use this function in tandem with a wait routine:

public bool IsFileLocked(string filePath)
{
    try
    {
        //Open the file exclusively
        using (File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { }
    }
    catch (IOException e)
    {
        var errorCode = Marshal.GetHRForException(e) & ((1 << 16) - 1);
        return errorCode == 32 || errorCode == 33;
    }

    return false;
}
Uzzia answered 17/8, 2016 at 15:43 Comment(0)
F
0

Add a script task executing below mentioned lines, before doing file operation:

Public Sub Main()

Dim procList() As Process = Process.GetProcesses()

Dim k As Integer

For k = 0 To procList.GetUpperBound(0) Step k + 1

If procList(k).ProcessName = "EXCEL" Then

procList(k).Close()

procList(k).Dispose()

End If

Next

GC.Collect()

GC.WaitForPendingFinalizers()

Dts.TaskResult = ScriptResults.Success

End Sub
Frankhouse answered 14/2, 2019 at 10:58 Comment(0)
D
0

In my case, I had previously opened a Flat File with System.IO.StreamReader but, after processing, forgot to close the Reader!


    Dim oReader As New StreamReader(sPathAndFileName)
    ...
    oReader.Close()
    oReader = Nothing
Dahlberg answered 13/7 at 0:4 Comment(0)
D
-1

You can easily "File System Task" component.

Image of Your Control Flow:

You can pass the name of your current file as a parameter to "File System Task" component

Image for File System Task Setting

I tested it myself and everything was ok!

Daze answered 7/10, 2020 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.