Type of the File
File Information
Copy a file
Move a file
Delete a file
Create a Text File
Appending Text to File
Reads Specified Number of characters from a Text File
Reads all the characters from a Text File
Reads the Text File Line by Line
Writes Text to a File
Writes Blank Line to a File
Column Number of current character position
Line Number of current position
'Syntax:
'OpenTextFile
'OpenTextFile(filename[, iomode[, create[, format]]])
'iomode:
'ForReading - 1 - Opens a file for Read Only
'ForWriting - 2 - Opens a file for Reading and Writing
'ForAppending - 8 - Opens a file for Appending
Delete
FileObject.Delete(force)
'force is boolean value
True - Files with Read-Only attribute is deleted
Move
FileObject.Move(Destination)
File Type
'0 - Normal File
'1 - Read Only File
'2 - Hidden File
'4 - System File
'8 - Volume File - Disk Drive Volume Label
'16 - Folder or Directory
'32 - Archieve - File has changed
'1024 - Alias
'2048 - Compressed File
Dim fso, f, s
Dim PathOfTheFile
PathOfTheFile = "C:\NewDebug.xml"
Set fso = CreateObject("Scripting.FileSystemObject")
Set FileInfo = fso.GetFile(PathOfTheFile)
AttrVal = FileInfo.Attributes
If AttrVal = 1 then
Msgbox "Normal File"
Else
Msgbox "Not a Normal File"
End If
File Information
Dim fso, f, s
Dim PathOfTheFile
PathOfTheFile = "C:\Documents and Settings\...\Desktop\NewDebug.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set FileInfo = fso.GetFile(PathOfTheFile)
s = "Date on which the file is Created: "&FileInfo.DateCreated &vbcrlf
s = s & "Date on which the file is Last Accessed: "&FileInfo.DateLastAccessed&vbcrlf
s = s & "Date on which the file is Last Modified: "&FileInfo.DateLastModified & vbcrlf
s = s & "Drive on which the file is Saved: "&FileInfo.Drive & vbcrlf
s = s & "Name of the file: "&FileInfo.Name & vbcrlf
s = s & "Name of the folder on which the file is saved: "&FileInfo.ParentFolder&vbcrlf
s = s & "Path of the file: "&FileInfo.Path&vbCrlf
s = s & "Short Name of the file: "&FileInfo.ShortName&vbcrlf
s = s & "Short Path of the file: "&FileInfo.ShortPath & vbcrlf
s = s & "Total Size of the file: "&FileInfo.Size & vbcrlf
s = s & "Type of the file: "&FileInfo.Type
msgbox s
'Copy a file from One location to Another location
Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceFilePath = "D:\debugnew.txt"
DestinationFilePath = "D:\debugnew1.txt"
Set objFileCopy = objFSO.GetFile(SourceFilePath)
If objFSO.FileExists(DestinationFilePath)Then
objFileCopy.Copy(DestinationFilePath)
End If
'Move a file from One location to Another location
Set objFSO = CreateObject("Scripting.FileSystemObject")
SourceFilePath = "d:\debugnew.txt"
DestinationFolder = "C:\"
Set SourceFile = objFSO.GetFile(SourceFilePath)
SourceFile.Move DestinationFolder
'Delete a file
Set objFSO = CreateObject("Scripting.FileSystemObject")
FilePath = "D:\debugnew.txt"
Set objFile = objFSO.GetFile(FilePath)
If objFSO.FileExists(FilePath)Then
objFile.Delete
End If
'Create a text file
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set Textfile = fso.CreateTextFile("C:\FileName.txt",True)
Textfile.WriteLine("First Sentence")
Textfile.Close
Set Textfile = Nothing
Set fso = Nothing
'Appending Text to File
Dim ForAppending, fso, TextFile
ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForAppending)
TextFile.WriteLine("Fail")
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Reading Text from File
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForReading)
Text=TextFile.Read(10) 'Reads first 10 characters
Msgbox "First 10 characters of file is "&Text
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Reading All the Text from File
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForReading)
Text=TextFile.ReadAll 'Reads entire file
Msgbox "File Contents "&Text
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Reading Text File Line by Line
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForReading)
linecount = 0
Text = ""
Do While TextFile.AtEndOfStream <> True
linecount = linecount + 1
Text=Text&"line "&linecount&" "&TextFile.ReadLine&vbCrLf
Loop
Msgbox "File Contents "&Text
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Writes Text to a File
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForWriting, True)
TextFile.Write "This is a new String of Data"
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Writes Blank Line to a File
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForWriting, True)
TextFile.WriteBlankLines(3) 'Writes 3 Blank Lines to the Text File
TextFile.Write "This is a new String of Data"
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
Column Number of current character position
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForWriting, True)
Text = TextFile.ReadLine
ColNum = Text.Column
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Line Number of current position
Const ForReading =1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set TextFile = fso.OpenTextFile("C:\FileName.txt",ForReading, True)
Text = TextFile.ReadAll
LineNum = Text.Line
'Gets the Last Line Number
msgbox "Number of Lines in the Text File "&LineNum
TextFile.Close
Set TextFile = Nothing
Set fso = Nothing
'Gets the current folder path
Set fso = CreateObject("Scripting.FileSystemObject")
GetCurrentFolder= fso.GetAbsolutePathName(".")
Table of Contents
- Scripting Techniques
- Data Types
- Variables
- Constants
- Conditional Statements
- Build-In-Functions
- Loop Statements
- Sample Scripts
- Comments
- Input Box
- VBScript-Excel Object
- VBScript - MS Word Object
- VBScript Dictionary Object
- VBScript - FSO - Drives
- VBScript - XML Object
- VBScript - FSO - Folders
- VBScript - FSO - Files
- VBScript - Coding Standards
Sunday, September 5, 2010
File System Object Vs Files
Subscribe to:
Post Comments (Atom)
thanks for sharing this, it helps me increase my knowledge in programming.
ReplyDeletewww.triciajoy.com
This is good shit.. Saved for later reference.
ReplyDeleteIt 's an amazing and Very Nice,Thanks for sharing
ReplyDeleteDot Net Online Training Hyderabad