VBA FileSystemObject showing files that no longer exist

Viewed 38

I am having some behaviour which seems very weird to me within VBA.

I have the following code:

Dim oFSO As FileSystemObject
Dim oFolder As Folder
Dim oFile As File

Set oFSO = New FileSystemObject
Set oFolder = oFSO.GetFolder(wbCases.Path & "\..\my\sub\path\to\photos")

For Each oFile In oFolder.Files
    If oFolder.Files.Count = 1 Then
        Name oFolder.Path & "\" & oFile.Name As oFolder.Path & "\" & "12345.jpg"
        oFSO.CopyFile oFolder.Path & "\12345.jpg", oFolder.Path & "\" & "1235.jpg"

    Else
        If oFolder.Files.Count > 1 Then
            Kill oFile
        End If
    End If
Next oFile

This code is supposed to check the contents of the folder specified in line

Set oFolder = oFSO.GetFolder(wbCases.Path & "\..\my\sub\path\to\photos")

which resolves to following existing folder on a network drive:

I:\root of Local Copy for building\my\sub\path\to\photos

If there is more than one file in this folder, I want to delete all the files until there is only one left. Then I want to copy this last file. However, this last step should be irrelevant, I would think, to the issue I am encountering, as I am encountering said issue before having deleted all but one file.

Namely, what is happening is that I am getting files which no longer exist in this directory.

E.g., when executing this code, I have five files in this folder, as confirmed by cmd:

I:\root of Local Copy for building\my\sub\path\to\photos>dir
 Volume in drive I is DE_User11
 Volume Serial Number is 009A-9A03

 Directory of I:\root of Local Copy for building\my\sub\path\to\photos

02.10.2019  09:43    <DIR>          .
02.10.2019  09:14    <DIR>          ..
19.08.2019  16:01         4.818.169 32993a - Copy (3).jpg
19.08.2019  16:01         4.818.169 32993a - Copy (3) - Copy - Copy.jpg
19.08.2019  16:01         4.818.169 32993a - Copy (4).jpg
19.08.2019  16:01         4.818.169 32993a - Copy (3) - Copy.jpg
19.08.2019  16:01         4.818.169 32993a.jpg
               5 File(s)     24.090.845 bytes
               2 Dir(s)     153.919.488 bytes free

However, when looping through the files in this folder using above-posted VBA code, I get, for example, my oFile variable filled with the following file:

I:\root of Local Copy for building\my\sub\path\to\photos\2229.jpg

This file used to exist, but I deleted it a couple of days ago, and, as stated above, it does not exist according to the dir command of cmd. Yet, my VBA code seems to think it exists, causing issues when trying to delete it using

Kill oFile

Why is this happening?

Update:

I have checked if the files VBA is finding are somehow hidden, and it seems they are. When I run

dir \a

on the folder, I get the following output

I:\root of Local Copy for building\my\sub\path\to\photos>dir /a
 Volume in drive I is DE_User11
 Volume Serial Number is 009A-9A03

 Directory of I:\root of Local Copy for building\my\sub\path\to\photos

02.10.2019  09:43    <DIR>          .
02.10.2019  09:14    <DIR>          ..
29.08.2019  09:41            19.456 2229.jpg
29.08.2019  09:41            19.456 2231.jpg
29.08.2019  09:41            19.456 2228.jpg
29.08.2019  09:41            19.456 2226.jpg
19.08.2019  16:01         4.818.169 32993a - Copy (3).jpg
29.08.2019  09:41            19.456 2224.jpg
29.08.2019  09:41            19.456 2225.jpg
29.08.2019  09:41            19.456 2227.jpg
19.08.2019  16:01         4.818.169 32993a - Copy (3) - Copy - Copy.jpg
19.08.2019  16:01         4.818.169 32993a - Copy (4).jpg
19.08.2019  16:01         4.818.169 32993a - Copy (3) - Copy.jpg
19.08.2019  16:01         4.818.169 32993a.jpg
29.08.2019  09:41            19.456 2224 - Copy.jpg
29.08.2019  09:41            19.456 2230.jpg
              14 File(s)     24.265.949 bytes
               2 Dir(s)     153.919.488 bytes free

However, I can not delete any of the hidden files:

I:\root of Local Copy for building\my\sub\path\to\photos>del 2229.jpg
Could Not Find I:\root of Local Copy for building\my\sub\path\to\photos\2229.jpg
0 Answers
Related