Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
iam trying to list the files and folders in the c drive. the problem is i dont want to list the "hidden files " . i searched in the internet and found some code that finds the file attributes.
Function ToggleArchiveBit(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
If f.attributes and 32 Then
f.attributes = f.attributes - 32
ToggleArchiveBit = "Archive bit is cleared."
Else
f.attributes = f.attributes + 32
ToggleArchiveBit = "Archive bit is set."
End If
End FunctionThe problem is i dont know wht is filespec variable in Function ToggleArchiveBit(filespec)
so i didnt know what to pass to this functionmy main prog looks like this..
Dim fso, fls, fc, fl, f
Dim i As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Set fls = fso.GetFolder("C:\")
Set fc = fls.subfolders
If fso.DriveExists("C") Then
MsgBox "Drive Exists"
...........
ToggleArchiveBit (..)
.........pls some one tell me what to pass to this function..

Argh!. Get rid of the File System Object. It is slow, cumbersome and uses loads of resources. It is designed for use with VB Script although it will work in VB. VB has functions to do everything that the FSO will do and a lot simpler.
Use the File List Box and set the properties appropriately. Takes 30 seconds to set up.
If you really want to be clever you can use an API call but that gets a bit complicated
but is very fast and efficient.
Stuart

Here is an example:
' Objects
'
' Type: DriveListBox, Name: DriveList
' Type: DirListBox, Name: DirList
' Type: FileListBox, Name: FileList
' Type: CheckBox, Name: ArchiveCheck
' Type: CheckBox, Name: HiddenCheck
' Type: CheckBox, Name: NormalCheck
' Type: CheckBox, Name: SystemCheck
'
Dim LastDrive As StringPrivate Sub DriveList_Change()
Call UpdateDrive
End SubPrivate Sub DirList_Change()
Call UpdateDir
End SubPrivate Sub Form_Load()
Call UpdateDrive
Call InitChecked
End SubPrivate Sub InitChecked()
If FileList.Archive = True Then
ArchiveCheck.Value = vbChecked
Else
ArchiveCheck.Value = vbUnchecked
End If
If FileList.Hidden = True Then
HiddenCheck.Value = vbChecked
Else
HiddenCheck.Value = vbUnchecked
End If
If FileList.Normal = True Then
NormalCheck.Value = vbChecked
Else
NormalCheck.Value = vbUnchecked
End If
If FileList.System = True Then
SystemCheck.Value = vbChecked
Else
SystemCheck.Value = vbUnchecked
End If
End SubPrivate Sub ArchiveCheck_Click()
Call UpdateArchive
End SubPrivate Sub HiddenCheck_Click()
Call UpdateHidden
End SubPrivate Sub NormalCheck_Click()
Call UpdateNormal
End SubPrivate Sub SystemCheck_Click()
Call UpdateSystem
End SubPrivate Sub UpdateDrive()
On Error GoTo Drive_error
LastDrive = DriveList.Drive
Call UpdateDir
Return
Drive_error:
DriveList.Drive = LastDrive
End SubPrivate Sub UpdateDir()
DirList.Path = DriveList.Drive
Call UpdateFile
End SubPrivate Sub UpdateFile()
FileList.Path = DirList.Path
End SubPrivate Sub UpdateArchive()
If ArchiveCheck.Value = vbChecked Then
FileList.Archive = True
Else
FileList.Archive = False
End If
End SubPrivate Sub UpdateHidden()
If HiddenCheck.Value = vbChecked Then
FileList.Hidden = True
Else
FileList.Hidden = False
End If
End SubPrivate Sub UpdateNormal()
If NormalCheck.Value = vbChecked Then
FileList.Normal = True
Else
FileList.Normal = False
End If
End SubPrivate Sub UpdateSystem()
If SystemCheck.Value = vbChecked Then
FileList.System = True
Else
FileList.System = False
End If
End Sub

If you add the 'Microsoft Scripting Runtime' reference to your project, you'll be able to intantiate your FSO object without using the CreateObject function. It'll also mean you'll be able to see the objects methods and properties by typing "." after your object variable.
eg:Dim objFSO As Scripting.FileSystemObject
Dim objFile As Scripting.File
Dim objAttr As Scripting.FileAttribute
Set objFSO = New Scripting.FileSystemObject
Set objFile = objFSO.GetFile("c:\test.xml")
MsgBox objFile.AttributesIn my example, the message box will return '33', this is because it is read-only and archive - the read only attribute evaluates to 1 and the archive attribute is worth 32, if you refer the link below you will see that every attribute has it's own number and that each combination will give you a unique result.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/jsproattributes.asp
You will also notice that I explictly declared the object types when I dim'ed them. This is good, try and remember to do this.

Forget about the FSO. Its an abomination and has no place in a VB programme.
egkenny's example may look a bit complicated but it has everything you need to navigate drives, folders and files using the facilities built into VB therefore making it far more efficient.
Stuart

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |