File System Object Property

G

Guest

I have the following code that creates a folder. After the folder is created,
I want to set the attributes to 'hidden'. I have tried fs.attrib = hidden and
fs.attribute= hidden but get syntax errors. What do I need to do? Thank you.

Dim fs As FileSystemObject 'declare file system object
Set fs = New FileSystemObject 'instantiate object
If Not fs.FolderExists("C:\Program Files\My Bike Log\BikeLogText") Then
fs.CreateFolder "C:\Program Files\My Bike Log\BikeLogText"
End If
 
D

Douglas J. Steele

It's not necessary to introduce the overhead of FSO to do what you're trying
to do.

The VBA SetAttr statement will do what you're trying to do:

SetAttr "C:\Program Files\My Bike Log\BikeLogText", vbHidden

VBA is also capable of determining whether or not a particular folder
exists, and to create it if it doesn't:

Dir("C:\Program Files\My Bike Log\BikeLogText\", vbDirectory)

will return an empty string if the directory doesn't exist, and

MkDir ""C:\Program Files\My Bike Log\BikeLogText\"

will create a directory.

The only caveat is that you cannot create C:\Program Files\My Bike
Log\BikeLogText if "C:\Program Files\My Bike Log doesn't already exist.
 
R

RoyVidar

Should FileSystemObject still be the preferred way, I think something
like
this might do:

Dim fs As Scripting.FileSystemObject 'declare file system
object
Dim fld as scripting.folder
Set fs = New scripting.FileSystemObject 'instantiate object
If Not fs.FolderExists("C:\Program Files\My Bike Log\BikeLogText") Then
set fld = fs.CreateFolder "C:\Program Files\My Bike
Log\BikeLogText"
fld.attributes = fld.attributes or hidden
End If


Douglas J. Steele wrote in message
 

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