?
Solved

Visual Basic 6 dir command not working as expected

Posted on 2010-11-16
2
Medium Priority
?
507 Views
Last Modified: 2012-06-21
I needed a program to loop through all sub-folders of a specified directory and check if a file exists. For testing, I made the folder to loop through c:\ and then I stuck a file called findme.txt in one of the sub-folders. I added code to show all sub-folders in c:\. This worked fine. I then used the DIR function to see if the file existed in any of the sub-folders. As soon as this command is added, I get an error "Invalid procedure call" and it highlights "A = Dir$". If I run the DIR command separately, it works fine. If I run the code to enumerate the sub-folders, it works fine, but combined always gives an error. What am I doing wrong? The code below works, until you uncomment out the Dir function code.
Dim A As String
A = Dir$("C:\", vbDirectory)
Do While A <> "" And A <> "." And A <> ".." And A <> "live"
    If GetAttr("c:\" & A) = "16" Or GetAttr("c:\" & A) = "17" Then
        MsgBox "c:\" & A
        'If Dir("c:\" & A & "\findme.txt") <> "" Then
            'Form1.List1.AddItem "c:\" & A & "\findme.txt"
        'Else
            'Form1.List1.AddItem "c:\" & A
        'End If
    End If
A = Dir$

Loop

Open in new window

0
Comment
Question by:advcom
[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 Comments
 
LVL 39

Accepted Solution

by:
appari earned 2000 total points
ID: 34152683
try this

Dim A As String
Dim dirCol As New Collection

A = Dir$("C:\", vbDirectory)

Do While A <> "" And A <> "." And A <> ".." And A <> "live"
   
    If GetAttr("c:\" & A) = "16" Or GetAttr("c:\" & A) = "17" Then
        MsgBox "c:\" & A
        dirCol.Add "c:\" & A
    End If
   
    A = Dir$

Loop

Dim i As Integer

For i = 1 To dirCol.Count
     
        A = dirCol.Item(i)
     
        If Dir(A & "\findme.txt") <> "" Then
            Form1.List1.AddItem A & "\findme.txt"
        Else
            Form1.List1.AddItem A
        End If

Next
0
 

Author Comment

by:advcom
ID: 34152692
Thanks, that worked perfectly
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

764 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