Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get file names from folder?

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
769 Views, 6 Replies

Get file names from folder?

I'm trying to get the filenames of some drawings in a folder.  I've got the following code, but it's not working.  I get a file count from the line "filecount = objFiles.Count", so it's recognizing "objFiles" as the collection of files in the folder, but everything after that simply does nothing.  Oddly, I can change the "If" statement from "<>" to "=", and it still reads the "Then" statement in both cases.  However, the FileName is blank.  What am I doing wrong?

 

Dim objFile As FileDialogs
    Set objFile = New FileDialogs
    objFile.Title = strFolderName
    objFile.StartInDir = "C:\Documents and Settings"
    strFolderName = objFile.ShowOpen
    strFolderName = Left$(strFolderName, InStrRev(strFolderName, "\") - 1)
MsgBox strFolderName
    Dim fso As Object
    Dim objFiles As Object
    Dim filecount As Integer
    Dim dwgfilenum As Integer
    Dim poo As Variant
     'Create objects to get a count of files in the directory
    Set fso = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set objFiles = fso.GetFolder(strFolderName).Files

    'If Err.Number <> 0 Then
        filecount = objFiles.Count
        For dwgfilenum = 1 To filecount
            If Right((objFiles.Item(dwgfilenum).FullFileName), 3) <> "dwg" Then
            poo = objFiles.Item(dwgfilenum).FullFileName
            MsgBox poo
            'filecount = filecount - 1
            End If
        Next
        MsgBox filecount

6 REPLIES 6
Message 2 of 7
augusto.goncalves
in reply to: Anonymous

Hi,

 

I'm not sure this is a Inventor API question, or if you are facing this only inside Inventor VBA engine. ]

 

Anyway, I tried the following and it is working fine:

 

>>>

Public Sub test()
    Dim fso As New FileSystemObject
    Dim fol As Folder
    Set fol = fso.GetFolder("C:\")
    
    For Each oFile In fol.Files
        Debug.Print oFile.Name
    Next
End Sub

<<<

 

Can you try again with for each instead?

 

Regards,

 

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 3 of 7
Anonymous
in reply to: augusto.goncalves

Probably should have noted that I'm using IV R2010... It recognizes "New FileSystemObject" and "Folder" as a user-defined types, which are not defined.  I'm an amateur ar VBA, and the user-defined type is above my level.  Thanks for the help, anyway.

Message 4 of 7
augusto.goncalves
in reply to: Anonymous

Hi,

 

Those types are defined on the system, but you need to add reference to Microsoft Scripting, which will also help you debug your code.

 

Regards,

 

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 5 of 7
Anonymous
in reply to: augusto.goncalves

How do I add the reference to MS Scripting?  If I need to get into the local drive, I'll have to contact I.T. department, not a problem but I'd like to know what to tell them I want done.  Thanx.

Message 6 of 7
augusto.goncalves
in reply to: Anonymous

Just go to Tool, references, and then select as shown below.

 

reference.png

 

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 7 of 7
Anonymous
in reply to: augusto.goncalves

I added the MS scripting, but I was also able to get the information I needed with the code I had... I just used a "For Each" statement.  Yhanks a bunch for your help!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report