Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Looping through folders and using the first file in each folder


Conan,
Couple of items...
Windows will not allow multiple files with the same name
in the same folder. If you know the file name why is there a problem?

How do you define the first file? The files could be sorted in any order.
Are you looking for the first file in an alpha sorted list?
Are you looking for the earliest file date or ?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Conan Kelly" <CTBarbarin at msn dot com
wrote in message
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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 137
Default Looping through folders and using the first file in each folder

Conan,

Are you wanting to open the OLDEST file in the folder? Is that what
you mean by the FIRST? Or is it the first ALPHABETICALLY?

You can get the OLDEST like this ...
From: http://j-walk.com/ss/excel/tips/tip97.htm

Option Base 1
Function OldestFile(Directory, FileSpec)
' Returns the full path and name of the most recent file in a Directory
' That matches the FileSpec (e.g., "*.xls").
' Returns an empty string if the directory does not exist or
' it contains no matching files
Dim NumFound As Long
NewestFile = ""
With Application.FileSearch
.NewSearch
.LookIn = Directory
.FileName = FileSpec
NumFound = .Execute(SortBy:=msoSortByLastModified, _
SortOrder:=msoSortOrderAscending)
If NumFound 0 Then OldestFile = .FoundFiles(1)
End With
End Function

And call the function: MyOldFile = OldestFile("C:\","*.*")


Jim Cone wrote:
Conan,
Couple of items...
Windows will not allow multiple files with the same name
in the same folder. If you know the file name why is there a problem?

How do you define the first file? The files could be sorted in any order.
Are you looking for the first file in an alpha sorted list?
Are you looking for the earliest file date or ?
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Conan Kelly" <CTBarbarin at msn dot com
wrote in message
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


Reply
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 07:45 AM.

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

About Us

"It's about Microsoft Excel"