Home All Groups Group Topic Archive Search About

List all jpg files from folder (loop problem)

Author
20 Sep 2005 9:42 AM
Robertico
I need to list all jpg files from a certain folder.
For that i use the following code:

    sFolder = SourcePath & "\*.jpg"
    sFile = Dir$(sFolder, vbNormal)

    Do While sFile <> ""
       Debug.Print sFile
       sFile = Dir$
    Loop

That works fine.

Now i'd like to use the variable sFile to get some properties from every
file.

    Do While sFile <> ""
       do something with the the variable sFile
       sFile = Dir$
    Loop

After that the loop won't work anymore. It only works for the first file.
What's going wrong ? (of course it's something simple, but i can't figure
out the problem)

Robertico

Author
20 Sep 2005 10:02 AM
Steve Barnett
I think, if you want people to offer any firm advice, you'll need to expand
upon what "do something with the variable sFile" actually means. What are
you doing? Can you post the code?

Steve


Show quoteHide quote
"Robertico" <Robertico@nomail.notvalid> wrote in message
news:dgorj9$s88$1@localhost.localdomain...
>I need to list all jpg files from a certain folder.
> For that i use the following code:
>
>    sFolder = SourcePath & "\*.jpg"
>    sFile = Dir$(sFolder, vbNormal)
>
>    Do While sFile <> ""
>       Debug.Print sFile
>       sFile = Dir$
>    Loop
>
> That works fine.
>
> Now i'd like to use the variable sFile to get some properties from every
> file.
>
>    Do While sFile <> ""
>       do something with the the variable sFile
>       sFile = Dir$
>    Loop
>
> After that the loop won't work anymore. It only works for the first file.
> What's going wrong ? (of course it's something simple, but i can't figure
> out the problem)
>
> Robertico
>
Author
20 Sep 2005 10:32 AM
Kevin J Prince
Old programmer, but I think I would have created a list of some kind and
..addItem for each then processed whatever else I wanted to do...

But as I say, I'm not very current with my programming techniques.

Regards

In message <dgorj9$s88$1@localhost.localdomain>, Robertico
<Robertico@nomail.notvalid> writes
Show quoteHide quote
>I need to list all jpg files from a certain folder.
>For that i use the following code:
>
>    sFolder = SourcePath & "\*.jpg"
>    sFile = Dir$(sFolder, vbNormal)
>
>    Do While sFile <> ""
>       Debug.Print sFile
>       sFile = Dir$
>    Loop
>
>That works fine.
>
>Now i'd like to use the variable sFile to get some properties from every
>file.
>
>    Do While sFile <> ""
>       do something with the the variable sFile
>       sFile = Dir$
>    Loop
>
>After that the loop won't work anymore. It only works for the first file.
>What's going wrong ? (of course it's something simple, but i can't figure
>out the problem)
>
>Robertico
>
>

--
Kevin J Prince
Skype address  princy557
Author
20 Sep 2005 1:21 PM
Bob Butler
"Robertico" <Robertico@nomail.notvalid> wrote in message
news:dgorj9$s88$1@localhost.localdomain
> Now i'd like to use the variable sFile to get some properties from
> every file.
>
>     Do While sFile <> ""
>        do something with the the variable sFile

I'll bet that that "something" uses the Dir$ function itself.  Doing that
will reset the Dir$ already in progress.  The classic case is using Dir$ to
check for a file's existence but any use will do it.

>        sFile = Dir$
>     Loop
>
> After that the loop won't work anymore. It only works for the first
> file. What's going wrong ? (of course it's something simple, but i
> can't figure out the problem)

Run your first loop to get all the jpg file names and add them to a
collection or array or whatever you like.  Then process that to do
"something" and you won't have to worry about this problem.  Either that or
switch to the FindFirstFile and related API calls to do the search.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
20 Sep 2005 1:28 PM
Martin
or switch to the FileSystemObject in the Scripting Library

Show quoteHide quote
"Bob Butler" <tiredofit@nospam.com> schreef in bericht
news:%23KNm3YevFHA.2064@TK2MSFTNGP09.phx.gbl...
> "Robertico" <Robertico@nomail.notvalid> wrote in message
> news:dgorj9$s88$1@localhost.localdomain
> > Now i'd like to use the variable sFile to get some properties from
> > every file.
> >
> >     Do While sFile <> ""
> >        do something with the the variable sFile
>
> I'll bet that that "something" uses the Dir$ function itself.  Doing that
> will reset the Dir$ already in progress.  The classic case is using Dir$
to
> check for a file's existence but any use will do it.
>
> >        sFile = Dir$
> >     Loop
> >
> > After that the loop won't work anymore. It only works for the first
> > file. What's going wrong ? (of course it's something simple, but i
> > can't figure out the problem)
>
> Run your first loop to get all the jpg file names and add them to a
> collection or array or whatever you like.  Then process that to do
> "something" and you won't have to worry about this problem.  Either that
or
> switch to the FindFirstFile and related API calls to do the search.
>
> --
> Reply to the group so all can participate
> VB.Net: "Fool me once..."
>
Author
20 Sep 2005 1:56 PM
Dave
Don't use the FSO unless you really want a slow program and nasty service
calls where system administrators have disabled scripting.

The FindFirst,FindNext,FindClose API not only give you the filename but its
size, all 3 dates and a few other odds and sods which may be handy, if dir
is giving a problem look at the API alternative, it's faster and not hard to
use (easier than the FSO anyway) and there are millions of examples out
there in netland.

Regards
Dave O.

Show quoteHide quote
"Martin" <ML@community.nospam> wrote in message
news:ubzErfevFHA.664@tk2msftngp13.phx.gbl...
> or switch to the FileSystemObject in the Scripting Library
>

>>   Either that or
>> switch to the FindFirstFile and related API calls to do the search.
>>
Author
20 Sep 2005 2:26 PM
Mike Williams
"Martin" <ML@community.nospam> wrote in message news:ubzErfevFHA.664@tk2msftngp13.phx.gbl...

> or switch to the FileSystemObject in the Scripting Library

Yuck! I feel sick already ;-)

Mike
Author
20 Sep 2005 1:51 PM
Robertico
Thanks.

found some intersting code on : http://vbnet.mvps.org/index.html

Robertico
Author
20 Sep 2005 6:19 PM
DRBarkley
Show quote Hide quote
"Robertico" <Robertico@nomail.notvalid> wrote in message
news:dgorj9$s88$1@localhost.localdomain...
> I need to list all jpg files from a certain folder.
> For that i use the following code:
>
>     sFolder = SourcePath & "\*.jpg"
>     sFile = Dir$(sFolder, vbNormal)
>
>     Do While sFile <> ""
>        Debug.Print sFile
>        sFile = Dir$
>     Loop
>
> That works fine.
>
> Now i'd like to use the variable sFile to get some properties from every
> file.
>
>     Do While sFile <> ""
>        do something with the the variable sFile
>        sFile = Dir$
>     Loop
>
> After that the loop won't work anymore. It only works for the first file.
> What's going wrong ? (of course it's something simple, but i can't figure
> out the problem)
>
> Robertico
>
>
Hi Robertico,

I'm not sure exactly what you mean by the First File.... Perhaps you meant
the first Do/While Loop.

Your problem is that you need to call the Dir Function WITH parameters,
again, before you start your second loop.

Your first loop calls the Dir Function UNTIL it returns nothing.

You are continuing after it has exhausted itself of items, and you have to
"re-seed" it as you did originally, with "sFile = Dir$(sFolder, vbNormal)".

HTH,

DRBarkley