PDA

View Full Version : [SOLVED] Delete all txt files within a Folder



Dreamer
07-03-2005, 11:18 AM
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!

Jacob Hilderbrand
07-03-2005, 11:52 AM
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

Dreamer
07-03-2005, 01:36 PM
Does it okay if some text files are in the sub-folder
(e.g A2001,B2001..etc) under folder 2001-2005?

sheeeng
07-03-2005, 06:39 PM
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....:doh:

Jacob Hilderbrand
07-04-2005, 11:05 AM
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.

Dreamer
07-04-2005, 09:18 PM
Hi, there are quite many, can you show me some hints on how to set up a recursive loop?
:)

Jacob Hilderbrand
07-04-2005, 10:45 PM
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.

Dreamer
07-07-2005, 04:27 PM
THX!!!!! It works!

chandansify
07-08-2005, 02:36 AM
It works but It is too dangerous macro.

Run at your own risk.

:devil:

excelliot
07-08-2005, 02:47 AM
THX!!!!! It works!

Be care full, u r playing With fire:whip

slight alteration in the macro code will empty yr C::banghead:

no Ctr + Alt + Panic then:think:

:clap:take care

Bob Phillips
07-08-2005, 03:08 AM
It works but It is too dangerous macro.

Run at your own risk.

:devil:

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.

chandansify
07-08-2005, 03:38 AM
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.


:hi:

sheeeng
07-08-2005, 03:53 AM
This program should be use with extreme care!
You cannot undo the process...

Take extra precaution when exexute this macro....