Solved

delete some files

Posted on 2002-07-01
10
134 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 143

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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
 
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 143

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 143

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

830 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