Grouping objects in VBA










0















I am trying to group two object into one list to be iterated in one 'for each' loop. Is it even possible in VBA/VBscript?



For example I would like to delete all files and folders from a specific location basing on last mod. date and I don't want to create two loops.



Set oFolder = fso.GetFolder(FolderPath)
Set oFileList = oFolder.Files
Set oFolderList = oFolder.SubFolders
Set oFullList = oFileList + oFolderList (???)

For Each oFile In oFullList
lastModDate = oFile.DateLastModified
If (lastModDate < PurgeDate) Then
oFile.Delete True
End If
Next









share|improve this question
























  • To merge the lists you will need to loop.

    – Brian M Stafford
    Nov 14 '18 at 13:43











  • @xLokos VBA or VBScript which is it?

    – Lankymart
    Nov 14 '18 at 14:09












  • @Lankymart In this particular case I don't think it makes a difference.

    – Ansgar Wiechers
    Nov 14 '18 at 22:35















0















I am trying to group two object into one list to be iterated in one 'for each' loop. Is it even possible in VBA/VBscript?



For example I would like to delete all files and folders from a specific location basing on last mod. date and I don't want to create two loops.



Set oFolder = fso.GetFolder(FolderPath)
Set oFileList = oFolder.Files
Set oFolderList = oFolder.SubFolders
Set oFullList = oFileList + oFolderList (???)

For Each oFile In oFullList
lastModDate = oFile.DateLastModified
If (lastModDate < PurgeDate) Then
oFile.Delete True
End If
Next









share|improve this question
























  • To merge the lists you will need to loop.

    – Brian M Stafford
    Nov 14 '18 at 13:43











  • @xLokos VBA or VBScript which is it?

    – Lankymart
    Nov 14 '18 at 14:09












  • @Lankymart In this particular case I don't think it makes a difference.

    – Ansgar Wiechers
    Nov 14 '18 at 22:35













0












0








0








I am trying to group two object into one list to be iterated in one 'for each' loop. Is it even possible in VBA/VBscript?



For example I would like to delete all files and folders from a specific location basing on last mod. date and I don't want to create two loops.



Set oFolder = fso.GetFolder(FolderPath)
Set oFileList = oFolder.Files
Set oFolderList = oFolder.SubFolders
Set oFullList = oFileList + oFolderList (???)

For Each oFile In oFullList
lastModDate = oFile.DateLastModified
If (lastModDate < PurgeDate) Then
oFile.Delete True
End If
Next









share|improve this question
















I am trying to group two object into one list to be iterated in one 'for each' loop. Is it even possible in VBA/VBscript?



For example I would like to delete all files and folders from a specific location basing on last mod. date and I don't want to create two loops.



Set oFolder = fso.GetFolder(FolderPath)
Set oFileList = oFolder.Files
Set oFolderList = oFolder.SubFolders
Set oFullList = oFileList + oFolderList (???)

For Each oFile In oFullList
lastModDate = oFile.DateLastModified
If (lastModDate < PurgeDate) Then
oFile.Delete True
End If
Next






excel vba excel-vba object vbscript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:27









Pᴇʜ

21.9k42750




21.9k42750










asked Nov 14 '18 at 13:15









xLokosxLokos

305




305












  • To merge the lists you will need to loop.

    – Brian M Stafford
    Nov 14 '18 at 13:43











  • @xLokos VBA or VBScript which is it?

    – Lankymart
    Nov 14 '18 at 14:09












  • @Lankymart In this particular case I don't think it makes a difference.

    – Ansgar Wiechers
    Nov 14 '18 at 22:35

















  • To merge the lists you will need to loop.

    – Brian M Stafford
    Nov 14 '18 at 13:43











  • @xLokos VBA or VBScript which is it?

    – Lankymart
    Nov 14 '18 at 14:09












  • @Lankymart In this particular case I don't think it makes a difference.

    – Ansgar Wiechers
    Nov 14 '18 at 22:35
















To merge the lists you will need to loop.

– Brian M Stafford
Nov 14 '18 at 13:43





To merge the lists you will need to loop.

– Brian M Stafford
Nov 14 '18 at 13:43













@xLokos VBA or VBScript which is it?

– Lankymart
Nov 14 '18 at 14:09






@xLokos VBA or VBScript which is it?

– Lankymart
Nov 14 '18 at 14:09














@Lankymart In this particular case I don't think it makes a difference.

– Ansgar Wiechers
Nov 14 '18 at 22:35





@Lankymart In this particular case I don't think it makes a difference.

– Ansgar Wiechers
Nov 14 '18 at 22:35












1 Answer
1






active

oldest

votes


















3














What you're asking is not possible with FileSystemObject methods alone. Even if you created a single array or ArrayList from the elements of oFolder.Files and oFolder.SubFolders you'd need two individual loops to do so.



You could use the Shell.Application object for enumerating folder content, but the resulting objects don't have a delete method, so you'd still need FileSystemObject objects and methods for deleting the files/folders:



path = "C:somefolder"

Set app = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")

For Each it In app.NameSpace(path).Items
If it.IsFolder Then
fso.GetFolder(it.Path).Delete
Else
fso.GetFile(it.Path).Delete
End If
Next


However, I fail to see a significant advantage of this approach over using just FileSystemObject methods and looping twice. If you're concerned about duplicating code just wrap the loop in a procedure and call that procedure for both subfolders and files.



path = "C:somefolder"

Sub DeleteItems(list)
For Each it In list
it.Delete
Next
End Sub

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(path)

DeleteItems oFolder.Files
DeleteItems oFolder.SubFolders





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301122%2fgrouping-objects-in-vba%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    What you're asking is not possible with FileSystemObject methods alone. Even if you created a single array or ArrayList from the elements of oFolder.Files and oFolder.SubFolders you'd need two individual loops to do so.



    You could use the Shell.Application object for enumerating folder content, but the resulting objects don't have a delete method, so you'd still need FileSystemObject objects and methods for deleting the files/folders:



    path = "C:somefolder"

    Set app = CreateObject("Shell.Application")
    Set fso = CreateObject("Scripting.FileSystemObject")

    For Each it In app.NameSpace(path).Items
    If it.IsFolder Then
    fso.GetFolder(it.Path).Delete
    Else
    fso.GetFile(it.Path).Delete
    End If
    Next


    However, I fail to see a significant advantage of this approach over using just FileSystemObject methods and looping twice. If you're concerned about duplicating code just wrap the loop in a procedure and call that procedure for both subfolders and files.



    path = "C:somefolder"

    Sub DeleteItems(list)
    For Each it In list
    it.Delete
    Next
    End Sub

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(path)

    DeleteItems oFolder.Files
    DeleteItems oFolder.SubFolders





    share|improve this answer



























      3














      What you're asking is not possible with FileSystemObject methods alone. Even if you created a single array or ArrayList from the elements of oFolder.Files and oFolder.SubFolders you'd need two individual loops to do so.



      You could use the Shell.Application object for enumerating folder content, but the resulting objects don't have a delete method, so you'd still need FileSystemObject objects and methods for deleting the files/folders:



      path = "C:somefolder"

      Set app = CreateObject("Shell.Application")
      Set fso = CreateObject("Scripting.FileSystemObject")

      For Each it In app.NameSpace(path).Items
      If it.IsFolder Then
      fso.GetFolder(it.Path).Delete
      Else
      fso.GetFile(it.Path).Delete
      End If
      Next


      However, I fail to see a significant advantage of this approach over using just FileSystemObject methods and looping twice. If you're concerned about duplicating code just wrap the loop in a procedure and call that procedure for both subfolders and files.



      path = "C:somefolder"

      Sub DeleteItems(list)
      For Each it In list
      it.Delete
      Next
      End Sub

      Set fso = CreateObject("Scripting.FileSystemObject")
      Set oFolder = fso.GetFolder(path)

      DeleteItems oFolder.Files
      DeleteItems oFolder.SubFolders





      share|improve this answer

























        3












        3








        3







        What you're asking is not possible with FileSystemObject methods alone. Even if you created a single array or ArrayList from the elements of oFolder.Files and oFolder.SubFolders you'd need two individual loops to do so.



        You could use the Shell.Application object for enumerating folder content, but the resulting objects don't have a delete method, so you'd still need FileSystemObject objects and methods for deleting the files/folders:



        path = "C:somefolder"

        Set app = CreateObject("Shell.Application")
        Set fso = CreateObject("Scripting.FileSystemObject")

        For Each it In app.NameSpace(path).Items
        If it.IsFolder Then
        fso.GetFolder(it.Path).Delete
        Else
        fso.GetFile(it.Path).Delete
        End If
        Next


        However, I fail to see a significant advantage of this approach over using just FileSystemObject methods and looping twice. If you're concerned about duplicating code just wrap the loop in a procedure and call that procedure for both subfolders and files.



        path = "C:somefolder"

        Sub DeleteItems(list)
        For Each it In list
        it.Delete
        Next
        End Sub

        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oFolder = fso.GetFolder(path)

        DeleteItems oFolder.Files
        DeleteItems oFolder.SubFolders





        share|improve this answer













        What you're asking is not possible with FileSystemObject methods alone. Even if you created a single array or ArrayList from the elements of oFolder.Files and oFolder.SubFolders you'd need two individual loops to do so.



        You could use the Shell.Application object for enumerating folder content, but the resulting objects don't have a delete method, so you'd still need FileSystemObject objects and methods for deleting the files/folders:



        path = "C:somefolder"

        Set app = CreateObject("Shell.Application")
        Set fso = CreateObject("Scripting.FileSystemObject")

        For Each it In app.NameSpace(path).Items
        If it.IsFolder Then
        fso.GetFolder(it.Path).Delete
        Else
        fso.GetFile(it.Path).Delete
        End If
        Next


        However, I fail to see a significant advantage of this approach over using just FileSystemObject methods and looping twice. If you're concerned about duplicating code just wrap the loop in a procedure and call that procedure for both subfolders and files.



        path = "C:somefolder"

        Sub DeleteItems(list)
        For Each it In list
        it.Delete
        Next
        End Sub

        Set fso = CreateObject("Scripting.FileSystemObject")
        Set oFolder = fso.GetFolder(path)

        DeleteItems oFolder.Files
        DeleteItems oFolder.SubFolders






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 13:48









        Ansgar WiechersAnsgar Wiechers

        142k13129187




        142k13129187



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301122%2fgrouping-objects-in-vba%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線