ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   deleting file(s) by their file extension (https://www.excelbanter.com/excel-programming/313427-deleting-file-s-their-file-extension.html)

jt46[_4_]

deleting file(s) by their file extension
 

Does anyone knows how to write a simple code to delete a file or file
by their file extension? Thanks in advance.

Joh

--
jt4
-----------------------------------------------------------------------
jt46's Profile: http://www.excelforum.com/member.php...fo&userid=1515
View this thread: http://www.excelforum.com/showthread.php?threadid=26891


Tom Ogilvy

deleting file(s) by their file extension
 
Kill "C:\test1\*.txt"

These don't go to the recycle bin, so make sure you know what you want to
delete.

--
Regards,
Tom Ogilvy

"jt46" wrote in message
...

Does anyone knows how to write a simple code to delete a file or files
by their file extension? Thanks in advance.

John


--
jt46
------------------------------------------------------------------------
jt46's Profile:

http://www.excelforum.com/member.php...o&userid=15154
View this thread: http://www.excelforum.com/showthread...hreadid=268917




K Dales[_2_]

deleting file(s) by their file extension
 
This function will delete all files with a given extension in a specified
folder; e.g. Clean("C:\TEMP","xml") will delete any .xml files in the
C:\TEMP folder. It returns the number of files deleted and also (if you need
to check the file names) prints the files deleted in the debugger.

Public Function Clean(FolderPath As String, Extension As String) As Integer

Dim FileName As String, CountFiles As Integer

CountFiles = 0
FileName = Dir(FolderPath & "\*." & Extension)
While FileName < ""
debug.print FolderPath & "\" & FileName
Kill FolderPath & "\" & FileName
CountFiles = CountFiles + 1
FileName = Dir
Wend

Clean = CountFiles

End Function


"jt46" wrote:


Does anyone knows how to write a simple code to delete a file or files
by their file extension? Thanks in advance.

John


--
jt46
------------------------------------------------------------------------
jt46's Profile: http://www.excelforum.com/member.php...o&userid=15154
View this thread: http://www.excelforum.com/showthread...hreadid=268917



Tom Ogilvy

deleting file(s) by their file extension
 
Altering files in a Dir Loop can cause problems - or so sayeth the Knowledge
Base. I have never encountered any, but one never knows. In any event,
this can all be done in one line, so no use looping or using Dir.

Public Function Clean(FolderPath As String, Extension As String)
Kill FolderPath & "*." & Extension
End Sub

Error checking for valid arguments can be added.

--
Regards,
Tom Ogilvy


"K Dales" wrote in message
...
This function will delete all files with a given extension in a specified
folder; e.g. Clean("C:\TEMP","xml") will delete any .xml files in the
C:\TEMP folder. It returns the number of files deleted and also (if you

need
to check the file names) prints the files deleted in the debugger.

Public Function Clean(FolderPath As String, Extension As String) As

Integer

Dim FileName As String, CountFiles As Integer

CountFiles = 0
FileName = Dir(FolderPath & "\*." & Extension)
While FileName < ""
debug.print FolderPath & "\" & FileName
Kill FolderPath & "\" & FileName
CountFiles = CountFiles + 1
FileName = Dir
Wend

Clean = CountFiles

End Function


"jt46" wrote:


Does anyone knows how to write a simple code to delete a file or files
by their file extension? Thanks in advance.

John


--
jt46
------------------------------------------------------------------------
jt46's Profile:

http://www.excelforum.com/member.php...o&userid=15154
View this thread:

http://www.excelforum.com/showthread...hreadid=268917






All times are GMT +1. The time now is 03:44 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com