Computing.Net > Forums > Programming > file attributes in VB

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

file attributes in VB

Reply to Message Icon

Name: sunny
Date: February 6, 2005 at 11:15:47 Pacific
OS: xp
CPU/Ram: 256
Comment:

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 Function

The problem is i dont know wht is filespec variable in Function ToggleArchiveBit(filespec)
so i didnt know what to pass to this function

my 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..




Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: February 6, 2005 at 12:06:08 Pacific
Reply:

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


0

Response Number 2
Name: egkenny
Date: February 6, 2005 at 21:36:53 Pacific
Reply:

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 String

Private Sub DriveList_Change()
Call UpdateDrive
End Sub

Private Sub DirList_Change()
Call UpdateDir
End Sub

Private Sub Form_Load()
Call UpdateDrive
Call InitChecked
End Sub

Private 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 Sub

Private Sub ArchiveCheck_Click()
Call UpdateArchive
End Sub

Private Sub HiddenCheck_Click()
Call UpdateHidden
End Sub

Private Sub NormalCheck_Click()
Call UpdateNormal
End Sub

Private Sub SystemCheck_Click()
Call UpdateSystem
End Sub

Private Sub UpdateDrive()
On Error GoTo Drive_error
LastDrive = DriveList.Drive
Call UpdateDir
Return
Drive_error:
DriveList.Drive = LastDrive
End Sub

Private Sub UpdateDir()
DirList.Path = DriveList.Drive
Call UpdateFile
End Sub

Private Sub UpdateFile()
FileList.Path = DirList.Path
End Sub

Private Sub UpdateArchive()
If ArchiveCheck.Value = vbChecked Then
FileList.Archive = True
Else
FileList.Archive = False
End If
End Sub

Private Sub UpdateHidden()
If HiddenCheck.Value = vbChecked Then
FileList.Hidden = True
Else
FileList.Hidden = False
End If
End Sub

Private Sub UpdateNormal()
If NormalCheck.Value = vbChecked Then
FileList.Normal = True
Else
FileList.Normal = False
End If
End Sub

Private Sub UpdateSystem()
If SystemCheck.Value = vbChecked Then
FileList.System = True
Else
FileList.System = False
End If
End Sub


0

Response Number 3
Name: MarkM
Date: February 16, 2005 at 12:17:03 Pacific
Reply:

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.Attributes

In 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.


0

Response Number 4
Name: StuartS
Date: February 16, 2005 at 20:09:12 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: file attributes in VB

File attributes in C www.computing.net/answers/programming/file-attributes-in-c/7025.html

Web File Download in VB.NET www.computing.net/answers/programming/web-file-download-in-vbnet/10015.html

How to OPen a File in VB.NET www.computing.net/answers/programming/how-to-open-a-file-in-vbnet/13055.html