170 likes | 315 Vues
Programming using File System Object. Introduction to File System Object. F I L E S Y S T E M O B J E C T S. File System Object allows us to:. Create files and folders. Determine the existence of file, folder, or drive. Open a text file.
E N D
Introduction to File System Object F I L E S Y S T E M O B J E C T S File System Object allows us to: Create files and folders Determine the existence of file, folder, or drive Open a text file Perform a variety of other tasks related to the file system
Working with FileSystemObject General Process of Working with File System Objects Create an instance of the File System Object by using Create Object method 1 2 3 Use appropriate method on the newly created object, as required If method returns an object, then access the properties of that object to manipulate it.
Creating a File System Object CreateObject() Method CreateObject("Scripting.FileSystemObject") SetobjFSO = CreateObject("Scripting.FileSystemObject")
File System Object – FileExists Method SetobjFSO = CreateObject("Scripting.FileSystemObject") bExists = objFSO.FileExists("D:\FileMgmt\Sample.doc") If bExiststhen msgbox"File exists in the given path" Else msgbox"File doesn’t exists in the given path“ End IF FileExist() Method: object.FileExists(filepath) Note: The path should also contain the name of the file, including extension. ‘
File System Object – GetFile Method SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.GetFile("D:\FileMgmt\Sample.doc") GetFile() Method object.GetFile(filepath) Returns – Object of File Type
File System Object – GetFile Method - Accessing File Properties SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.GetFile("D:\WorkingFolder\Sample.doc") msgboxobjFile.Name msgboxobjFile.ParentFolder msgboxobjFile.Drive Every file has some properties, such as Name, Drive, Parent Folder, etc, and also has some methods, such as: Copy, delete, paste, etc.
File System Object – GetSpecialFolder Method SetobjFSO = CreateObject("Scripting.FileSystemObject") msgboxobjFSO.GetSpecialFolder(1) msgboxobjFSO.GetSpecialFolder(2) GetSpecialFolder() Method object.GetSpecialFolder(specialfoldername) Acceptable Values for Special Folder Name 0 - for Windows Folder 1 – for System folder 2 – for Temp folder The Get Special Folder method returns the path of the specified Windows special folder.
File System Object – CreateTextFile Method CreateTextFile() Method object.CreateTextFile(filename,[overwrite],[unicode]) Filename – Is a string that specifies the file name. Overwrite – It is a Boolean value, and it is an optional argument. Unicode – It is a Boolean value that specifies the format of the file to be created. This method returns object of a file type.
File System Object – CreateTextFile Method – Example ‘Example 1: 'Create FSO object SetobjFSO = CreateObject("Scripting.FileSystemObject") 'Create a new text file for the given path Set objNewFile = objFSO.CreateTextFile("D:\SampleScripts\samplefile1.txt") ‘Example 2: 'Create FSO object SetobjFSO = CreateObject("Scripting.FileSystemObject") 'Create a new text file for the given path SetobjNewFile = objFSO.CreateTextFile ‘Example 3: 'Create FSO object SetobjFSO = CreateObject("Scripting.FileSystemObject") 'Create a new text file for the given path SetobjNewFile = objFSO.CreateTextFile("D:\SampleScripts\samplefile3.txt",true,true) ("D:\SampleScripts\samplefile1.txt",false) ("D:\SampleScripts\samplefile2.txt”)
Writing to Text File Methods For writing to a text file, we can employ any of these methods: Write Method: It writes the data to a text file. object.Write text • WriteLine method: It writes the data to a text file and then appends a newline character at the point, where writing is complete. • object.WriteLine text Write Blank Lines method: Enters the specified number of blank lines object.writeblanklines(number)
Writing to Text File Methods – Write Method Set objFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = bjFSO.CreateTextFile("D:\SampleScripts\WriteFile.txt") objFile.Write"This is line 1.“ objFile.Write"This is line 2.“ objFile.Close
OpenTextFile Method OpenTextFile() Method Object.OpenTextFile (filename , [iomode], [create], [format]) Filename – Is the name of the file Note: The iomode is the mode in which you want to open the file Create argument is to be used only when the iomode argument is specified as “For Writing” or “For Appending”. The Create argument is a Boolean value. Format can be Unicode, ASCII, or system default This method returns object of file type
Read from Text File For reading from a text file, we can employ any of these methods: Read Method: Reads a specified number of characters from a file. Note: Once you use the Read All method, then do not use any other Read methods, including the Read All method, as the Read All method moves the cursor to the End of document or page. ReadLine Method: Reads entire line ReadAll Method: Reads the entire content of a text file
Read text from File SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.Read(20) MsgboxobjFile.Read(18) objFile.Close
Read method SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.ReadLine MsgboxobjFile.ReadLine objFile.Close
ReadLine method SetobjFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.ReadAll objFile.Close