PC Review


Reply
Thread Tools Rate Thread

Deleting files in a directory

 
 
=?Utf-8?B?U2Ft?=
Guest
Posts: n/a
 
      7th Jun 2007
I have a process that creates csv files in a directory, however a byproduct
is an identical log file, e.g. 5C015000. If you were to rename the file with
a .xls extension, it would be exactly the same as the csv that I'm saving. It
has no extension and is file type "File".

If the directory were C:\GLPVC\ what code can I use to loop through all
files in the directory (there may be hundreds) and delete all files that
begin with a numeric character or whose file type is "File"?
 
Reply With Quote
 
 
 
 
JohnELaw
Guest
Posts: n/a
 
      7th Jun 2007
On Jun 7, 3:15 pm, Sam <S...@discussions.microsoft.com> wrote:
> I have a process that creates csv files in a directory, however a byproduct
> is an identical log file, e.g. 5C015000. If you were to rename the file with
> a .xls extension, it would be exactly the same as the csv that I'm saving. It
> has no extension and is file type "File".
>
> If the directory were C:\GLPVC\ what code can I use to loop through all
> files in the directory (there may be hundreds) and delete all files that
> begin with a numeric character or whose file type is "File"?


Here try this...

Function DeleteFiles(MyWildcard As String) As Long
Dim Match
Match = Dir(MyWildcard)
If Len(Match) > 0 Then
Do
MsgBox Match
Match = Dir
Loop Until Len(Match) = 0
End If
End Function

You will call it with a line that looks like...

Call DeleteFiles("C:\My Docs\*.*")

Now, instead of the line that reads...

MsgBox Match

....put a line that reads...

Kill Match

You would effectively be going through the folder one file at a time
and looking for a file size greater than 0. Any that match this
criteria get the ax. You can further make this dynamic by adding rules
to this code...

 
Reply With Quote
 
JohnELaw
Guest
Posts: n/a
 
      8th Jun 2007
On Jun 7, 3:34 pm, JohnELaw <sean_dor...@yahoo.com> wrote:
> On Jun 7, 3:15 pm, Sam <S...@discussions.microsoft.com> wrote:
>
> > I have a process that creates csv files in a directory, however a byproduct
> > is an identical log file, e.g. 5C015000. If you were to rename the file with
> > a .xls extension, it would be exactly the same as the csv that I'm saving. It
> > has no extension and is file type "File".

>
> > If the directory were C:\GLPVC\ what code can I use to loop through all
> > files in the directory (there may be hundreds) and delete all files that
> > begin with a numeric character or whose file type is "File"?

>
> Here try this...
>
> Function DeleteFiles(MyWildcard As String) As Long
> Dim Match
> Match = Dir(MyWildcard)
> If Len(Match) > 0 Then
> Do
> MsgBox Match
> Match = Dir
> Loop Until Len(Match) = 0
> End If
> End Function
>
> You will call it with a line that looks like...
>
> Call DeleteFiles("C:\My Docs\*.*")
>
> Now, instead of the line that reads...
>
> MsgBox Match
>
> ...put a line that reads...
>
> Kill Match
>
> You would effectively be going through the folder one file at a time
> and looking for a file size greater than 0. Any that match this
> criteria get the ax. You can further make this dynamic by adding rules
> to this code...


Actually, you can remove the part that says as long...then you could
use it like...

Call DeleteFiles("C:\My Folder\*.*")

 
Reply With Quote
 
=?Utf-8?B?dXJrZWM=?=
Guest
Posts: n/a
 
      8th Jun 2007
You can also use the FileSystemObject object for that.

Loop through the files in C:\GLPVC\ , check if the file type is "Type" and
the first character in the file name is numeric, and, if so, delete the file:

Sub deleteFiles()

fPath = "C:\GLPVC\"

Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = fso.GetFolder(fPath)

For Each file In folder.Files

If file.Type = "File" And _
IsNumeric(Left(file.Name, 1)) Then
Debug.Print file.Name
'file.Delete
End If

Next

Set folder = Nothing
Set fso = Nothing

End Sub


Instead of checking the file type, you can delete all the files without
extension:


Sub deleteFiles1()

fPath = "C:\GLPVC\"

Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = fso.GetFolder(fPath)

For Each file In folder.Files

If fso.GetExtensionName(file) = "" And _
IsNumeric(Left(file.Name, 1)) Then
Debug.Print file.Name
'file.Delete
End If

Next

Set folder = Nothing
Set fso = Nothing

End Sub




--
urkec


"Sam" wrote:

> I have a process that creates csv files in a directory, however a byproduct
> is an identical log file, e.g. 5C015000. If you were to rename the file with
> a .xls extension, it would be exactly the same as the csv that I'm saving. It
> has no extension and is file type "File".
>
> If the directory were C:\GLPVC\ what code can I use to loop through all
> files in the directory (there may be hundreds) and delete all files that
> begin with a numeric character or whose file type is "File"?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting files and folders in the C:\Windows directory B Windows XP General 3 12th Aug 2010 04:14 PM
Deleting files from one directory that are not present in another PhilHibbs Windows XP General 5 5th Dec 2008 03:34 PM
Creating and Deleting files from Virtual Directory ankit.sri Microsoft C# .NET 1 12th Jun 2008 07:55 AM
Deleting directory path and files. VB.net 2003 Kimera.Kimera@gmail.com Microsoft VB .NET 3 30th Dec 2006 02:13 AM
deleting files from web server directory Simon Microsoft ASP .NET 1 10th Nov 2005 01:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:06 AM.