FSO
The FileSystemObject (FSO) object model allows you to use the familiar object.method syntax with a rich set of properties, methods, and events to process folders and files.
The FSO object model gives your server-side applications the ability to create, alter, move, and delete folders, or to detect if particular folders exist, and if so, where. You can also find out information about folders, such as their names, the date they were created or last modified, and so forth.
The FSO object model also makes it easy to process files. When processing files, the primary goal is to store data in a space- and resource-efficient, easy-to-access format. You need to be able to create files, insert and change the data, and output (read) the data. Since storing data in a database, such as Access or SQL Server, adds a significant amount of overhead to your application, storing your data in a binary or text file may be the most efficient solution. You may prefer not to have this overhead, or your data access requirements may not require all the extra features associated with a full-featured database.
- The FSO object model, which is contained in the Scripting type library (Scrrun.dll), supports text file creation and manipulation through the TextStream object. Although it does not yet support the creation or manipulation of binary files, future support of binary files is planned.
Playing with Folders
How to creat Folders?
Dim objFSO
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
‘The folder “MyFolder will be created in “C” drive.
Set objFolder = objFSO.CreateFolder(“C:\MyFolder”)
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
‘The folder “MyFolder will be created in “C” drive.
Set objFolder = objFSO.CreateFolder(“C:\MyFolder”)
Verify that a Folder Exists….!
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
If objFSO.FolderExists(“C:\MyFolder”) Then
MsgBox “Folder exists.”
Else
MsgBox “Folder does not exist.”
End If
If objFSO.FolderExists(“C:\MyFolder”) Then
MsgBox “Folder exists.”
Else
MsgBox “Folder does not exist.”
End If
Verify that a Folder Exists else create the folder….!
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
If objFSO.FolderExists(“C:\MyFolder”) Then
MsgBox “Folder exists.”
Else
MsgBox “Folder does not exist.”
objFSO.CreateFolder(“C:\MyFolder”)
End If
If objFSO.FolderExists(“C:\MyFolder”) Then
MsgBox “Folder exists.”
Else
MsgBox “Folder does not exist.”
objFSO.CreateFolder(“C:\MyFolder”)
End If
How to Count number of folders inside a folder?
Function foldercount(strFolderPath)
Dim objfso,objFolder,objFolders,intfoldercount,strSubFoldername,strSubFolderPath
Set objFso = Createobject(“Scripting.FileSystemObject”)
Set objFolder = objFso.GetFolder(strFolderPath)
Set objFolders=objFolder.SubFolders
‘Counting the number of folders in the Parent folder
intfoldercount = objFolders.count
For each objfold in objFolders
‘Collecting the name of the subfolders
strSubFoldername=objfold.Name
‘Collecting the path of the subfolders
strSubFolderPath=objfold.path
print strSubFolderPath
‘Counting all the folders in each subfolder – recursion point
intfoldercount=intfoldercount + foldercount(strSubFolderPath)
Next
foldercount=intfoldercount
End function
Dim objfso,objFolder,objFolders,intfoldercount,strSubFoldername,strSubFolderPath
Set objFso = Createobject(“Scripting.FileSystemObject”)
Set objFolder = objFso.GetFolder(strFolderPath)
Set objFolders=objFolder.SubFolders
‘Counting the number of folders in the Parent folder
intfoldercount = objFolders.count
For each objfold in objFolders
‘Collecting the name of the subfolders
strSubFoldername=objfold.Name
‘Collecting the path of the subfolders
strSubFolderPath=objfold.path
print strSubFolderPath
‘Counting all the folders in each subfolder – recursion point
intfoldercount=intfoldercount + foldercount(strSubFolderPath)
Next
foldercount=intfoldercount
End function
print foldercount(“C:\Documents and Settings\ADMIN\Desktop\New Folder”)
Counting the Number of lines in a Notepad.
Function countTotalLines(byval filepath)
Dim ofso,ofso1
Set ofso=CreateObject(“Scripting.FilesystemObject”)
Set ofso1=ofso.opentextfile(filepath,1)
ofso1.readall
countTotalLines=ofso1.line
End Function
Dim ofso,ofso1
Set ofso=CreateObject(“Scripting.FilesystemObject”)
Set ofso1=ofso.opentextfile(filepath,1)
ofso1.readall
countTotalLines=ofso1.line
End Function
‘Calling the function here
intlines=countTotalLines(“C:\Documents and Settings\ADMIN\Desktop\book.txt”)
msgbox intlines
intlines=countTotalLines(“C:\Documents and Settings\ADMIN\Desktop\book.txt”)
msgbox intlines
Finding a string in a notepad and replacing it through another string.
Function FindandReplaceinTheTextFile(byval filepath,strFindData,strReplaceData)
Dim ofso,ofso1,ofso2
Set ofso=CreateObject(“Scripting.FilesystemObject”)
Set ofso1=ofso.opentextfile(filepath,1)
strData=ofso1.readall
If (instr(1,strData,strFindData)) Then
strData= replace( strData,strFindData,strReplaceData)
Set ofso2=ofso.opentextfile(filepath,2)
ofso2.write strData
End If
End function
‘Calling the function here
call FindandReplaceinTheTextFile(“C:\Documents and Settings\ADMIN\Desktop\book.txt”,”h”,”z”)
Dim ofso,ofso1,ofso2
Set ofso=CreateObject(“Scripting.FilesystemObject”)
Set ofso1=ofso.opentextfile(filepath,1)
strData=ofso1.readall
If (instr(1,strData,strFindData)) Then
strData= replace( strData,strFindData,strReplaceData)
Set ofso2=ofso.opentextfile(filepath,2)
ofso2.write strData
End If
End function
‘Calling the function here
call FindandReplaceinTheTextFile(“C:\Documents and Settings\ADMIN\Desktop\book.txt”,”h”,”z”)
No comments:
Post a Comment