8 Replies - 1712 Views - Last Post: 02 May 2012 - 08:12 AM Rate Topic: -----

#1 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

GetFileName using vb6

Posted 01 May 2012 - 01:12 PM

hi, i need a help in getting the file names in a given path i have tried the
following codes but i get an error message that says "fol.Files(i).name <Invalid procedure call or argument>"
the "name" "n" should be in capital case but when change to capital it automatically changes to small... any idea ?


Private Sub Form_Load()
    Dim fso As New FileSystemObject
    Dim fol As Folder
    Dim i As Integer
    Dim name1 As String
    
    Set fol = fso.GetFolder("C:\")
    For i = 0 To fol.Files.Count
        name1 = fol.Files(i).name
        Label1.Caption = name1
    Next i
    
End Sub




thanks advance... :)

Is This A Good Question/Topic? 0
  • +

Replies To: GetFileName using vb6

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: GetFileName using vb6

Posted 01 May 2012 - 01:36 PM

A better way of looping through a collection is by using a for each
    Dim aFile as Variant 

    For Each aFile In fol.Files
        name1 = aFile.Name
    Next


Was This Post Helpful? 1
  • +
  • -

#3 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: GetFileName using vb6

Posted 01 May 2012 - 02:36 PM

i belive this is VB.net code and not VB6
VB6 does not have FileSystemObject and you cant dim variables as folders.
as far as getting file name and path you could try the GetLongPathName function.

here is the declaration:

Private Declare Function GetLongPathName Lib "kernel32.dll" Alias "GetLongPathNameA" (ByVal lpszShortPath As String, _
    ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long



it could be used if your program have file extantion accosiated to it since when you'll try to open a file by double clicking its path will be translated to the old MSDOS version aka it will have ~1 in the path name as part of every folder on the path and as part of the file name.


also for your question: since you wrote
Dim name1 As String
with small 'n' you'll need to change it on that line to big 'N'

one last note is that on VB6 you have controls named DriveListBox, DirListbox and FileListbox.
you could try to use them to get folders on different drives and files inside the folders.
Was This Post Helpful? 0
  • +
  • -

#4 sanrooney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 25-April 11

Re: GetFileName using vb6

Posted 01 May 2012 - 02:37 PM

wow great man...

Thanks a lot...:)
Was This Post Helpful? 0
  • +
  • -

#5 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: GetFileName using vb6

Posted 01 May 2012 - 03:07 PM

Quote

i belive this is VB.net code and not VB6

At first glance, I said the same thing. But then I pasted the code into a VBA and after referencing Microsoft Runtime Scripting library, it worked without a problem. So, I must give to op the credit for it.

Quote

wow great man...

Thanks a lot...

Any time.

This post has been edited by Ionut: 01 May 2012 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 273
  • View blog
  • Posts: 1,635
  • Joined: 26-March 09

Re: GetFileName using vb6

Posted 01 May 2012 - 11:52 PM

Neku, as Ionut has said, the FileSystemObject is something that is available in VB6 through the MS Scripting Library.

It really is quite powerful and is something you should check out if you need to work with the file system.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: GetFileName using vb6

Posted 02 May 2012 - 06:10 AM

View PostNeku, on 01 May 2012 - 10:36 PM, said:

i belive this is VB.net code and not VB6
VB6 does not have FileSystemObject and you cant dim variables as folders.
as far as getting file name and path you could try the GetLongPathName function.

here is the declaration:

Private Declare Function GetLongPathName Lib "kernel32.dll" Alias "GetLongPathNameA" (ByVal lpszShortPath As String, _
    ByVal lpszLongPath As String, ByVal cchBuffer As Long) As Long



it could be used if your program have file extantion accosiated to it since when you'll try to open a file by double clicking its path will be translated to the old MSDOS version aka it will have ~1 in the path name as part of every folder on the path and as part of the file name.


also for your question: since you wrote
Dim name1 As String
with small 'n' you'll need to change it on that line to big 'N'

one last note is that on VB6 you have controls named DriveListBox, DirListbox and FileListbox.
you could try to use them to get folders on different drives and files inside the folders.

Sorry to correct you, Neku, but VB6 does indeed have the FileSystemObject, and you can indeed dim a variable as Folder. You need to have a reference to the "Microsoft Scripting Runtime" (scrrun.dll) set. None of this code implies VB.Net. Also, while it is a good idea to match the case in VB6 (or VB.Net, for that matter), neither language is case-sensitive.

The FileSystemObject is the standard means of manipulating files and folders in VB6. For doc, have a look at this.

This post has been edited by BobRodes: 02 May 2012 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: GetFileName using vb6

Posted 02 May 2012 - 07:05 AM

ok ok sowwy dont eat me xD
every day i learn new stuff :D

i guess with that i can also completaly change the subrutine i use for opening files right?

This post has been edited by Neku: 02 May 2012 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 550
  • View blog
  • Posts: 2,911
  • Joined: 19-May 09

Re: GetFileName using vb6

Posted 02 May 2012 - 08:12 AM

I see I was the third person to explain that, and steamed right on as if I were the first...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1