Sie sind auf Seite 1von 8

'********************************************************************************

'Write a program to read data from a text file

'********************************************************************************

'Read Text File

Set fso=CreateObject("scripting.filesystemobject")

Set fl=fso.OpenTextFile(FilePath,1)

'Reading Complete Data from File

MsgBox fl.ReadAll

'********************************************************************************

'Write a program to write data into 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")

'********************************************************************************

'Write a program to print all lines that contains a word either “testing” or “qtp”
'********************************************************************************

Set fso=CreateObject("scripting.filesystemobject")

Set fl=fso.OpenTextFile(FilePath,1)

'Read Data Line by Line

While Not fl.AtEndOfStream

If instr(1,fl.ReadLine,"testing")<>0 or instr(1,fl.ReadLine,"qtp")<>0 then

print fl.ReadLine

End if

Wend

'********************************************************************************

'Write a program to print the current foldername

'********************************************************************************

Set fso=CreateObject("scripting.filesystemobject")

msgbox fso.GetAbsolutePathName("")

'********************************************************************************

'Write a program to print files in a given 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

print sFile.name

Next

'********************************************************************************

'Write a program to print subfolders in a given 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

'********************************************************************************

'Write a program to print all drives in the file system

'********************************************************************************

Set fso = CreateObject("Scripting.FileSystemObject")

Set drvs = fso.Drives

For Each drv in drvs

print drv.DriveLetter
Next

'********************************************************************************

'Write a program to print current drive name

'********************************************************************************

Set fso = CreateObject("Scripting.FileSystemObject")

msgbox fso.GetDriveName(fso.GetAbsolutePathName(""))

'********************************************************************************

'Print the last modified and creation date of a given file

'********************************************************************************

Set fso = CreateObject("Scripting.FileSystemObject")

Set fl = fso.GetFile(FilePath)

print fl.DateLastModified

print fl.DateCreated

'********************************************************************************

'Print the size of the file

'********************************************************************************

Set fso = CreateObject("Scripting.FileSystemObject")

Set fl = fso.GetFile(FilePath)

msgbox fl.Size
'********************************************************************************

'Write a program to display files of a specific type

'********************************************************************************

Function DisplaySpecificFileTypes(FolderPath,FileType)

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder(FolderPath)

Set sFiles = fld.files

For Each sFl in sFiles

If lcase(sFl.type)=lcase(FileType) then

print sFl.name

End if

Next

End Function

'Calling the Function

DisplaySpecificFileTypes "C:\","Text Document"

'********************************************************************************

'Write a program to print the free space in a given drive

'********************************************************************************
Set fso = CreateObject("Scripting.FileSystemObject")

Set d = fso.GetDrive(drvPath)

print "Free Space: " & FormatNumber(d.FreeSpace/1024, 0)

'********************************************************************************

'Write a program to find whether a given folder is a special folder

'********************************************************************************

fldPath="C:\WINDOWS"

Const WindowsFolder =0

Const SystemFolder = 1

Const TemporaryFolder = 2

Set fso = CreateObject("Scripting.FileSystemObject")

If lcase(fso.GetSpecialFolder(WindowsFolder))=lcase(fldPath) or
lcase(fso.GetSpecialFolder(SystemFolder))=lcase(fldPath) or
lcase(fso.GetSpecialFolder(TemporaryFolder))=lcase(fldPath) then

print "Given Folder is a special Folder"

Else

print "Given Folder is not a special Folder"

End if

'********************************************************************************

'Write a program to remove all empty files in the folder

'********************************************************************************
FolderPath="C:\Documents and Settings\sudhakar kakunuri\Desktop\"

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder(FolderPath)

Set sFiles = fld.Files

For Each sFile in sFiles

If sFile.size=0 Then

print sFile.name

End If

Next

'********************************************************************************

'Write a program to Copy contents of one folder to other folder

'********************************************************************************

Function CopyContentstoOtherFolder(SourceFolder,TargetFolder)

Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder(SourceFolder)

Set sFiles = fld.Files

For Each sFile in sFiles

sFile.copy TargetFolder&"\"&sFile.name

Next
Set sFlds = fld.SubFolders

For Each sFld in sFlds

sFld.copy TargetFolder&"\"&sFld.name

Next

End Function

'Calling the Function

CopyContentstoOtherFolder "C:\Documents and Settings\sudhakar


kakunuri\Desktop\Test1","C:\Documents and Settings\sudhakar kakunuri\Desktop\Test2"

'********************************************************************************

'Write a program to check whether a given path represents a file or a folder

'********************************************************************************

Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(fPath) then

Print "Path Representing a File"

Elseif fso.FolderExists(fPath) then

Print "Path Representing a Folder"

End if

Das könnte Ihnen auch gefallen