Results 1 to 10 of 10

Thread: Deleting Subfolders

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736

    Question

    Hi,

    Through VB, I need to delete all subFolders under a Parent folder, whether they contain data or not. Does anyone have an example of looping through the subFolders and removing them ?

    I have tried deleting the Parent folder and then using MkDir to put it back, but keep receiving error messages.

    Thanks for any help !

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'this will kill all sub folders in C:\My Documents
    
    Option Explicit
    
    
    Sub ShowFolderList(folderspec)
    
        Dim fs, f, f1, s, sf
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFolder(folderspec)
        Set sf = f.SubFolders
        For Each f1 In sf
            s = f1.Name
    
            fs.Deletefolder (folderspec & "\" & s)
            s = s & vbCrLf
        Next
       
    End Sub
    
    
    Private Sub Image1_Click()
        Call ShowFolderList("C:\my documents")
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Thank you for that example. That "almost" works all the way. Out of 7 subFolders, 2 are left after I get the message "70 - Permission denied".

    Any suggestions ?

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Some of the files in the folders have attributes set to read only. You need to first pass through your folders and set the attributes to Normal...then it will work.

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Guest
    Use Deltree.

    Code:
    Shell "DELTREE /Y C:\MyFolder\*.*"

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    That wasn't the exact problem, but it made me think. I am reading scanned images from a file server and they are automatically cached on the local disk. The images for the last retrieval were still displayed on the screen, so they were in use and the cache folder(s) containing them could not be deleted.

    So, I just cleared the image from the screen and all seems to be working fine.

    Thanks for your help.

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Matthew:

    For future reference, your code only deletes the contents of the folder, not the folder or sub folders beneath it if they are present and contain files.

    Later,
    Wayne
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Guest

    Re: <?>

    Originally posted by HeSaidJoe
    Matthew:

    For future reference, your code only deletes the contents of the folder, not the folder or sub folders beneath it if they are present and contain files.

    Later,
    Wayne

    It doesn't? Because I've created a copy of a folder with subdirectories in it and it deletes everything (but the main folder, which you can use the RmDir statement).

  9. #9
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Sorry Matthew:

    I tested it but not fully.

    Weird, if you have a space in the folder name it doesn't recognize it and so it does nothing. If my folder is called A Wayne will do nothing but if it's AWayne then it does it's thing.

    My apologies... it does kill the subs but not the parent. I originally tested it on a folder with a space in it's name and because the 3 subs remained I figured it did the parent but not the subs when in fact I must have had nothing in the parent but the 3 subs and that would account for the subs not being touched and my thinking it cleaned the parent.

    Wayne




    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  10. #10
    Guest

    Re: <?>

    Originally posted by HeSaidJoe
    Sorry Matthew:

    I tested it but not fully.

    Weird, if you have a space in the folder name it doesn't recognize it and so it does nothing. If my folder is called A Wayne will do nothing but if it's AWayne then it does it's thing.

    My apologies... it does kill the subs but not the parent. I originally tested it on a folder with a space in it's name and because the 3 subs remained I figured it did the parent but not the subs when in fact I must have had nothing in the parent but the 3 subs and that would account for the subs not being touched and my thinking it cleaned the parent.

    Wayne






    Deltree...
    Kill ..\*.*
    RmDir MyFolder


    Notice: I am always right!

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