Solved

Pattern Searching and File Moving

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

930 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