Solved

Pattern Searching and File Moving

Posted on 2004-04-26
5
128 Views
Last Modified: 2011-04-05
Hi

I was wondering if anyone has any code that would allow me to search the whole of my hard disk for particular files (*.NSF), present me with the results and then allow me to move the file to a different drive? (N:)

I also need this code to be able to skip a directory that I specify.

Does anyone know how I can do this? I have only just started out in VB

Thanks in Advanced

Steven
0
Comment
Question by:stewatts
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:jmwheeler
ID: 10919142
Add a reference to the 'Microsoft Scripting Runtime' in your project references (Project -> References).  Then you can use this code.


Option Explicit
Private fso As FileSystemObject
Private fldr As Folder
Private fldr2 As Folder
Private fl As File

Private Sub Command1_Click()
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fldr = fso.GetFolder("C:\")
    GetSubFolderFiles fldr  
End Sub

Private Sub GetSubFolderFiles(fldr As Folder)
    For Each fl In fldr.Files
        If fl.Name Like "*.nsf" Then
            List1.AddItem fl.Path
        End If
    Next
    For Each fldr2 In fldr.SubFolders
        GetSubFolderFiles fldr2
    Next
End Sub

That code will add all the .nsf files on the C: drive to a list box.  Then if you want to move them you can use this code.

Private Sub Command2_Click()
    For i = 0 To List1.ListCount - 1
        Set fl = fso.GetFile(List1.List(i))
        fso.MoveFile fl.Path, "N:\" & fl.Name
    Next i
End Sub

When you are all done you should free everything up.

fldr = nothing
fldr2 = nothing
fl = nothing
fso = nothing
0
 

Author Comment

by:stewatts
ID: 10919236
This seems to work great except it gets so far through and throws a run-time error saying access denied. I am local admin on the box. The last successful file found was in the c:\recycler dir.

I also need to be able to skip a certain directory which I know will cause a problem (its a problem with SMS that creates a chinese character dir)

Thanks
0
 
LVL 11

Accepted Solution

by:
jmwheeler earned 250 total points
ID: 10919477
alright, at the top of the GetSubFolderFiles Sub type "On Error Resume Next"

ex.
Private Sub GetSubFolderFiles(fldr As Folder)
On Error Resume Next

As far as skipping a directory you will have to store the full path of the directory somewhere (textbox, variable, etc.)  Then run a check to see if the path contains that directory.  You might not have to do this though if you add the line suggested above.
0
 

Author Comment

by:stewatts
ID: 10925943
That is fantastic, no errors now when scanning the file system :-)

when I try and move the file though I get i not defined (within the Command2_Click code?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 68
VBS file using code from 2nd file (txt or vbs) 4 36
MS Date Picker 64 bit 32 bit issue 12 53
using web browser with BING 40 119
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now