Displaying File Information using VBS and the FileSystemObject

12/15/07

Permalink 03:24:23 pm, by dave Email , 457 words, 3462 views   English (US)
Categories: Windows Scripting, Wsh Scripts

Displaying File Information using VBS and the FileSystemObject

While working on a script that enumerates the files in the startup folder, I wanted to list certain information about a file, create time, last access time, last modified time, and the file's attributes. This can be done fairly easily using the Scripting.FileSystemObject. Here is a function that will list the basic information about a file.

[More:]

function getFileInfo(filePath)

dim fso, fileObj, outMsg

set fso = createobject("Scripting.FileSystemObject")

set fileObj = fso.getfile(filePath)

outMsg = ""

outMsg = outMsg & " Created: " & fileObj.DateCreated & vbcrlf

outMsg = outMsg & " Last Accessed: " & fileObj.DateLastAccessed & vbcrlf

outMsg = outMsg & " Last Modified: " & fileObj.DateLastModified & vbcrlf

outMsg = outMsg & " File Type: " & fileObj.Type & vbcrlf

if fileObj.attributes and 0 then

outMsg = outMsg & " File Attributes: Normal File"

else

outMsg = outMsg & " File Attributes: "

if fileObj.attributes and 1 then
outMsg = outMsg & "Read Only "
end if

if fileObj.attributes and 2 then
outMsg = outMsg & "Hidden "
end if

if fileObj.attributes and 4 then
outMsg = outMsg & "System "
end if

if fileObj.attributes and 8 then
outMsg = outMsg & "Volume "
end if

if fileObj.attributes and 16 then
outMsg = outMsg & "Directory "
end if

if fileObj.attributes and 32 then
outMsg = outMsg & "Archive "
end if

if fileObj.attributes and 1024 then
outMsg = outMsg & "Link "
end if

if fileObj.attributes and 2048 then
outMsg = outMsg & "Compressed "
end if

end if

set fileObj = nothing
set fso = nothing

getFileInfo = outMsg

end function

The most important thing in this function is being able to translate the attributes values into something we can understand.

Normal  0  Normal file. No attributes are set.
ReadOnly  1  Read-only file. Attribute is read/write.
Hidden  2  Hidden file. Attribute is read/write.
System  4  System file. Attribute is read/write.
Volume  8  Disk drive volume label. Attribute is read-only.
Directory  16  Folder or directory. Attribute is read-only.
Archive  32  File has changed since last backup. Attribute is read/write.
Alias  1024  Link or shortcut. Attribute is read-only.
Compressed  2048  Compressed file. Attribute is read-only.

To call the function, you just have to pass in the path to the file to check. Something like.....

wscript.echo getFileInfo("C:\funcTest.vbs")

The results will look something like........

C:\>cscript funcTest.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Created: 12/14/2007 10:20:46 AM
Last Accessed: 12/15/2007 2:57:32 PM
Last Modified: 12/15/2007 2:57:32 PM
File Type: VBScript Script File
File Attributes: Archive

I hope this post helped you out. If it didn't, I am always looking for new scripts to add so submit a request for your question or need and I will see if I can answer it.



Did you like this post? If so, Share it!  del.icio.us digg reddit slashdot this article Facebook Twitter MySpace Email



Pingbacks:

No Pingbacks for this post yet...

This post has 18 feedbacks awaiting moderation...

Scripts

This is somewhere I can post interesting snippets as I come across them. Hopefully some folks out there will find this helpful.

Search

Follow Me:

Misc

Who's Online?

  • Guest Users: 1

powered by b2evolution free blog software