Consulting

Results 1 to 13 of 13

Thread: Delete all txt files within a Folder

  1. #1

    Delete all txt files within a Folder

    Dear all,

    How can I delete all files with *.Txt?

    C:\2001
    C:\2002
    C:\2003
    C:\2004
    C:\2005

    There are over 500 text files in the above 5 folders,
    Can I delete all of them once?

    Thanks in advance!

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ok, this will work, but note that all the txt files will be deleted and this cannot be undone.


    Option Explicit
     
    Sub Main()
    Dim i               As Long
    For i = 2001 To 2005
            Call KillTxt("C:\" & i)
        Next i
    End Sub
     
     
    Sub KillTxt(Path As String)
    Dim FileName        As String
    FileName = Dir(Path & "\*.txt", vbNormal)
        Do Until FileName = ""
            Kill Path & "\" & FileName
            FileName = Dir()
        Loop
    End Sub

  3. #3
    Does it okay if some text files are in the sub-folder
    (e.g A2001,B2001..etc) under folder 2001-2005?

  4. #4
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by Dreamer
    Dear all,

    How can I delete all files with *.Txt?

    C:\2001
    C:\2002
    C:\2003
    C:\2004
    C:\2005

    There are over 500 text files in the above 5 folders,
    Can I delete all of them once?

    Thanks in advance!
    I wondering that myself too....

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    If there are just a few folders you can run the macro for each separate folder. If there are many sub folders, then we would need to set up a recursive loop.

  6. #6
    Hi, there are quite many, can you show me some hints on how to set up a recursive loop?

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    First, set a reference to the Microsoft Scripting Runtime (Tools | References).


    Option Explicit
     
    Sub Main()
    Dim FSO             As FileSystemObject
    Dim F               As Folder
    Set FSO = New FileSystemObject
        Set F = FSO.GetFolder(Root Folder Path Here)
        Call KillTextRecursive(F)
        Set F = Nothing
        Set FSO = Nothing
    End Sub
     
    Sub KillTextRecursive(ByVal Fld As Folder)
    Dim SubF            As Folder
    Dim FileName        As String
    For Each SubF In Fld.SubFolders
            FileName = Dir(SubF.Path & "\*.txt", vbNormal)
            Do Until FileName = ""
                Kill SubF.Path & "\" & FileName
                FileName = Dir()
            Loop
            KillTextRecursive SubF
        Next SubF
    Set SubF = Nothing
    End Sub

    Just specify the path to the root folder in the code above and it will search through all sub folders.

  8. #8
    THX!!!!! It works!

  9. #9
    It works but It is too dangerous macro.

    Run at your own risk.


  10. #10
    Quote Originally Posted by Dreamer
    THX!!!!! It works!
    Be care full, u r playing With fire

    slight alteration in the macro code will empty yr C:

    no Ctr + Alt + Panic then

    take care
    A mighty flame followeth a tiny sparkle!!



  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by chandansify
    It works but It is too dangerous macro.

    Run at your own risk.

    That is true of all code, but the sensible computer user will have backups, and they will test the code by taking out the 'dangerous' statements and substitute some test message, write to a log, MsgBox, Debug.Print etc., and TEST it.

    Any code is dangerous. Setting a single cell to 0 seems pretty innocuous, but if that is on a data entry sheet, it could have significant effects on the financial bottom line.

    If you are going to develop code, you need to understand and practice the dfisciplines.

  12. #12

    Smile

    Quote Originally Posted by xld
    That is true of all code, but the sensible computer user will have backups, and they will test the code by taking out the 'dangerous' statements and substitute some test message, write to a log, MsgBox, Debug.Print etc., and TEST it.

    Any code is dangerous. Setting a single cell to 0 seems pretty innocuous, but if that is on a data entry sheet, it could have significant effects on the financial bottom line.

    If you are going to develop code, you need to understand and practice the dfisciplines.



    you are right master.



  13. #13
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    This program should be use with extreme care!
    You cannot undo the process...

    Take extra precaution when exexute this macro....

Posting Permissions

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