arithmetic operations in batch file, using variables -SOLVED

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
microfot
Posts: 3
Joined: 10 Feb 2012 11:39
Location: Rome - Italy

arithmetic operations in batch file, using variables -SOLVED

#1 Post by microfot » 10 Feb 2012 12:05

Hi to everybody,
I need your help to solve a problem for which I could not find a solution.
By specifically:
I have a text file (list.txt) consists of n rows and associated name
It's generated by a batch file using the command findstr / N.

list.txt is like this:

1:PIPPO
2:PLUTO
3:PALLA
..
..
n:ZUZZO

but I need its start from zero 0 like this:

0:PIPPO
1:PLUTO
2:PALLA
..
..
n-1:ZUZZO

I wrote this batch, but doesn't work!

for /f "tokens=1,2* delims=:" %%a in (list.txt) do (
set wa=%%a
set /a tot=%­wa%-1
echo %­tot%:%%b>>new_list.txt
)

where is the mistake?
how can I fix it?
as optional solution is there a possibility to use findstr /n and start the line number from zero?

PLEASE HELP ME!!!
I stopped on this point from some months.
I don't see the light at the end of this tunnel.
Last edited by microfot on 10 Feb 2012 15:50, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: arithmetic operations in batch file, using variables

#2 Post by Squashman » 10 Feb 2012 12:59

You need to use Delayed Expansion with the code you are using.

microfot
Posts: 3
Joined: 10 Feb 2012 11:39
Location: Rome - Italy

Re: arithmetic operations in batch file, using variables

#3 Post by microfot » 10 Feb 2012 14:52

it doesn't work equally :cry:

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: arithmetic operations in batch file, using variables

#4 Post by Ed Dyreen » 10 Feb 2012 15:12

'
try delayed Expansion

Code: Select all

@echo off &setlocal enableDelayedExpansion

for /f "tokens=1,2* delims=:" %%a in (list.txt) do (
   set "wa=%%a"
   set /a tot=wa-1
   echo.!­tot!:%%b>>new_list.txt
)
type new_list.txt
pause

Code: Select all

@echo off
setlocal /?
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: arithmetic operations in batch file, using variables

#5 Post by Squashman » 10 Feb 2012 15:32

microfot wrote:it doesn't work equally :cry:

It does work when used correctly.

microfot
Posts: 3
Joined: 10 Feb 2012 11:39
Location: Rome - Italy

Re: arithmetic operations in batch file, using variables

#6 Post by microfot » 10 Feb 2012 15:47

dear Ed Dyreen
you are the nation saviour!
Thanks a lot :D
very long time trouble solved in few minutes!

Thanks also to Squashman
I had not understood the correct way to use your your help :oops:

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: arithmetic operations in batch file, using variables

#7 Post by Squashman » 10 Feb 2012 18:19

microfot wrote:Thanks also to Squashman
I had not understood the correct way to use your your help :oops:

I thought maybe you knew how to do it.
Ed showed you exactly what I was referring to.

Post Reply