0
votes

I am trying to move csv files from one location to another based on create date.

But the code I using in not working it shows error that sample.csv is already being used by another process , how can I resolve this issue ?

this is the error message "Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: The process cannot access the file 'C:\New folder\Test.txt' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite) at ST_b8571e8d94a54a80ab50a1e221d93b11.vbproj.ScriptMain.Main() --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()"

 Public Sub Main()
        Dim csvFilesToday = New List(Of String)
        Dim sourceDir As String = "C:\New folder"
        Dim backupDir As String = "C:\New folder (2)"

        For Each csv In Directory.GetFiles(sourceDir, "*.csv", IO.SearchOption.AllDirectories)
            If File.GetCreationTime(csv).Date = Date.Today Then
                File.Copy(Path.Combine(sourceDir, csv), Path.Combine(backupDir, csv), True)
            End If
        Next
        Dts.TaskResult = ScriptResults.Success
    End Sub

End Class
2
What's the exact error?Joel Coehoorn

2 Answers

1
votes

You must identify the process that is holding a lock on the file.

I have a writeup on how to use process explorer to accomplish this: File in use by another process

The quick though though is to close Excel if you have it open. Otherwise, check your text editor(s)

0
votes

This won't fix your problem, but it will improve your code:

 Public Sub Main()
    Dim sourceDir As New DirectoryInfo("C:\New folder")
    Dim backupDir As String = "C:\New folder (2)"

    'Using DirectoryInfo.EnumerateFiles will reduce the total number of disk accesses
    For Each csv In sourceDir.EnumerateFiles("*.csv", SearchOption.AllDirectories.Where(Function(i) i.CreationTime >= Date.Today)
        Try
            File.Copy(csv.FullName, Path.Combine(backupDir, csv.Name), True)
        Catch Ex As IOException
            'Do something with the error here

        End Try
    Next csv

    Dts.TaskResult = ScriptResults.Success
End Sub