Solved

delete some files

Posted on 2002-07-01
10
130 Views
Last Modified: 2010-05-02
Hi,

I have a directory that has sometimes up to 10000 files , what I want to do is the following

1 - Look up all the files with names indexN.htm where N is a number from 2 till infinity(not that long probably till 200 or 300)

2 - Delete these indexN.html files

Note : I do not want to delete a file named index.html only files with indexN.htm

Any ideas ??

Thnx a lot
0
Comment
Question by:marounk
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7121981

Private Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Form_Load()
    Dim SHFileOp As SHFILEOPSTRUCT
    With SHFileOp
        'Delete the file
        .wFunc = FO_DELETE
        'Select the file
        .pFrom = "Index*.htm"
        'Allow 'move to recycle bn'
        .fFlags = FOF_ALLOWUNDO
    End With
    'perform file operation
    SHFileOperation SHFileOp
End Sub
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 150 total points
ID: 7121999
 Dim strFolder As String
  strFolder = "E:\work\x\"
 
  Dim strPattern As String
  strPattern = strFolder & "index*.html"
 
  Dim strFile As String
  strFile = Dir(strPattern)
 
  While strFile <> Empty
    If strFile Like "index[0-9]*.html" Then
      Kill strFolder & strFile
    End If
    strFile = Dir()
  Wend


Cheers
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7122215
Probably the fastest and simplest is to issue ten Kills statements and let the operating system do the bulk of the work for you.

For lngLoop = 0 to 9
    Kill "directorypath\index" & lngloop & "*.htm"
Next
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122525
dim ffile as string
const MYPATh as string="C:\myIndexFiles\"

ffiles=dir$(MYPATH & "index*.html",vbarchive)
do while ffile<>""


ffilse=dir$()
loop
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7122533
Sorry:

dim ffile as string
const MYPATH as string="c:\myfiles\"

ffile=Dir$(MYPATH & "index*.html",vbarchive)
do while ffile<>""
    if ffile <> "index.html" then
       kill MYPATH & ffile
    end if

ffile=dir$()
loop
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:marounk
ID: 7123901
Thnx a lot for Everybody
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7124461
marounk,

Since my solution is the simplest and fastest, why wasn't it chosen?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7124485
aikimark,
because your loop is not covering all the files:
>For lngLoop = 0 to 9
although the question is:
> ... long probably till 200 or 300 ...

Also the question says:
1 - Look up all the files with names ...

While your code is short (i don't doubt that), It will need error handling (at least ON ERROR RESUME NEXT) as some files may not exists, and that loop will then run longer than eventually needed...

CHeers
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7124533
Angel,

0*
1*
2*
3*
4*
5*
6*
7*
8*
9*
does indeed cover all possible numeric combinations, skipping the index.htm file (as the questioner wanted).  Since we didn't know whether the "N" in the question was formatted with leading zeroes, I included the 0 in my solution.

Yes, an On Error Resume Next should appear before the
"For lngLoop..."  statement.

=======================================
I interpreted "looking" at the files as ensuring all files were deleted, rather than making a Dir or FileSystemObject call to access them individually, prior to deletion.

=======================================
My loop is guaranteed to issue a maximum of 10 Kill statements.  Some of the Kill statements may not delete any files, but I avoid any (VB) application processing of the file names by bringing the file name into the loop.  The more IndexN.htm files exist, the greater the performance difference between the solutions that delete individual files and my solution.

=======================================
If I were supplying a quasi-literal solution, I would have suggested:
For lngLoop = 0 to 9
   If Dir("directorypath\index" & lngloop & "*.htm")="" Then
   Else
       Kill "directorypath\index" & lngloop & "*.htm"
   End If
Next
Note: no On Error Resume Next is required
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7124565
I agree that i didn't know that KILL accepts wildcards...

Now, to defend my lookup solution:
If you delete files (automatically), some logging might be requested that files were deleted. With the KILL "index*.html" you won't be able to do that...

Learned something basic today (after 4 years of VB !!!)

CHeers
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now