LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 98
Default Looping through folders and using the first file in each folder

Hello all,

How do I loop through folders and get the first file from each folder?

I have code that will loop through a set of folders, gets the column labels out of the first file in each folder and lists them in
the current workbook. The problem is that I'm getting the first file in each folder by using it's file name. If the files in each
folder are named the same, then this won't work correctly.

Isn't there a way to get the first file without having to use the file name?

Here is the code:


Sub List_FileName_ColumnHeaders()
Dim fso As New FileSystemObject
Dim fsoFolder As Folder
Dim fsoSubFolder As Folder
Dim fsoFile As File

Dim pstrCol1 As String
...
Dim pstrCol16 As String

pstrCol1 = ""
...
pstrCol16 = ""

Set fsoFolder = fso.GetFolder("X:\Some Folder\Data Files")

For Each fsoSubFolder In fsoFolder.SubFolders


Set fsoFile = fsoSubFolder.Files("2003-08_Aug 2003.csv")
'***This is where I'm having problems. ***'
'***"Set fsoFile = fsoSubFolder.Files(1)" does not work ***'
'***When I use the "...fsoSubFolder.Files(1)", I get an ***'
'***"Invalid procedure call or argument".


ActiveCell = fsoFile.Name

Open fsoFile For Input As #1

Input #1, pstrCol1, ... , pstrCol16

Cells(ActiveCell.Row, ActiveCell.Column + 1) = pstrCol1
...
Cells(ActiveCell.Row, ActiveCell.Column + 16) = pstrCol16

Close #1

Cells(ActiveCell.Row + 1, ActiveCell.Column).Select

Next fsoSubFolder



End Sub



--
Thanks for any help anyone can provide,

Conan Kelly


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula to go into sub folders of main folder and get values from BadBoy Excel Worksheet Functions 0 January 15th 10 10:55 PM
Hyperlink to folder only brings up Explorer's Folders view Bob W Excel Discussion (Misc queries) 0 January 21st 08 02:49 PM
Save file in a new folder, but create folder only if folder doesn't already exist? nbaj2k[_40_] Excel Programming 6 August 11th 06 08:41 PM
How to decide folder-depth or How to select more folders/subfolders (folder-tree) ? Subteam Excel Discussion (Misc queries) 2 May 7th 06 08:14 PM
delete all the contents (sub folders and files) in the temp folder Joseph Excel Discussion (Misc queries) 0 June 6th 05 08:01 AM


All times are GMT +1. The time now is 10:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"