1 / 17

Programming using File System Object

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.

eddy
Download Presentation

Programming using File System Object

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming using File System Object

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

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

  4. Creating a File System Object CreateObject() Method CreateObject("Scripting.FileSystemObject") SetobjFSO = CreateObject("Scripting.FileSystemObject")

  5. 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. ‘

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

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

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

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

  10. 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”)

  11. 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)

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

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

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

  15. Read text from File SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.Read(20) MsgboxobjFile.Read(18) objFile.Close

  16. Read method SetobjFSO = CreateObject("Scripting.FileSystemObject") SetobjFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.ReadLine MsgboxobjFile.ReadLine objFile.Close

  17. ReadLine method SetobjFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("D:\SampleScripts\ReadFile.txt“,1) MsgboxobjFile.ReadAll objFile.Close

More Related