?
Solved

Pattern Searching and File Moving

Posted on 2004-04-26
5
Medium Priority
?
134 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
[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
  • 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 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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