• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Import all files in a directory in Visual Basic

I have a program that imports data from a text file.  I am able to import the data from the specific file by specifying the exact path.  I would like to know how to import each file in the directory regardless of the name of the file.  After the file has been imported I would like it deleted.  

Directory to import from is c:\importfolder

Thank You
0
dupont2406
Asked:
dupont2406
  • 5
1 Solution
 
[ fanpages ]IT Services ConsultantCommented:
Here's one method...

Dim strFile As String

strFile = Dir$("c:\yourfolder\")

While Len(Trim$(strFile)) > 0

    ' Import your file here based on the filename being in the string variable strFile

    ' YOUR CODE HERE

    ' After processing, delete the specific file & see if there are any more...

    Kill strFile

    strFile = Dir$()

Wend



BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
Sorry, for:

strFile = Dir$("c:\yourfolder\")

Read:

strFile = Dir$("c:\importfolder\")
0
 
[ fanpages ]IT Services ConsultantCommented:
Apologies again,

Replace:
strFile = Dir$("c:\yourfolder\")

With:
strFile = Dir$("c:\importfolder\*.*")


(It's 3:28am local time, btw!)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
[ fanpages ]IT Services ConsultantCommented:
Another method...

  Dim objFileSystemObject                               As Object
  Dim objFSO_Folder                                     As Object
  Dim objFSO_Folder_File                                As Object
 
  Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
 
  If Not (objFileSystemObject Is Nothing) Then
     Set objFSO_Folder = objFileSystemObject.GetFolder("c:\importfolder")
  End If
 
  If Not (objFSO_Folder Is Nothing) Then
     For Each objFSO_Folder_File In objFSO_Folder.Files
         ' Place your code here to import the file contained in objFSO_Folder_File.Name
         ' Then...
         objFileSystemObject.DeleteFile(objFSO_Folder_File.Name)
     Next objFSO_Folder_File
 End If

  Set objFSO_Folder_File = Nothing
  Set objFSO_Folder = Nothing
  Set objFileSystemObject = Nothing



BFN,

fp.
0
 
dupont2406Author Commented:
That works

Thank You
0
 
[ fanpages ]IT Services ConsultantCommented:
You're welcome.

Thanks for closing the question so promptly.

BFN,

fp.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now