Cari Blog Ini

19 Juli 2013

How To Get Path Folder Name

Want to :

* Using scripting File System Object 

* Using VBScript

* Recursively List All Files and SubFolders Inside a Folder at Any Level Depth





Step : 
1. Example of Folder


2. Create new file, example name "GetPathFolderName.vbs"
and file the below code :

-----CODE START-----
Dim fso
Dim ObjOutFile

'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Create an output file
Set ObjOutFile = fso.CreateTextFile("GetPathFolderName_Output.csv")

'Writing CSV headers
ObjOutFile.WriteLine("Type,File Name,Date Modified,File Size,File Path")

'Call the GetFile function to get all files
GetFiles("C:\Work")

'Close the output file
ObjOutFile.Close

WScript.Echo("Completed")

Function GetFiles(FolderName)
    On Error Resume Next
     
    Dim ObjFolder
    Dim ObjSubFolders
    Dim ObjSubFolder
    Dim ObjFiles
    Dim ObjFile

    Set ObjFolder = fso.GetFolder(FolderName)
    Set ObjFiles = ObjFolder.Files
     
    'Write all files to output files
    For Each ObjFile In ObjFiles
        ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.DateLastModified & "," & ObjFile.Size & "," & ObjFile.Path)
    Next

    'Getting all subfolders
    Set ObjSubFolders = ObjFolder.SubFolders
     
    For Each ObjFolder In ObjSubFolders
        'Writing SubFolder Name and Path
        ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFile.DateLastModified & "," & ObjFile.Size & "," & ObjFolder.Path)
         
        'Getting all Files from subfolder
        GetFiles(ObjFolder.Path)
    Next
     
End Function
-----CODE END-----




3. Try to run the script, name "GetPathFolderName.vbs"


4. The file output, name "GetPathFolderName_Output.csv" will created automatically.


5. Try to open the output file, using Microsoft Excel



Source:
http://easyprograming.com/index.php/vb-script/34-list-all-files-and-subfolders-inside-a-folder-using-vbscript