FileSystemObject
================
Accessing Existing Drives, Files, and Folders
---------------------------------------------
To gain access to an existing drive, file, or folder, use the appropriate "get" method of the FileSystemObject object:
GetDrive
GetFolder
GetFile
Creating a NewFolder
--------------------
Dim fso, f, fc, nf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(path)
Set fc = f.SubFolders
Set nf = fc.Add("Ravi Folder")
Dim fso, fldr
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.CreateFolder("C:\MyTest")
Accessing the Object's Properties
---------------------------------
Set fldr = fso.GetFolder("c:\")
Msgbox fldr.Name
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile("c:\detlog.txt")
MSGBOX f1.DateLastModified, DateLastAccessed, DateCreated,
f.Drive - Returns the drive letter of the drive
f.Type - Returns information about the type of a file or folder. For example, for files ending in .TXT, "Text Document" is returned.
f.size - returns the size in bytes
f.Path - Returns the path for a specified file, folder, or drive.
f.ParentFolder - Returns the folder object for the parent of the specified file or folder.
f.IsRootFolder - Returns True if the specified folder is the root folder; False if it is not.
Listing all the files
---------------------
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name
s = s & "
"
Next
Listing all sub folders
-----------------------
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.SubFolders
For Each f1 in fc
s = s & f1.name
s = s & "
"
Next
Display Level Depth
-------------------
Dim fso, f, n
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(pathspec)
If f.IsRootFolder Then
DisplayLevelDepth = "The specified folder is the root folder."
Else
Do Until f.IsRootFolder
Set f = f.ParentFolder
n = n + 1
Loop
MsgBox "The specified folder is nested " & n & " levels deep."
End If
Working with Files
------------------
Creating Files:-
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
OR
Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)
OR
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)
Adding Data to the File:-
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\testfile.txt", True)
tf.WriteLine("Testing 1, 2, 3.") 'Writes in New Line
tf.WriteBlankLines(3)
tf.Write ("This is a test.")
tf.Close
Reading Files:-
Const ForReading = 1
Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
s = ts.ReadLine ' Other Methods Read, ReadAll
MsgBox "File contents = '" & s & "'"
ts.Close
Moving, Copying, and Deleting Files:-
Dim fso, f1, f2, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
f1.Write ("This is a test.")
f1.Close
Set f2 = fso.GetFile("c:\testfile.txt")
Move the file to \tmp directory.
f2.Move ("c:\tmp\testfile.txt")
' Copy the file to \temp.
f2.Copy ("c:\temp\testfile.txt")
' Get handles to files' current location.
Set f2 = fso.GetFile("c:\tmp\testfile.txt")
Set f3 = fso.GetFile("c:\temp\testfile.txt")
' Delete the files.
f2.Delete
f3.Delete
Delete File / Folder
--------------------
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filespec)
fso.DeleteFolder(filespec)
fso.MoveFile Drivespec, "c:\windows\desktop\"
fso.MoveFolder Drivespec, "c:\windows\desktop\"
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"
FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"
File/Folder Exist
-----------------
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists(strFolderPath)
If objFso.FileExists(strFilePath)
Tuesday, October 12, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment