Thursday, January 14, 2010

Write a program to remove the empty files in the folder

Dim fso, filespec, MyReturn
filespec = ("C:\Test\vbsample.xlsx") ' testfile1.txt") 'C:\Test\testfile
Set fso = CreateObject("Scripting.fileSystemObject")
Set fsosize = fso.Getfile(filespec)
MyReturn = " Name Is: " & fsosize.Name & "file size Is : " & fsosize.Size
MyReturn1 = fsosize.Size
MsgBox MyReturn1
If (MyReturn1 > 1) Then
MsgBox " File have Data so Don't Delete the file"
Else
MyReturn = fso.Deletefile(filespec)
MsgBox " file Deleted"
End If

Write a program to compare 2 folder

Dim fso, fsofol, filespec, fsoNfol, fsoDfol
filespec = "C:\Test\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsofol = fso.GetFolder(filespec)
If (fsofol.IsRootFolder = False) Then
MsgBox "Folder Exists"
End If

Write a program to display all subfolders in a given folder

filespec = "C:\Program Files\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsodri = fso.GetFolder(filespec)
Set fsubfile = fsodri.Subfolders
For Each fsubfile1 In fsubfile
MyMsg = MyMsg & "Name is : " & fsubfile1.Name & "
"
Next
MsgBox MyMsg

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

Dim fso, fspace, filespec
filespec = "D:\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fspace = fso.GetDrive(fso.GetDrivename(filespec))
MyReturn = "Drive Name : " & fspace.volumeName & " Available space is : " & fspace.Availablespace
MsgBox MyReturn

Write a program to display files of a specific type

Dim fso, gfiles, filespec
'filespec = "C:\testfile.txt"
filespec = "C:\vbsample.xlsx"
Set fso = CreateObject("Scripting.FileSystemObject")
Set gfiles = fso.GetFile(filespec)
MyReturn = gfiles.Name & " File Type Is : " & gfiles.Type
MsgBox MyReturn

Print the size of the file and size on the disk

Dim fso, gfiles, filespec
filespec = "C:\Program Files\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set gfiles = fso.GetFolder(filespec)
MyReturn = gfiles.Name & " Used:" & gfiles.Size & "Bytes"
MsgBox MyReturn

Write a program to print current drive name

filespec = "C:\Program Files\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoobj = fso.GetFolder(filespec)
Set fsodri = fso.getdrive(fso.getdrivename(filespec))
'Display the Root folder
fRoot = fsodri.rootfolder
MsgBox fRoot

Find the file location, root folder, last modified, Last Data Accessed

Dim fso, fsoobj
filespec = "c:\testfile.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoobj = fso.GetFile(filespec)
'File Location for testing
fPath = fsoobj.Path
'File Created Date
fPath = fPath & "File Created:" & fsoobj.DateCreated
'File Last Accessed
fPath = fPath & "Data Last Accessed:" & fsoobj.DateLastAccessed
'File Last Modified
fPath = fPath & "Data Last Modified:" & fsoobj.DateLastModified
MsgBox fPath
'Display the Drive Name & Location
Set fsodri = fso.getdrive(fso.getdrivename(filespec))
MsgBox fsodri

Write a program to print each letter

Dim MyString
MyString = "Vinodh test !"
For i = 1 To 999 Step 1
MyStrReturn = Left(MyString, i)
MsgBox MyStrReturn
MyStrlen = Len(MyString)
'MsgBox MyStrlen
If (i = MyStrlen) Then
Exit For
End If
Next