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

for do if

80 views
Skip to first unread message

j4e8a16n

unread,
Aug 11, 2011, 6:14:02 AM8/11/11
to
Hi everybody,

I have many images in a folder and I want to delete 4 files every
five files . That is to say keeping only one fift of the images
files.


set a=0
for /r %i in (*.png) do if %a% LSS 5 (set /a a+=1 & del "%i" ) else
(set /a a=0)

The variable a does not seem to be set to zero has if %a% keeps
increasing

Thanks for yur attention,

J

billious

unread,
Aug 11, 2011, 1:12:41 PM8/11/11
to

"j4e8a16n" <davi...@videotron.ca> wrote in message
news:a532cd82-ebb6-42c4...@b34g2000yqi.googlegroups.com...

Commonly-encountered quirk. Try alt.msdos.batch.nt for extended discussions
over many years.

Batch parses the ENTIRE logical FOR statement (from the FOR to the closing
parenthesis), SUBSTITUTES THE CURRENT VALUE OF ANY %var%, and then executes
the statement. Hence, since %a% has a value of 0 when the FOR is parsed, the
line executed is:

for /r %i in (*.png) do if 0 LSS 5 (set /a a+=1 & del "%i" ) else
(set /a a=0)

There are two common solutions: (I've converted the syntax to batchfile
syntax - I'm sure you don't really want to keep retyping the command...)

1/

SETLOCAL ENABLEDELAYEDEXPANSION
set a=0
for /r %%i in (*.png) do if !a! LSS 5 (set /a a+=1 & del "%%i"
) else (
set /a a=0)


* Here, !a! is the dynamic value of A - that is, as it is changed by the
loop. This syntax
is invoked by the SETLOCAL ENABLEDELAYEDEXPANSION
* Note that reaching EOF is an implicit ENDLOCAL, which restores the
original environment

2/

set a=0
for /r %%i in (*.png) do call :sub "%%i"
goto :eof
:sub
if %a% LSS 5 (set /a a+=1 & del %1
) else (
set a=0)
goto :eof

Where :sub is an internal routine to the batchfile
the syntax CALL :SUB ... (with the colon, which specifies an internal
subroutine;
no colon indicates an external batch file)
the label :eof is predefined by the processor as physical end-of-file
(and should not be defined by the programmer)
%1 within :sub refers to parameter #1 to the subroutine, not #1 to the batch
file

0 new messages