R
Ron Rosenfeld
I am trying to use the FileSystemObject to make a list of files which I am
going to delete (and also use it to delete them). These files are
characterized by names which have a certain unique pattern. I believe they
were caused by an ungraceful shutdown of a file backup/restore program,
although the cause is not relevant to this issue. Because I transport these
files between two computers, they are present on both my XP and Windows 7
systems. And there are about 10,000 of them.
On my XP machine, I used a recursive routine to list all the files in all the
subfolders, filtering the list by the file name pattern, and this worked fine.
However, on my Windows 7 machine, there are some apparent "folder shortcuts".
These folders have the "shortcut arrow" on their icon. At least some of them
seem to have been created by the system, to mimic folders which were present in
XP, but aren't normally present in W7. I assume this has something to do with
compatibility.
But I've run into a problem I don't know how to deal with.
In c:\users\ron\local settings, one of the subfolders appears to be a "shortcut
folder" named "Application Data".
Except for the fact that there is an arrow overlying the folder icon, I don't
see a "property" that allows me to tell that it is a shortcut and not a real
folder. So maybe it is real but just has a funny icon
It is causing a problem because this particular folder points to the folder in
which it exists, or to a separate deeper folder with the same content. In
other words, when I open it, it opens c:\users\ron\local settings. And one of
the subfolders listed in c:\users\ron\local settings is this Application Data
shortcut folder.
So a recursive routine which opens subfolders will also open this one, and keep
going deeper and deeper and deeper ...
Suggestions how to gracefully handle this problem would be appreciated.
I have shamelessly adapted the code from Chip Pearson's excellent site, and I
post my present modification below. It seems to work OK on the W7 machine
unless the issue mentioned above is present.
If TopFolderName is "c:\users\ron" (instead of the path name in the routine
below), then the sub fails with a "path not found" error.
?OfFolder in the immediate window returns:
C:\Users\Ron\AppData\Local\Application Data\Application Data\Application
Data\Application Data\Application Data\Application Data\Application
Data\Application Data\Application Data\Application Data\Application
Data\Application Data\Application Data\Adobe\Color
?OfFolder.name returns:
Color
and the folder attribute is 16 which is "Directory"
=================================
Option Explicit
Dim FSO As FileSystemObject
Dim RE As RegExp
Sub GetTildeFiles()
Dim TopFolderName As String
Dim TopFolderObj As Folder
Dim DestinationRange As Range
TopFolderName = "c:\users\ron\documents"
Set DestinationRange = Worksheets(1).Range("A1")
Cells.Clear
If FSO Is Nothing Then
Set FSO = New FileSystemObject
End If
If RE Is Nothing Then
Set RE = New RegExp
End If
RE.Pattern = "^[^~]+\.[^~]{3}~.+"
Set TopFolderObj = FSO.GetFolder(TopFolderName)
ListSubFolders OfFolder:=TopFolderObj, _
DestinationRange:=DestinationRange
Set RE = Nothing
Set FSO = Nothing
End Sub
'------------------------------------------
Sub ListSubFolders(OfFolder As Folder, _
DestinationRange As Range)
Dim SubFolder As Folder, f As File
For Each f In OfFolder.Files
If RE.Test(f.Name) = True Then
Set DestinationRange = DestinationRange.Offset(1, 0)
DestinationRange.Value = f.path
End If
Next f
For Each SubFolder In OfFolder.SubFolders
ListSubFolders OfFolder:=SubFolder, _
DestinationRange:=DestinationRange
Next SubFolder
End Sub
===============================
--ron
going to delete (and also use it to delete them). These files are
characterized by names which have a certain unique pattern. I believe they
were caused by an ungraceful shutdown of a file backup/restore program,
although the cause is not relevant to this issue. Because I transport these
files between two computers, they are present on both my XP and Windows 7
systems. And there are about 10,000 of them.
On my XP machine, I used a recursive routine to list all the files in all the
subfolders, filtering the list by the file name pattern, and this worked fine.
However, on my Windows 7 machine, there are some apparent "folder shortcuts".
These folders have the "shortcut arrow" on their icon. At least some of them
seem to have been created by the system, to mimic folders which were present in
XP, but aren't normally present in W7. I assume this has something to do with
compatibility.
But I've run into a problem I don't know how to deal with.
In c:\users\ron\local settings, one of the subfolders appears to be a "shortcut
folder" named "Application Data".
Except for the fact that there is an arrow overlying the folder icon, I don't
see a "property" that allows me to tell that it is a shortcut and not a real
folder. So maybe it is real but just has a funny icon
It is causing a problem because this particular folder points to the folder in
which it exists, or to a separate deeper folder with the same content. In
other words, when I open it, it opens c:\users\ron\local settings. And one of
the subfolders listed in c:\users\ron\local settings is this Application Data
shortcut folder.
So a recursive routine which opens subfolders will also open this one, and keep
going deeper and deeper and deeper ...
Suggestions how to gracefully handle this problem would be appreciated.
I have shamelessly adapted the code from Chip Pearson's excellent site, and I
post my present modification below. It seems to work OK on the W7 machine
unless the issue mentioned above is present.
If TopFolderName is "c:\users\ron" (instead of the path name in the routine
below), then the sub fails with a "path not found" error.
?OfFolder in the immediate window returns:
C:\Users\Ron\AppData\Local\Application Data\Application Data\Application
Data\Application Data\Application Data\Application Data\Application
Data\Application Data\Application Data\Application Data\Application
Data\Application Data\Application Data\Adobe\Color
?OfFolder.name returns:
Color
and the folder attribute is 16 which is "Directory"
=================================
Option Explicit
Dim FSO As FileSystemObject
Dim RE As RegExp
Sub GetTildeFiles()
Dim TopFolderName As String
Dim TopFolderObj As Folder
Dim DestinationRange As Range
TopFolderName = "c:\users\ron\documents"
Set DestinationRange = Worksheets(1).Range("A1")
Cells.Clear
If FSO Is Nothing Then
Set FSO = New FileSystemObject
End If
If RE Is Nothing Then
Set RE = New RegExp
End If
RE.Pattern = "^[^~]+\.[^~]{3}~.+"
Set TopFolderObj = FSO.GetFolder(TopFolderName)
ListSubFolders OfFolder:=TopFolderObj, _
DestinationRange:=DestinationRange
Set RE = Nothing
Set FSO = Nothing
End Sub
'------------------------------------------
Sub ListSubFolders(OfFolder As Folder, _
DestinationRange As Range)
Dim SubFolder As Folder, f As File
For Each f In OfFolder.Files
If RE.Test(f.Name) = True Then
Set DestinationRange = DestinationRange.Offset(1, 0)
DestinationRange.Value = f.path
End If
Next f
For Each SubFolder In OfFolder.SubFolders
ListSubFolders OfFolder:=SubFolder, _
DestinationRange:=DestinationRange
Next SubFolder
End Sub
===============================
--ron