Solved

delete some files

Posted on 2002-07-01
10
131 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now