Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > files in folder


Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2003, 04:06 PM
DavidPinzon
Guest
 
Posts: n/a
Default files in folder


How do I get all the file names in a folder?
I need to create an outbox folder and get all the files inside that folder to further processing.

thanks

another quick question, what function do I use to move a FileName from a folder to another one

thanks
Reply With Quote
  #2  
Old 03-31-2003, 04:39 PM
dgdolins1 dgdolins1 is offline
Centurion
 
Join Date: Dec 2002
Posts: 124
Default

Try something like this:

Code:
Private Sub Command1_Click() Dim Files As String Files = Dir("c:\") While Files <> "" If Files <> "" Then MsgBox Files Files = Dir End If Wend End Sub

Use FileCopy to move/copy a file to another folder, then delete it (Kill) from the current folder if you need to.
Reply With Quote
  #3  
Old 03-31-2003, 04:44 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

You can also use the Name statement to move a file, that way you don't need to use Kill.
Reply With Quote
  #4  
Old 03-31-2003, 05:04 PM
strBean's Avatar
strBean strBean is offline
Junior Contributor
 
Join Date: May 2002
Location: Rural Northwest USA
Posts: 315
Default

Was going to post a quick reply, but got bogged down experimenting. This function uses Dir() to fill an array with filenames. It's clumsy, but you get the idea:
Code:
Function GetAllFilesInFolder(strPathToFolder As String, _
strFileNamesArray() As String) As Boolean
On Error GoTo Whoops
    Dim x As Integer, y As Integer, strFileName As String
    ReDim strFileNamesArray(0 To 99)
    strFileNamesArray(0) = Dir(strPathToFolder)
    x = 1
    strFileName = Dir
    Do While strFileName <> ""
        If strFileName <> "" Then
            strFileNamesArray(x) = strFileName
            x = x + 1
        strFileName = Dir
        Else: Exit Do
        End If
    Loop
    ReDim Preserve strFileNamesArray(0 To x)
    GetAllFilesInFolder = True
    For y = 0 To x   'strip this out once you get it working
        Debug.Print strFileNamesArray(y)
    Next
OffRamp:
    Exit Function
Whoops:
    GetAllFilesInFolder = False
End Function
You need to Dim a string array in the code that calls this, and use it for the 2nd argument.
Reply With Quote
  #5  
Old 03-31-2003, 05:04 PM
DavidPinzon
Guest
 
Posts: n/a
Default

Thanks a lot dgdolins1, that routine worked as expected.

Deadalus, how do I use the Name statement?

What I plan to do is take the files from an 'outbox' folder and place all those files in a 'sent' folder

I thought about using the FileCopy and Kill functions as suggested by dgdolins, but if there is a more efficient way of doing it the better.

Thanks guys
Reply With Quote
  #6  
Old 03-31-2003, 05:11 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,402
Default

Name Oldname as Newname

I don't believe it works across drive boundries, though.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #7  
Old 03-31-2003, 06:02 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

I've heard that before about the Name statement, but MSDN says this about it
Quote:
Name can move a file across drives, but it can only rename an existing directory or folder when both newpathname and oldpathname are located on the same drive. Name cannot create a new file, directory, or folder.
In this part of strBean's code
Code:
Do While strFileName <> "" If strFileName <> "" Then strFileNamesArray(x) = strFileName x = x + 1 strFileName = Dir Else: Exit Do End If Loop
only this is functional:
Code:
Do While strFileName <> "" strFileNamesArray(x) = strFileName x = x + 1 strFileName = Dir Loop
The if condition was just repeating the While condition. Also, the label OffRamp doesn't seem to be used and you might want to add code in the loop to redimension the array when x reaches the ubound of the array (over 100 files).
Reply With Quote
  #8  
Old 03-31-2003, 06:07 PM
strBean's Avatar
strBean strBean is offline
Junior Contributor
 
Join Date: May 2002
Location: Rural Northwest USA
Posts: 315
Default

Quote:
Originally Posted by Deadalus

only this is functional:
Code:
Do While strFileName <> "" strFileNamesArray(x) = strFileName x = x + 1 strFileName = Dir Loop



I know, I saw that. And I saw where the ubound could be a problem. But you guys are so fast I had to hurry up and post.
Reply With Quote
  #9  
Old 03-31-2003, 06:26 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

Hey, it's not a race. In the end things get solved quicker (and you learn more) if you take your time to test and review the code. Plus you get to correct hurried posters.
Reply With Quote
  #10  
Old 04-01-2003, 06:36 AM
DavidPinzon
Guest
 
Posts: n/a
Default

Thanks guys, this forum is the best!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
copy files from folder to folder sirbum69 General 32 11-29-2002 12:29 PM
copying files to program files folder during setup mattrade Installation / Documentation 3 09-20-2002 10:18 AM
Delete a folder and files within it lamar_air General 4 06-07-2002 09:17 AM
Required files burningodzilla General 11 09-12-2001 04:51 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->