Solved

delete some files

Posted on 2002-07-01
10
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 46

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 46

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 46

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

623 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