File Handling Using VBScript

1.    Create text file Using VBScript
strFilePath = "D:\ techvbscript\Test.txt"            'set the path of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")    'create file system object
Set ObjFile = ObjFSO.CreateTextFile(strFilePath, True, False)  ' True – To Overwrite , False – Creates ASCII File
ObjFile.Write "This is a Text"       ' write text in Test file      
ObjFile.close                 'close file
Set ObjFile = Nothing   ' release file object
Set ObjFSO = Nothing   ' release file system object

2.    Delete text file Using VBScript
strFilePath = "D:\ techvbscript\Test.txt"            'set the path of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")    'create file system object
If ObjFSO.FileExists(strFilePath) Then         'check if file exists
       ObjFSO.DeleteFile strFilePath, True     ' True of files with the read-only attribute set are to be deleted
Else   
       MsgBox   "File Not Exist"
End If
Set ObjFile = Nothing   ' release file object
Set ObjFSO = Nothing   ' release file system object

3.    Move a file Using VBScript
strFilePath = "D:\ techvbscript\Test.txt"            'set the path of text file
strDestinationPath = "D:\ temp\ "           'set the destination of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")    'create file system object
If ObjFSO.FileExists(strFilePath) Then         'check if file exists
       ObjFSO.MoveFile strFilePath, strDestinationPath    'destination folder must exists
Else   
       MsgBox   "File Not Exist"
End If
Set ObjFSO = Nothing   ' release file system object

4.    Copy a file Using VBScript
strFilePath = "D:\ techvbscript\Test.txt"            'set the path of text file
strDestinationPath = "D:\ temp\ "           'set the destination of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")    'create file system object
If ObjFSO.FileExists(strFilePath) Then         'check if file exists
       ObjFSO.CopyFile strFilePath, strDestinationPath    'destination folder must exists
Else   
       MsgBox   "File Not Exist"
End If
Set ObjFSO = Nothing   ' release file system object

5.     Read a file Using VBScript
ForReading = 1    'parameter for open a file in read mode
strFilePath = "D:\ techvbscript\Test.txt"         'set the path of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")     'create file system object
If ObjFSO.FileExists(strFilePath) Then    'check if file exists
    set ObjFile = ObjFSO.OpenTextFile(strFilePath,ForReading)   'open file in read mode

    'different approaches to read a file. Try anyone of them based on your requirement
   'first
    ObjFile.skip(2)     'skip 2 characters
    MsgBox   ObjFile.Read(3)    ' Read First 3 Character and set pointer to 6th character
    MsgBox   ObjFile.ReadLine   ' Read a line from 6th character and set pointer to next line
    ObjFile.skipline  ‘Skip the line and set pointer to 3rd Line   
    MsgBox   ObjFile.ReadAll    ' Read All the data from 4rd Line

      'second
    While ObjFile.AtEndOfLine<> True
           MsgBox   ObjFile.Read(1)     ' Read a character till end of file
    Wend

       'third
    While ObjFile.AtEndOfStream<> true
            MsgBox   ObjFile.ReadLine   ' Read a line till end of file
    Wend
   set ObjFile = Nothing     ' release file object

Else
  MsgBox   "File Not Exist"
End If
Set ObjFSO = Nothing    ' release file system object

6.    Write a File using VBScript
ForWriting=2      'parameter for open a file in write mode
ForAppending=8    'parameter for open a file in append mode
strFilePath = "D:\ techvbscript\Test.txt"         'set the path of text file
Set ObjFSO = CreateObject("Scripting.FileSystemObject")    'create file system object
set ObjFile = ObjFSO.OpenTextFile(strFilePath,ForWriting)    'open file in write mode
ObjFile.writeBlankLines(3)     'write 3 blank lines
ObjFile.write "This is new string"      'write mentioned string
ObjFile.WriteLinevbnewline      'write a new line
ObjFile.WriteLine "This is a line of data"    'write mentioned string
ObjFile.WriteLine    ' write a blank line
ObjFile.close          'close file
Set ObjFile = Nothing   ' release file object
Set ObjFSO = Nothing     ' release file system object

No comments:

Post a Comment