Link to home
Start Free TrialLog in
Avatar of marounk
marounk

asked on

delete some files

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
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image


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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
dim ffile as string
const MYPATh as string="C:\myIndexFiles\"

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


ffilse=dir$()
loop
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
Avatar of marounk
marounk

ASKER

Thnx a lot for Everybody
marounk,

Since my solution is the simplest and fastest, why wasn't it chosen?
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
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
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