Results 1 to 7 of 7

Thread: export list of subdirectories to excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    export list of subdirectories to excel

    Hi, I came across this script in a old thread which does nearly what i want it to do hoping it can be modified to do what i want but i dont know how the script below will export all .txt files in said directory to excel, I know i can alter the file extension to other file types but is there a way for it to send folder names in said directory to excel, also when dealing with file types is there away of just importing the names without the extension on the end. many thanks for any help. James.

    Code:
    Dim fso, objFolder, obFileList, folderpath, fullpath, xl, i, j
    folderpath = "C:\Test"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(folderpath)
    Set objFileList = objFolder.Files
    
    Set xl = CreateObject("Excel.application")
    if not fso.folderexists("C:\Forms") then
        fso.CreateFolder("C:\Forms")
    end if
    if not fso.fileexists("c:\Forms\Test.xls") then 
        xl.application.workbooks.add
        xl.application.save("C:\Forms\Test.xls")
        xl.application.quit
    End if
    xl.Application.Workbooks.Open "C:\Forms\Test.xls"
    xl.Application.Visible = True
    set objSheet = xl.ActiveWorkbook.Worksheets(1)
    
    i = 1
    j = 1
    
    For Each File In objFileList
        fullpath = folderpath & "\" & file.name
        if fso.getextensionname(fullpath) = "txt" then
            objSheet.Cells(i, j).value = file.name
            i = i + 1
        end if
    Next
    
    xl.DisplayAlerts = FALSE
    objSheet.saveas("C:\Forms\Test.xls")
    xl.application.quit

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: export list of subdirectories to excel

    Here's a script that will help you list all folders.
    vb Code:
    1. Dim objFSO, objFile, objFolder, objFc, objF
    2.  
    3. Set objFSO = CreateObject("Scripting.FileSystemObject")
    4. Set objFolder = objFSO.GetFolder("d:\")
    5. Set objFc = objFolder.SubFolders
    6.  
    7. For Each objF in objFC
    8.     Wscript.Echo "Listing sub folder  " & objF.Name
    9. Next
    10.  
    11.  
    12. Set objFolder = Nothing
    13. Set objFSO = Nothing
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: export list of subdirectories to excel

    I modified your script and merged it with mine. I hope this is what you want.

    vb Code:
    1. Dim objFolder, obFileList, folderpath, fullpath, xl, i, j
    2. folderpath = "C:\Test"
    3.  
    4. Dim objFSO, objFile, objFc, objF
    5.  
    6. Set objFSO = CreateObject("Scripting.FileSystemObject")
    7. Set objFolder = objFSO.GetFolder("d:\")
    8. Set objFc = objFolder.SubFolders
    9. Set xl = CreateObject("Excel.application")
    10. if not objFSO.folderexists("d:\Forms") then
    11.         objFSO.CreateFolder("d:\Forms")
    12. end if
    13. if not objFSO.fileexists("d:\Forms\Test.xls") then
    14.     xl.application.workbooks.add
    15.     xl.application.save("d:\Forms\Test.xls")
    16.     xl.application.quit
    17. End if
    18. xl.Application.Workbooks.Open "d:\Forms\Test.xls"
    19. xl.Application.Visible = True
    20. set objSheet = xl.ActiveWorkbook.Worksheets(1)
    21.  
    22. i = 1
    23. j = 1
    24.  
    25. For Each objF in objFC
    26.     objSheet.Cells(i, j).value = objF.Name
    27.     i = i + 1
    28. Next
    29.  
    30. xl.DisplayAlerts = FALSE
    31. objSheet.saveas("d:\Forms\Test.xls")
    32. xl.application.quit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    Re: export list of subdirectories to excel

    Hi, thanks for your reply I apreciate your time and trouble in trying to help, I have tried your script but am getting an error message says
    line: 11
    char: 1
    error: expected statement
    code: 800A0400
    any ideas sorry I dont even have know the basics of programming vbs.
    thanks, James.

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: export list of subdirectories to excel

    Quote Originally Posted by mrwonker View Post
    Hi, thanks for your reply I apreciate your time and trouble in trying to help, I have tried your script but am getting an error message says
    line: 11
    char: 1
    error: expected statement
    code: 800A0400
    any ideas sorry I dont even have know the basics of programming vbs.
    thanks, James.
    Do you have D:\ on your machine and do you have permissions to create a folder on it?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    6

    Re: export list of subdirectories to excel

    d: is my dvd drive I had alreardy changed all the d: in the script to C: my main hard drive and am running as administrator
    thanks.

  7. #7
    New Member
    Join Date
    Feb 2014
    Posts
    3

    Re: export list of subdirectories to excel

    Quote Originally Posted by mrwonker View Post
    d: is my dvd drive I had alreardy changed all the d: in the script to C: my main hard drive and am running as administrator
    thanks.
    Make sure the Excel spreadsheet exists, also make sure Excel is installed on the computer you are running the script on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width