Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default loop thru folder looking for files with date code in name

I need to learn a new technique to use for my next activity.

In a macro I need to find all the files in a folder that contain a
string (specified by the user at the start of the macro), typically a
date code and open that file and then do some processing on it.

For instance:

If the folder C:\monthly_reports contains the following files:

Name_1 20090905.xls
Name_2 20090905.xls
Name_3 20090905.xls
Name_1 20090922.xls
Name_2 20090922.xls
Name_3 20090922.xls

and the user asks for reports that contain the string "20090922" in
the file name would open (one at a time) the last three files in the
above folder.

I suspect this is pretty simple but something I haven't done yet.


John Keith

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default loop thru folder looking for files with date code in name

index = 0
sData ="*20090905*"
sFilename = DIR("C:\monthly_reports\*.xls"
do until sFilename = ""
if sFilename LIKE sDate then
; do something
index = index +1
cells(index,"A")= sFilename
end if
sFilename = DIR()
loop

"John Keith" wrote:

I need to learn a new technique to use for my next activity.

In a macro I need to find all the files in a folder that contain a
string (specified by the user at the start of the macro), typically a
date code and open that file and then do some processing on it.

For instance:

If the folder C:\monthly_reports contains the following files:

Name_1 20090905.xls
Name_2 20090905.xls
Name_3 20090905.xls
Name_1 20090922.xls
Name_2 20090922.xls
Name_3 20090922.xls

and the user asks for reports that contain the string "20090922" in
the file name would open (one at a time) the last three files in the
above folder.

I suspect this is pretty simple but something I haven't done yet.


John Keith


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default loop thru folder looking for files with date code in name

On Thu, 24 Sep 2009 07:29:02 -0700, Patrick Molloy
wrote:

index = 0
sData ="*20090905*"
sFilename = DIR("C:\monthly_reports\*.xls"
do until sFilename = ""
if sFilename LIKE sDate then
; do something
index = index +1
cells(index,"A")= sFilename
end if
sFilename = DIR()
loop


Patrick,

Thank you! I'll try it shortly.

This group is fantastic!


John Keith

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default loop thru folder looking for files with date code in name

On Thu, 24 Sep 2009 07:29:02 -0700, Patrick Molloy
wrote:

index = 0
sData ="*20090905*"
sFilename = DIR("C:\monthly_reports\*.xls"
do until sFilename = ""
if sFilename LIKE sDate then
; do something
index = index +1
cells(index,"A")= sFilename
end if
sFilename = DIR()
loop


Patrick,

It worked great, thank you. You also alerted me the to function "LIKE"
which is new to me.

For the sake of anyone who wants to use this same technique there is a
typo, "sDate" should be "sData".


John Keith

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default loop thru folder looking for files with date code in name

sData ="*20090905*"
....
....
if sFilename LIKE sDate then


It worked great, thank you. You also alerted me the to function "LIKE"
which is new to me.


I also like the Like function<g... it is very flexible; but, I think in
this situation, I think I would use a simple InStr test instead, mainly
because InStr is a more efficient function than the Like operator (it
executes faster than the Like operator does which can be important in large
loops).

sData = "20090905"
.....
.....
If InStr(sFilename, sData) 0 Then

If you are not completely familiar with the InStr function, you should look
it up in the help files... there are optional arguments which allows you to
make the search case insensitive (which is not important in your current
needs, of course).

--
Rick (MVP - Excel)



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default loop thru folder looking for files with date code in name

On Thu, 24 Sep 2009 13:17:38 -0400, "Rick Rothstein"
wrote:

I also like the Like function<g... it is very flexible; but, I think in
this situation, I think I would use a simple InStr test instead, mainly
because InStr is a more efficient function than the Like operator (it
executes faster than the Like operator does which can be important in large
loops).


Hi Rick,

Thank you for the comments. I am familiar with the Instr fucntion, in
fact it seems to be in heavy use in my recent work.

My initial test using the LIKE function was doone with a folder that
only had 5 files so speed issues were not evident. The folder I'll use
this on is currently ~200 files and grows at a rate of about 15/month
so I'll get a chance to evaluate the speed soon.


John Keith

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default loop thru folder looking for files with date code in name

good spot re my typo - my keyboard does have a habit of doing that!

you could also use an IF statement

IF RIGHT(sFilename,12) = sData & ".xls" Then

worth testing large folders to see which is faster, this or the INSTR()


best regards

"John Keith" wrote:

On Thu, 24 Sep 2009 13:17:38 -0400, "Rick Rothstein"
wrote:

I also like the Like function<g... it is very flexible; but, I think in
this situation, I think I would use a simple InStr test instead, mainly
because InStr is a more efficient function than the Like operator (it
executes faster than the Like operator does which can be important in large
loops).


Hi Rick,

Thank you for the comments. I am familiar with the Instr fucntion, in
fact it seems to be in heavy use in my recent work.

My initial test using the LIKE function was doone with a folder that
only had 5 files so speed issues were not evident. The folder I'll use
this on is currently ~200 files and grows at a rate of about 15/month
so I'll get a chance to evaluate the speed soon.


John Keith


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 172
Default loop thru folder looking for files with date code in name

On Mon, 28 Sep 2009 08:18:02 -0700, Patrick Molloy
wrote:

good spot re my typo - my keyboard does have a habit of doing that!


My keyboard does that a LOT also.

you could also use an IF statement

IF RIGHT(sFilename,12) = sData & ".xls" Then

worth testing large folders to see which is faster, this or the INSTR()


Unfortunately the filenames are searching are more complicated than
you assumed.

Thanks for the tips.



John Keith

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
VBA Macro - Loop through folder and move files to other folders hurlbut777 Excel Programming 9 December 5th 08 05:30 PM
Code to open multiple files from within a folder Karen Excel Programming 0 September 17th 08 08:11 PM
Loop through files in a folder and unprotect sheets Diddy Excel Programming 13 August 15th 08 09:59 AM
Loop thru All Files in a Folder Juan Sanchez Excel Programming 3 July 5th 04 06:38 PM
Loop through all files in a folder Fred Smith Excel Programming 4 June 7th 04 12:30 AM


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

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"