Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File & Folder attributes (when name has a space) 2

Status
Not open for further replies.

cresbydotcom

Technical User
May 22, 2006
234
Hi

detecting folders &/or files with spaces in their name

I just found a problem reading the file attributes when the object is a folder. By detecting an error this code then reads folder attributes.
However if there is a space in the file name the error is not trapped - but in a folder it does not cause the same error

Excel 2002, XP SP2

any ideas how to get file attributes if the file name has a space?

Code:
On Error GoTo fol
If InStr(1, docs, " ") < 1 Then Set f = fs.GetFile(docs): Range("O" & o).Value = docs: o = o + 1: GoTo fol2
fol:    Set f = fs.GetFolder(docs): Range("N" & n).Value = docs: n = n + 1:
fol2:  On Error GoTo 0

after this the file/folder attributes can be read (eg folder = 16)
 
I'm not sure why you're having this problem. The following works fine for me in the Immediate Window:
Code:
Set fs = CreateObject("Scripting.FileSystemObject")
set f=fs.getfile("t:\syseng\aehf\AEHF Recurring Meetings.doc")
print f.name
AEHF Recurring Meetings.doc

_________________
Bob Rashkin
 
The problem is probably the way I look for "Folder"

The real poser which I haven't figured is: how to distinguish a folder. Inspecting attributes seemed easy but if you try to "getfolder" and it is not a folder then the error cannot be trapped it bombs-out of the interpreter rather than going to an error label. ditto "getfile". Any ideas?

"docs" in my example comes from a
Code:
docs = DIR("C:\blah_blah")
line in a loop - the end result is a list of files and a list of folders. Or would be.

So the problem is that I asked the wrong question. Give me a negative star.
 
I guess I don't know what you're trying to do. First of all, dir only returns a single filename (see the entry in VBA help). You can specify that dir return a folder name with the attributes field,
Dir[(pathname[, attributes])], where the attribute value 16 (vbDirectory) will return folders.

Maybe you could give a brief synopsis of what you're trying to accomplish.


_________________
Bob Rashkin
 
Yes, please clearly spell out what you are trying to do. I am unsure, as there seems to be use of both Dir, and FSO.

Also, it may (or may not) be helpful to post more expanded code.

BTW: while technically valid, using ":" like you do in your code makes it much harder for others to read. Not saying you should not use it for yourself, but it is not recommended when posting code for others to read.

faq219-2884

Gerry
My paintings and sculpture
 
Code:
Private Sub Print_Directory_Listing()
   Dim path_name As String, file_name As String

   path_name = "C:\Documents and Settings\WinblowsME\Desktop\"
   file_name = Dir(path_name, vbNormal + vbReadOnly + vbHidden + vbSystem + vbDirectory)
   
   Do While file_name <> ""
      Select Case GetAttr(path_name & file_name)
         Case vbDirectory
            Debug.Print "Directory:  " & file_name
         Case Else
            Debug.Print "File:  " & file_name
      End Select
      
      file_name = Dir
   Loop
End Sub
 
I will try the GetAttr - seems a more obvious choice (now!). Like all these problems - you have to know enough of the answer to ask the question properly.


OK - I want two lists of everything in a specific folder (pre-determined), in two Excel columns.

One for Folders only
one for files only. (thence to move according to name)
 
GetAttr does the job. Many thanx.

Here have a gold star - er - will a purple star do?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top