Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


March 20, 2000

Understanding VBScript: Working with the File Object


RSS
View this exclusive article with VIP access -- click here to join |
See More VBScript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Files are the building blocks of any application running under any OS. Files are also the software elements that administrators and users manipulate most of their workday. However, none of the Windows file systems (e.g., FAT, NTFS) follow an object-oriented schema. In other words, Windows file systems don't consider a file (or a folder) as an object with a fixed programming interface. To make Windows file systems recognize a file as an object, you need to use a tool such as the File System Object (FSO) model. With the FSO model's File object, you can manipulate files in VBScript applications.

The File object represents a file in a Windows file system. After you access the File object, you can use the object's methods and properties to work with it. The File object has 12 properties that provide information about files and four methods that manipulate files.

Accessing the File Object
To work with a File object, you need a reference to it. You can obtain this reference in two ways, both of which rely on the FileSystemObject object. First, you can call the FileSystemObject object's GetFile method and specify the fully qualified filename (i.e., the absolute or relative path) of the file you want to access. Second, you can enumerate the items in the FileSystemObject object's Files collection and stop at the element whose properties match your criteria. Let's examine the pros and cons of both techniques. . . .


Already a VIP member?
Please log on to view the full article

Why become a VIP member?

VIP-only online access
VIP CD delivered twice a year: offline access to the entire Windows IT Pro article library
Monthly issue of your choice of Windows IT Pro or SQL Server Magazine

Subscribe Now
Reader Comments
Good article for understanding FSO and Attributes. I'm getting an attribute of 38. Does anyone know what that one means?

gpalmer4177 July 18, 2004 (Article Rating: )


I am very new to the whole scripting thing... I am trying to simply run the delete example in the article; what do I do to run any of these besides saving them in a .vb file? Thank you very much!

Anonymous User November 03, 2004


Nice Stating Tutorial on FileSystemObject!

Anonymous User March 03, 2005 (Article Rating: )


Set fso = CreateObject _
("Scripting.FileSystemObject")
Set f = _ fso.GetFolder("C:\mydir")
For Each file In f.Files
MsgBox f.Name
Next

Should be:

Set fso = CreateObject _
("Scripting.FileSystemObject")
Set f = _ fso.GetFolder("C:\mydir")
For Each file In f.Files
MsgBox file.Name
Next

Anonymous User April 29, 2005 (Article Rating: )


In layman's terms for those of us that don't understand the jargon above, use the following code just pasted to the sub command you're trying to make work.

Set fso = CreateObject _
("Scripting.FileSystemObject")
Set MyFile = fso.GetFile("T:\rk_inugo.csv")
MyFile.Copy "C:\rk_inugoxx.csv", 0

NOTE:

T:\rk_inugo.csv is the file I'm copying.

C:\rk_inugoxx.csv is the file I'm copying to.

The zero (0) at the end of the file keeps you from overwriting the file if it exists. Change to a "1" if you want it to overwrite the file.

Anonymous User May 04, 2005 (Article Rating: )


How about looping throug subfolders and folders?

Anonymous User July 14, 2005 (Article Rating: )


In a DOS prompt, I need to locate the name of the folder from which my script is being run. Up to this point, I've simply copied a dummy text file from C:\ via the script, then obtained a handle on the copied file to get the absolutepathname of said file, after which I'll delete the dummy file.

Is there any easier, cleaner way of obtaining the path from which my script is being run in a DOS prompt??

Thanks

Anonymous User July 15, 2005 (Article Rating: )


Snipped from Eric Phelps GlobalSearchAndReplace.vbs on www.ericphelps.com

This function when called with "" returns the path you're running in


Function FileNameInThisDir(strFileName) 'As String
'Returns the complete path and file name to a file in
'the script directory. For example, "trans.log" might
'return "C:\Program Files\Scripts\Database\trans.log"
'if the script was in the "C:\Program Files\Scripts\Database"
'directory.
Dim fs 'As Scripting.FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName))
'''''Clean up
Set fs = Nothing
End Function


Anonymous User August 29, 2005 (Article Rating: )


'Recursive directory file list
'July 2007 - Robert Heritage - mrheritage@gmail.com
'Please mention me in your script if you use this code :)
' to pipe the output into the file use: cscript thisfile.vbs > outfile.csv


Dlm = ","' Variable Delimiter for output
redim FolderList(0)'A flat list of the directory subtree

FolderList(0) = "c:\"'can also use \\servername\c$ etc but it is sloooow


'This part of the script walks the directory structure and stores all of he folder paths in the FolderList Array.

FolderIndex = 0
Do Until FolderIndex > Ubound(FolderList)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderList(folderindex))

If f.subfolders.count > 0 Then

For Each folder In f.subFolders

if folder.attributes <> 22 Then 'Exclude hidden system folders
redim preserve FolderList(Ubound(FolderList)+1)
FolderList(ubound(FolderList)) = folder.path
End If
Next
End If
FolderIndex = FolderIndex + 1
Loop


'Set up a header for the output

wscript.echo ubound(FolderList) & " Folders. Root: " &Folderlist(0)
wscript.echo "FileName" & dlm & "Folder" & dlm & "Size" & dlm & "Date Created" & dlm & "Age" & dlm & "Date Modified" & dlm & "UnModified Age"

& dlm & "Last Accessed" & dlm & "Unread Age" & dlm & "peers"


'This part of the script Works through the list of folders and outputs the data for each file

for n = 0 to ubound(FolderList)

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(FolderList(n))
For Each file In f.Files
dslc = DateDiff("d",Now,file.datecreated) * -1
dslm = DateDiff("d",Now,file.dateLastModified) * -1
dsla = DateDiff("d",Now,file.dateLastAccessed) * -1

wscript.echo file.Name & dlm & file.parentfolder & dlm & file.size & dlm & file.DateCreated & dlm & dslc & dlm & file.DateLastModified &

dlm & dslm & dlm & file.DateLAstAccessed & dlm & dsla & dlm & f.Files.count
Next

next

MrHeritage July 26, 2007 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement