Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Friday, May 15, 2009

File System Object

'********************************************************************************
'******************************* Working with drives ****************************
'********************************************************************************
Dim fso, d
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
'Volume Name
msgbox d.VolumeName
'The total size of the drive in bytes (TotalSize property) 
msgbox "Total Space: " & FormatNumber(drv.TotalSize / 1024, 0)
'How much space is available on the drive in bytes (AvailableSpace or FreeSpace properties) 
msgbox "Available Space: " & FormatNumber(d.AvailableSpace/1024, 0)
'What letter is assigned to the drive (DriveLetter property) 
msgbox "Drive " & d.DriveLetter
'What type of drive it is, such as removable, fixed, network, CD-ROM, or RAM disk (DriveType property) 
'0:"Unknown"
'1:"Removable"
'2:"Fixed"
'3:"Network"
'4:"CD-ROM"
'5:"RAM Disk"
msgbox d.DriveType
'The drive's serial number (SerialNumber property) 
msgbox d.SerialNumber
'********************************************************************************
'******************************Working with Folders*********************************
'********************************************************************************
'Create Folder
CreateObject("scripting.filesystemobject").CreateFolder(FolderPath)
'Delete Folder
CreateObject("scripting.filesystemobject").DeleteFolder(FolderPath)
'Move Folder
CreateObject("scripting.filesystemobject").MoveFolder SourceFolderPath,DestinationFolderPath
'Copy Folder
CreateObject("scripting.filesystemobject").CopyFolder SourceFolderPath,DestinationFolderPath
'Get Name of a Folder
msgbox CreateObject("scripting.filesystemobject").GetFolder(FolderPath).Name
'Check for Folder Existance
msgbox CreateObject("scripting.filesystemobject").FolderExists(FolderPath)
'Get Parent Folder Name
msgbox CreateObject("scripting.filesystemobject").GetFolder(FolderPath).ParentFolder.Name
'Get Sub Folders from a Folder
Dim fso,fld,sfolders,sFld
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sfolders = fld.SubFolders
For Each sFld in sfolders
msgbox sFld.name 
Next
'Get Files From a Folder
Dim fso,fld,sFiles,sFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(FolderPath)
Set sFiles = fld.Files
For Each sFile in sFiles
msgbox sFile.name 
Next
'********************************************************************************
'******************************Working with Files***********************************
'********************************************************************************
'Creating Text Files
CreateObject("Scripting.FileSystemObject").CreateTextFile(FileName)
'Read Text File 
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,1)
'Reading Specified number of Characters
MsgBox fl.Read(5)
'Reading Complete Data from File
MsgBox fl.ReadAll
'Read Data Line by Line
While Not fl.AtEndOfStream
MsgBox fl.ReadLine
Wend
'Write Data to a text file
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,2)
'Write characters
fl.Write("hello")
'Write blank lines
fl.WriteBlankLines(2)
'Write data line by line 
fl.WriteLine("A New Line")
 
'Update/Append Data to a Text File
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,8)
'Write characters
fl.Write("hello")
'Write blank lines
fl.WriteBlankLines(2)
'Write data line by line 
fl.WriteLine("A New Line")
 
'Remove Data From a text File
Set fso=CreateObject("scripting.filesystemobject")
Set fl=fso.OpenTextFile(FilePath,2)
'Write nothig
fl.Write("")
'Moving a File
CreateObject("scripting.filesystemobject").MoveFile SourceFilePath,DestinationFilePath
'Copying a File
CreateObject("scripting.filesystemobject").CopyFile SourceFilePath,DestinationFilePath
'Deleting files
CreateObject("scripting.filesystemobject").DeleteFile(FilePath)
 
'********************************************************************************
'Modify a Specific Line in a TextFile
'********************************************************************************
Dim txtFilePath,txtRowNumber,TextToModify
Dim fso,f,txtData,strTxtData,cnt
txtFilePath= "C:\abc.txt"
txtRowNumber=3
TextToModify="three"
Set fso= CreateObject( "scripting. FilesystemObject ")
Set f=fso.OpenTextFile( txtFilePath, 1)
txtData=f.readall
f.close
Set f=fso.OpenTextFile( txtFilePath, 2)
strTxtData=split( txtData,vbnewline)
For cnt=0 to ubound(strTxtData)
If cnt<>txtRowNumber- 1 Then
f.writeline( strTxtData( cnt))
else
f.writeline( TextToModify )
End If
Next
f.close
'********************************************************************************
'********************************************************************************

No comments :

Post a Comment