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

Increment a number in a file name

1,166 views
Skip to first unread message

Carsten Bartels

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to
Hello!

I must write a little batch file wich download a picture from the web
every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...

I've got the programm wget to get the picture from the URL.

I don't know how to write a loop in a batchfile with a variable wich
will be incremented and written into the output-filename of wget.

please help.

thanks in advance!

Georg Pohl

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to
Carsten Bartels schrieb in Nachricht
<39ABB7E8...@vt.siemens.de>...


I can give only a hint. Try somthing like this:

set nmbr=1
if not exist nmbr.log goto loop
type nmbr.log | env_str nmbr /inc 1

:loop
if %nmbr%==100 goto end
echo %nbr%
wget ftp.picture pic%nmbr%.jpg
env_str nmbr /inc 1
goto loop
:end
echo %nmbr% > nmbr.log
set nbr=

env_str is a small tool to modify environment variables. the "/inc 1"
switch increments the variable nmbr. In connection with the type
statement and a pipe the variable can be initialized.

env_str is freeware anc can be downloaded from my website.

HTH
Georg Pohl

--
Email: Pohl...@compuserve.de
Web: http://www.online-club.de/~Eulenspiegel
ICQ: 78600280


Ted Davis

unread,
Aug 29, 2000, 3:00:00 AM8/29/00
to
On Tue, 29 Aug 2000 15:17:28 +0200, Carsten Bartels
<carsten...@gmx.de> wrote:

>Hello!
>
>I must write a little batch file wich download a picture from the web
>every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...
>
>I've got the programm wget to get the picture from the URL.
>
>I don't know how to write a loop in a batchfile with a variable wich
>will be incremented and written into the output-filename of wget.
>

>please help.

This depends entirely on the operating system in use. You posted from
NT, so I'll assume that is the one you want the code for.

set number=0
:loop
set /a %number%+1
START your command(s) here - the filename is "pic%number%.jpg"
delay program here
goto loop

T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Christoph Basedau

unread,
Aug 29, 2000, 6:52:04 PM8/29/00
to

Carsten Bartels <carsten...@gmx.de> schrieb in im Newsbeitrag:
39ABB7E8...@vt.siemens.de...

> Hello!
>
> I must write a little batch file wich download a picture from the web
> every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...
>
> I've got the programm wget to get the picture from the URL.
>
> I don't know how to write a loop in a batchfile with a variable wich
> will be incremented and written into the output-filename of wget.
>
> please help.
>
> thanks in advance!

Hello Carsten

This is a pure DOS approach
I tested it in Win9x.
should work in NT/2000 to.

Creates Files pic00000.pic to pic99999.pic

CODE:
picount.bat

@echo off
set z=c:\archiv
set y=c:\temp.pic
set a=if not
set b=DO %a% exist
set c=for %%K IN
set d=%c% (9 8 7 6 5 4 3 2 1 0) %b%
set e=%c% (0 1 2 3 4 5 6 7 8 9) %b%
c:\
%a% exist %z%\*.* md %z%>nul
cd %z%
set f=set c0
%d% "pic%%K9999.pic" set z0=%%K
%f%=%z0%
%d% "pic%c0%%%K999.pic" set z0=%c0%%%K
%f%=%z0%
%d% "pic%c0%%%K99.pic" set z0=%c0%%%K
%f%=%z0%
%d% "pic%c0%%%K9.pic" set z0=%c0%%%K
%e% "pic%z0%%%K.pic" call makepic.bat %y% %%K
%c% (f e d b a c0 y z) DO set %%K=
set c=

remarks:
the wget-file is "c:\temp.pic"
the pic-archive is "c:\archiv".
change these values with set z/y=
parms a to f are just cosmetic for less coding.
parms c0 and z0 do the counting
a-f and c0+z0 should not be changed.
parms z+y define path and name of the pic-archive(z)
and the temorary pic (y).
Due to the many parms environment space shuold be
increased in config.sys
Both batches must be in a %PATH%-folder.

counter checks file with "9"at the end.
none of these files should be removed later on.

------------------------------------------
CODE
makepic.bat (called by picount.bat)

@echo off
if %z0%)==) goto end
copy %1 "pic%z0%%2.pic" > nul
echo File %z%\"pic%z0%%2.pic" was copied!
set z0=
:end


-------------------------------------------


regards
Christoph

Carsten Bartels

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to

Ted Davis schrieb:


>
> On Tue, 29 Aug 2000 15:17:28 +0200, Carsten Bartels
> <carsten...@gmx.de> wrote:
>

> >Hello!
> >
> >I must write a little batch file wich download a picture from the web
> >every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...
> >
> >I've got the programm wget to get the picture from the URL.
> >
> >I don't know how to write a loop in a batchfile with a variable wich
> >will be incremented and written into the output-filename of wget.
> >
> >please help.
>

> This depends entirely on the operating system in use. You posted from
> NT, so I'll assume that is the one you want the code for.
>
> set number=0
> :loop
> set /a %number%+1
> START your command(s) here - the filename is "pic%number%.jpg"
> delay program here
> goto loop

Thank you very much. (And to all other, too!)
It works! (but it must be "set /a number=%number%+1")

BTW: The code was for NT.

Mit freundlichem Gruss
Carsten Bartels
(Germany)

Carsten Bartels

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to

Ted Davis schrieb:
>
> On Tue, 29 Aug 2000 15:17:28 +0200, Carsten Bartels
> <carsten...@gmx.de> wrote:
>
> >Hello!
> >
> >I must write a little batch file wich download a picture from the web
> >every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...
> >
> >I've got the programm wget to get the picture from the URL.
> >
> >I don't know how to write a loop in a batchfile with a variable wich
> >will be incremented and written into the output-filename of wget.
> >
> >please help.
>
> This depends entirely on the operating system in use. You posted from
> NT, so I'll assume that is the one you want the code for.
>
> set number=0
> :loop
> set /a %number%+1
> START your command(s) here - the filename is "pic%number%.jpg"
> delay program here
> goto loop

Oh, sorry, it's me.

Now i've got the problem that the pictures are sorted like this:

pic1.jpg pic10.jpg pic11.jpg ... pic2.jpg pic20.jpg ...

is it possible to change the script to get file-names like this?:

pic00001.jpg pic00002.jpg ...

this doesn't work: set number=00000 ... set /a number=%number%+00001

BTW: I work with WinNT

Thanks.

Todd Vargo

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
Christoph Basedau <christop...@gmx.de> wrote in message
news:8oheoe$msk$18$1...@news.t-online.com...

This only counts 0 to 9 for me.
Does it count past 9 for anyone else?

========================================

Hey Carsten, try this batch counter too.
I didn't include an environment test, but that's easily done.
Also, make sure you have CHOICE in your path!

@echo off % Tested in Windows 98 %
if %Done%'==Yes' goto end
if !!!!!==%1 goto CopyFile
if '%1==' for %%? in (count done) do set %%?=
if '%1==' echo Press any key to pause, [C] to continue, or [Q] to quit.
for %%? in (0 1 2 3 4 5 6 7 8 9) do call %0 %1! %2%%?
if '%1==' set Done=
goto end

:CopyFile
set Count=%Count%!
::Change the "1" below to 60 for a 5 min. delay.
choice /c~cq /t~,1 >nul
if errorlevel 3 set Done=Yes
if errorlevel 3 goto end
if not !!!!!'==%count%' goto CopyFile
::Do whatever here.
echo "copy PicFile.jpg Pic%2.jpg"

:end
set Count=

--
Todd Vargo (body of msg must contain my name to reply)


Ted Davis

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to
On Wed, 30 Aug 2000 10:38:27 +0200, Carsten Bartels
<carsten...@vt.siemens.de> wrote:

>
>
>Ted Davis schrieb:
>>
>> On Tue, 29 Aug 2000 15:17:28 +0200, Carsten Bartels
>> <carsten...@gmx.de> wrote:
>>

>> >Hello!
>> >
>> >I must write a little batch file wich download a picture from the web
>> >every 5 minutes and save it like this: pic00001.jpg, pic00002.jpg, ...
>> >
>> >I've got the programm wget to get the picture from the URL.
>> >
>> >I don't know how to write a loop in a batchfile with a variable wich
>> >will be incremented and written into the output-filename of wget.
>> >
>> >please help.
>>

>> This depends entirely on the operating system in use. You posted from
>> NT, so I'll assume that is the one you want the code for.
>>
>> set number=0
>> :loop
>> set /a %number%+1
>> START your command(s) here - the filename is "pic%number%.jpg"
>> delay program here
>> goto loop
>
>Oh, sorry, it's me.
>
>Now i've got the problem that the pictures are sorted like this:
>
>pic1.jpg pic10.jpg pic11.jpg ... pic2.jpg pic20.jpg ...
>
>is it possible to change the script to get file-names like this?:
>
>pic00001.jpg pic00002.jpg ...
>
>this doesn't work: set number=00000 ... set /a number=%number%+00001
>
>BTW: I work with WinNT

When it's necessary to format that precisely, I generally switch to a
language that has a printf() function (C, AWK, Perl, etc.), at least
for the part that generates the string. See:
<http://gearbox.maem.umr.edu/~batch/multilingual.html> and following
pages for somewhat related examples.

I have also done leading zero counters in pure batch, and some of the
concepts may be useful, though I have not even looked at porting the
code to NT specific batch language:
<http://gearbox.maem.umr.edu/~batch/intrin1.htm#count>.

Christoph Basedau

unread,
Aug 30, 2000, 3:00:00 AM8/30/00
to

> This only counts 0 to 9 for me.
> Does it count past 9 for anyone else?
>
> ========================================

> :end


> set Count=
>
> --
> Todd Vargo (body of msg must contain my name to reply)
>

I am sorry Mr. Vargo, but you are utterly wrong.
My aprroach works for numbers 00000 to 99999,
On Win 9x and DOS-systems.
with exactly the code I posted and the condition
I stated.


Regards
Christoph Basedau

Todd Vargo

unread,
Aug 31, 2000, 7:24:30 AM8/31/00
to
Christoph Basedau <christop...@gmx.de> wrote in message
news:8ojb48$7hl$13$1...@news.t-online.com...

Maybe I missed something, I'll try it again.

Helge Wunderlich

unread,
Sep 1, 2000, 3:56:00 PM9/1/00
to
Carsten Bartels <carsten...@vt.siemens.de> wrote:

>is it possible to change the script to get file-names like this?:
>
>pic00001.jpg pic00002.jpg ...
>
>this doesn't work: set number=00000 ... set /a number=%number%+00001
>
>BTW: I work with WinNT

Here is a batch routine that will patch the variable %number% with
leading zeroes until the string is five characters long. It's slow,
but since you only need to run it every five minutes, it is probably
useful:

set number=%number%X
:next
set check=%number:~5,1%
if "%check%"=="X" goto done
set number=0%number%
goto next
:done
set number=%number:~0,5%
set check=

--
Helge Wunderlich

Please delete obvious part of email address to send email.

0 new messages