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!
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!
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
Does it okay if some text files are in the sub-folder
(e.g A2001,B2001..etc) under folder 2001-2005?
I wondering that myself too....Originally Posted by Dreamer
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.
Hi, there are quite many, can you show me some hints on how to set up a recursive loop?
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.
THX!!!!! It works!
It works but It is too dangerous macro.
Run at your own risk.
Be care full, u r playing With fireOriginally Posted by Dreamer
slight alteration in the macro code will empty yr C:
no Ctr + Alt + Panic then
take care
A mighty flame followeth a tiny sparkle!!
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.Originally Posted by chandansify
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.
Originally Posted by xld
you are right master.
This program should be use with extreme care!
You cannot undo the process...
Take extra precaution when exexute this macro....