Solved

Pattern Searching and File Moving

Posted on 2004-04-26
5
126 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Share codes 68 114
Vb.net Listview 9 76
DIR issue 7 47
Looking for VB6 code to read SQL table export it to ascii 8 24
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now