Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to loop non-recursive over all files in a directory?

707 views
Skip to first unread message

Andrew Chambers

unread,
Dec 2, 2008, 1:35:35 PM12/2/08
to
I though I could loop (non-recursive) in a bacth script over all files in a certain directory with:

for %%a in (%1\*.jpg) do (
Echo Next picture=%%a
....more commands....)

but this doesn't work.

for /F %%a in (%1\*.jpg) do (
Echo Next picture=%%a
....more commands....)

does not work as well. The output is always
Next picture=%a

How do I iterate otherwise?
BTW: %1 is valid directory

Andrew

foxidrive

unread,
Dec 2, 2008, 1:53:29 PM12/2/08
to
On 02 Dec 2008 18:35:35 GMT, bc...@cableone.com (Andrew Chambers) wrote:

>I though I could loop (non-recursive) in a bacth script over all files in a certain directory with:
>

>for %%a in (%1\*.jpg) do Echo Next picture=%%a


>
>but this doesn't work.

What error do you get?

This syntax should work as an alternative.

@echo off
for /F "delims=" %%a in ('dir "%~1\*.jpg" /b') do (
Echo Next picture=%%a
)


Tom Lavedas

unread,
Dec 2, 2008, 2:01:56 PM12/2/08
to

Your first formulation DOES work - unless there is something else
wrong. For example, what is the exact content of %1? Is it created
by drag-and-drop onto the batch or a shortcut to the batch in explorer
or is it keyed from the command line. If it is the latter, does it
have an embedded space or other delimiter?

I tested this with long folders with DandD and it worked just fine ...

@echo off
for %%a in ("%~1\*.jpg") do (
echo Next picture=%%a)
pause

I altered the handling of the surrounding double quotes a little, but
otherwise it's pretty much the same as your first example.

If you are changing the names or moving the the files as part of your
"....more commands....", then you should process an intermediate list,
instead, something like this ...

for /f "delims=" %%a in ('dir /b "%~1\*.jpg"') do (
echo Next picture=%~1\%%a)

If you are setting an intermediate variable with the name and then
trying to use it later in the "....more commands...." part, you will
either need to enable delayed expansion and replace the percent signs
with exclamation points or call a subroutine instead, something
like ...

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /b "%~1\*.jpg"') do (
set "NextPicture=%~1\%%a"
echo Next picture=!NextPicture!)

or

for /f "delims=" %%a in ('dir /b "%~1\*.jpg"') do (
call :sub "%~1\%%a")
goto :EOF

:sub
set "NextPicture=%1"
echo Next picture=%NextPicture%

HTH,

Tom Lavedas
***********
http://there.is.no.more/tglbatch/

billious

unread,
Dec 2, 2008, 4:36:42 PM12/2/08
to

"Andrew Chambers" <bc...@cableone.com> wrote in message
news:49357ff7$0$30233$9b4e...@newsspool1.arcor-online.net...

Works perfectly here (XP/SP3)

The first routine is correct. /F is used to operate on the each line of
output-generated.

Please cut-and-paste the relevant logical line exactly as you have
constructed it, specify your OS and specify the exact format of the
parameter you are using.

At a guess, you may be trying to specify a parameter that contains spaces
without "enclosing the parameter in quotes" on the command-line - or perhaps
the parameter includes "poison characters" (those with a special meaning to
the command-interpreter.)

You might also try using
Echo next picture=%%~fa

to deliver the full filename for each .jpg when you use a relative path
(parameter of

.\subdir

for instance)

although this is an enhancement - nothing to do with the problem you are
encountering


Batchman

unread,
Dec 8, 2008, 6:35:22 PM12/8/08
to
Hi Andrew,

On Tue, 02 Dec 2008 18:35:35 +0000, Andrew Chambers wrote:

> I though I could loop (non-recursive) in a bacth script over all files in a
>

> for %%a in (%1\*.jpg) do (
> Echo Next picture=%%a
> ....more commands....)
>
> but this doesn't work.

> SNIP

You'll need to create a separate routine to perform the require task(s) on
each file thus...

@if not (%2==!!) goto Begin
echo Next picture is %1
Quit
:Begin
@echo off
REM>Quit.Bat
for %%f in (*.jpg) do CALL %0 %%f !!

In the above example line 1 tests for the %1 parameter being `!!' and when
it is not, passes control to the `Begin' label.
The For/Do loop CALLs the same batch file (via `%0'), passing the required `!!'
parameter to allow line 2 (and any other lines) to be accessed.

NOTE the batch file above uses `REM>Quit.Bat' to create a zero length
batch file to allow for quick termination after each CALL. You may need to
use some other command depending on your Win version.

`ECHO.>Quit.Bat' does almost the same thing.

BatchMan!


Batchman

unread,
Dec 9, 2008, 11:34:55 PM12/9/08
to
Hi Andrew,

On Tue, 02 Dec 2008 18:35:35 +0000, Andrew Chambers wrote:

> I though I could loop (non-recursive) in a bacth script over all files
>

OOps, there is a problem with that sample batch I sent you.

#1 Line 1 should read...

@if not (%2)==(!!) goto Begin

...to test for the %2 parameter being passed by the CALL.

BatchMan

0 new messages