Solved

Pattern Searching and File Moving

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 66
Windows Script Host failed (Access is denied.) error 6 221
Add a task in Outlook from access 11 42
IF ELSE Statement in Excel Macro VBA 16 75
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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

809 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