Question

I am using the below code to cycle through '.csv' files in a directory and move them to a new directory (strRootDir and strTargetDir are local variables that have been initiated):

Dim objFile As file
Dim objFSO As FileSystemObject:     Set objFSO = New FileSystemObject
Dim objFolder As Folder:            Set objFolder = objFSO.GetFolder(strRootDir)

For Each objFile In objFolder.Files

    If InStr(1, objFile.Name, ".csv") Then

        FileFolderExists strTargetDir, True
        objFile.Move (strTargetDir)

    End If

Next objFile

Set objFile = Nothing
Set objFolder = Nothing
Set objFSO = Nothing

Where the method 'FileFolderExists' is defined as:

Public Sub FileFolderExists(strFullPath As String, bMkDir As Boolean)
    Dim bExists As Boolean
    If Not Dir(strFullPath, vbDirectory) = vbNullString Then bExists = True
    If Not bExists And bMkDir Then MkDir strFullPath
End Sub

My question is that once this process has completed, if I try to delete the directory 'strTargetDir' I get an error message saying the folder is in use by another program.

How do I stop this from happening?

Was it helpful?

Solution

What you see is a Ghost File. The explorer has an uncanny habit of being stubborn :)

Here is another scenario which illustrates the "Ghost File"

Just issue a Dir command to remind explorer that the file no longer exists after you move the file and every thing will be ok :)

Ret = Dir(Path_And_FileName_Which_Was_Moved)

Also why use fso for moving files? Here is a one line command

Name "C:\Path1\File1.Ext" As "C:\Path2\File2.Ext"

This will also not leave a GHOST File

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top