Sunday, April 2, 2017

File System Object

'FileSystemObject Object
'
'Description
'
'Provides access to a computer's file system.
'
'Syntax
'
'Scripting.FileSystemObject
'
'Remarks
'
'The following code illustrates how the FileSystemObject is used to return a TextStream object that can be read from or written to:

Option Explicit
'List all files in a given folder to sheet1
Sub listfilesinfolder()

Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fl As Scripting.File
Dim fldname As String
Dim counter As Integer

Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder(fldname & "\")

fldname = Application.InputBox("Enter the folder name to display all the files under it", "Folder Name")

Sheet1.Activate
Range("a1").Activate

For Each fl In fld.Files
    ActiveCell.value = fl.Name
    ActiveCell.Offset(0, 1).value = fl.Type
    ActiveCell.Offset(0, 2).value = fl.Size
    ActiveCell.Offset(1, 0).Select
Next
End Sub

'Creating a folder using file system object and restricting if folder exist
'Copying files to a destination and checking if already exist
Sub UsingTheScriptRunTimeLibrary()

Dim fso As Scripting.FileSystemObject
Dim NewFolderPath As String
Dim OldFolderPath As String

Set fso = New Scripting.FileSystemObject

NewFolderPath = Environ("UserProfile") & "\Desktop\SRTL"
OldFolderPath = Environ("UserProfile") & "\Desktop\Akshit - Rhyams"

If Not fso.FolderExists(NewFolderPath) Then

    fso.CreateFolder (NewFolderPath)
   
End If

If fso.FileExists(OldFolderPath & "\Mamy poko pants.flv") Then
        fso.CopyFile Source:=OldFolderPath & "\Mamy poko pants.flv", _
            Destination:=NewFolderPath & "\Mamy poko pants.flv", overwritefiles:=True
End If

Set fso = Nothing
End Sub

'Creating a folder using file system object and restricting if folder exist
Sub UsingTheScriptRunTimeLibrary2()

Dim fso As Scripting.FileSystemObject
Dim fil As Scripting.File
Dim NewFolderPath As String
Dim OldFolderPath As String

NewFolderPath = Environ("UserProfile") & "\Desktop\SRTL"
OldFolderPath = Environ("UserProfile") & "\Desktop\Akshit - Rhyams"

Set fso = New Scripting.FileSystemObject

If Not fso.FolderExists(NewFolderPath) Then

    fso.CreateFolder (NewFolderPath)
   
End If

If fso.FileExists(OldFolderPath & "\Mamy poko pants.flv") Then
   
Set fil = fso.GetFile(OldFolderPath & "\Mamy poko pants.flv")
   
    If fil.Size > 2000000 Then
        fil.Copy NewFolderPath & "\" & fil.Name
   
    End If
   
End If

Set fso = Nothing
End Sub
'Filerting mp4 file through FileSystem Object
Sub UsingTheScriptRunTimeLibrary3()

Dim fso As Scripting.FileSystemObject
Dim fil As Scripting.File
Dim OldFolder As Scripting.Folder
Dim NewFolderPath As String
Dim OldFolderPath As String

NewFolderPath = Environ("UserProfile") & "\Desktop\SRTL"
OldFolderPath = Environ("UserProfile") & "\Desktop\Akshit - Rhyams"

Set fso = New Scripting.FileSystemObject

If fso.FolderExists(OldFolderPath) Then

    Set OldFolder = fso.GetFolder(OldFolderPath)

        If Not fso.FolderExists(NewFolderPath) Then
            fso.CreateFolder (NewFolderPath)
        End If

            For Each fil In OldFolder.Files
                If Left(fso.GetExtensionName(fil.Path), 3) = "mp4" Then
                    fil.Copy NewFolderPath & "\" & fil.Name
                End If
            Next fil
End If

Set fso = Nothing

End Sub

No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...