Dealing with windows 7 folder structure

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
 
R

Ron Rosenfeld

I don't have windows 7 but understand your proplem. I have two
suggestions. the folder you are looking at are links and not folder. I
assume you already know that. But sometimes with links, one method wil
recognize the links and other won't

1) Try "If folderExists

Set fs = CreateObject("Scripting.FileSystemObject")
fs.FolderExists(folderspec)


2) Try

FolderName = Dir[(pathname[, attributes])]

the attributes can be as follows

vbNormal 0 (Default) Specifies files with no attributes.
vbReadOnly 1 Specifies read-only files in addition to files with no
attributes.

vbHidden 2 Specifies hidden files in addition to files with no
attributes.

VbSystem 4 Specifies system files in addition to files with no
attributes. Not available on the Macintosh.

vbVolume 8 Specifies volume label; if any other attributed is
specified, vbVolume is ignored. Not available on the Macintosh.

vbDirectory 16 Specifies directories or folders in addition to files
with no attributes.

vbAlias 64 Specified file name is an alias. Available only on the
Macintosh.

Thanks for the suggestions, Joel.

I wound up doing something similar to what you recommended.

The folderexists method failed because the "shortcut" folder returned TRUE

The Dir idea almost worked. But there were some hidden files that needed to be
searched also. Instead of expanding the Dir idea, I decided to try checking
the file attribute properties with:

If Not (SubFolder.Attributes And Alias) = Alias And _
Not (SubFolder.Attributes And System) = System Then
ListSubFolders OfFolder:=SubFolder, _
DestinationRange:=DestinationRange
End If

And that almost worked except for a few folders for which I received a
"permission denied" error, even when running as Administrator.

I gave up and decided to handle those files with an "on error resume next".

For a home user, the W7 securities scheme is really a PITA.

Thanks for your help, though.
--ron
 
J

Jim Cone

Ron,

RE: "And that almost worked except for a few folders for which I received a
"permission denied" error, even when running as Administrator."

My experience has been that the "System Volume" Folder(s) in Windows XP are entirely and completely
off limits.
Probably the same in Windows 7.
 
R

Ron Rosenfeld

Ron,

RE: "And that almost worked except for a few folders for which I received a
"permission denied" error, even when running as Administrator."

My experience has been that the "System Volume" Folder(s) in Windows XP are entirely and completely
off limits.
Probably the same in Windows 7.

That was one of them. I forget the other one's, but there weren't many.
--ron
 
R

Ron Rosenfeld

I had similar problems with the Scripting Object and had to resort to
using On Error to get past the errors.

Thank you for letting me know "I am not alone!"

(As well as for pointing me in a good direction)
--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top