none
list folders and files using .vbs file RRS feed

  • Question

  • Hello guys!
    I'm very new on programming but I'm enjoying it so far .
    I need help to make a vbs fil that returns
    SO FAR I HAVE THIS (but it doesnt separates the fields in different columns)

    Dim fso Dim ObjOutFile Set fso = CreateObject("Scripting.FileSystemObject") Set ObjOutFile = fso.CreateTextFile("OutputFiles.csv") ObjOutFile.WriteLine("Type,File Name,File Path") GetFiles("C:\Intel") 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 For Each ObjFile In ObjFiles ObjOutFile.WriteLine("File," & ObjFile.Name & "," & ObjFile.Path) Next Set ObjSubFolders = ObjFolder.SubFolders For Each ObjFolder In ObjSubFolders ObjOutFile.WriteLine("Folder," & ObjFolder.Name & "," & ObjFolder.Path) GetFiles(ObjFolder.Path) Next End Function

    Thanks in advance all you guys!
    You'll have a very thankfull girl posting here!


    Thursday, August 14, 2014 11:07 AM

All replies

  • Hello,

    test this

    Dim fso,folder, files, YourFolder, fsoTextFile Set fso = CreateObject("Scripting.FileSystemObject") YourFolder = "C:\ExampleFolder" Set fsoTextFile = fso.CreateTextFile(YourFolder&"\List.txt",True) Set folder = fso.GetFolder(YourFolder) Set files = folder.Files For each file in files fsoTextFile.Writeline(file.Name) next fsoTextFile.close



    • Edited by Piedy Tuesday, August 19, 2014 12:51 PM
    Tuesday, August 19, 2014 12:51 PM
  • Your code works perfectly for me - if you open the file using Excel, then the commas will separate the values into columns.  If you want them to be aligned without using a CSV file, then you need to pad the values with blanks - and view the file using a mono-spaced font.  You could also read all the file names first to get the longest one, or just guess like I did ;-)

    Dim fso
    Dim ObjOutFile
    Dim strFN As String

    Sub TestMacro()

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set ObjOutFile = fso.CreateTextFile("OutputFiles2.csv")

    strFN = "File Name                          "   'Note the padding - at least as long as your longest name..

    ObjOutFile.WriteLine "Type    " & strFN & "File Path"

    GetFiles (ThisWorkbook.Path)

    ObjOutFile.Close
    MsgBox "Completed"
    End Sub
    Sub 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

    For Each ObjFile In ObjFiles
    ObjOutFile.WriteLine ("File    " & Left(ObjFile.Name & String(Len(strFN), " "), Len(strFN)) & ObjFile.Path)
    Next

    Set ObjSubFolders = ObjFolder.SubFolders
    For Each ObjFolder In ObjSubFolders

    ObjOutFile.WriteLine ("Folder  " & Left(ObjFolder.Name & String(Len(strFN), " "), Len(strFN)) & ObjFolder.Path)

    GetFiles (ObjFolder.Path)
    Next
        
    End Sub

    Tuesday, August 19, 2014 1:05 PM