Solved Batch file adds more and more white spaces each run

August 11, 2013 at 18:39:09
Specs: Windows 7
I have been trying to create a simple day counter in batch to keep track how long people have been in a guild of mine.

Each time i run the batch, i want it to add another day to the users total day(s). I also want to be able to enter another user to the list and start them at 0 days.

The problem is each time i run it, it adds more and more white spaces between the numbers and days if they are 2 digits. I'm trying to keep the numbers and days 1 space separated from each other (if that's possible).

I want it like this each time it runs, just adding a day onto the total.

3 days Accountname[8231]
3 days Accountname[4857]
4 days Accountname[6493]
4 days Accountname[1534]
9 days Accountname[6584]
9 days Accountname[5620]
11 days Accountname[2469]
12 days Accountname[2469]

But it ends up like this and gets worse each run.

3 days Accountname[8231]
3 days Accountname[4857]
4 days Accountname[6493]
4 days Accountname[1534]
9 days Accountname[6584]
9 days Accountname[5620]
11  days Accountname[2469]
12   days Accountname[2469]

Here is my batch code:

@echo on
setlocal EnableDelayedExpansion
color 0f

set /p wantname=Do you want to add a name? (Y/N)

if /i {%wantname%}=={y} (goto :yes)
if /i {%wantname%}=={n} (goto :next)
if /i {%wantname%}=={Y} (goto :yes)
if /i {%wantname%}=={N} (goto :next)

:yes
set /p name=Enter a name:
echo 0 days %name%>>list.txt
goto :next

:next
set /p add=Do you want to add a day to everyone? (Y/N)
if /i {%add%}=={y} (goto :yess)
if /i {%add%}=={n} (goto :list)
if /i {%add%}=={Y} (goto :yess)
if /i {%add%}=={N} (goto :list)

:yess
for /F "tokens=*" %%a in (list.txt) do (
set originaltotal=%%a
set /a addone=1+originaltotal
echo !addone!>>templist.txt
)

setLocal EnableDelayedExpansion > templist2.txt
for /f "usebackq tokens=* delims= " %%b in (list.txt) do (
set x=%%b
>>templist2.txt echo.!x:~2!
)

set i=0
for /f "delims=" %%c in (templist.txt) do (
set /a i+=1
set A1[!i!]=%%c
)

set i=0
for /f "delims=" %%d in (templist2.txt) do (
set /a i+=1
set A2[!i!]=%%d
)

del list.txt
for /l %%i in (1,1,%i%) do (
echo !A1[%%i]! !A2[%%i]!>>list.txt
)

del templist.txt
del templist2.txt

:list
color 0a
cls
type list.txt
pause

Sorry for my messy code :x


See More: Batch file adds more and more white spaces each run

Report •


#1
August 11, 2013 at 21:29:52
✔ Best Answer
@echo off>templist.txt & setlocal EnableDelayedExpansion

set /p wantname=Do you want to add a name? (Y/N)

::Note: IF with "/i" is exactly designed to eliminate testing upper/lower case
if /i {%wantname%}=={y} (goto :yes)
if /i {%wantname%}=={n} (goto :next)

:yes
set /p name=Enter a name:
echo 0 days %name%>>list.txt
goto :next

:next
set /p add=Do you want to add a day to everyone? (Y/N)
if /i {%add%}=={y} (goto :yess)
if /i {%add%}=={n} (goto :list)

:yess
for /F "tokens=1*" %%a in (list.txt) do (
set /a originaltotal=%%a+1
echo !originaltotal! %%b>>templist.txt
)
copy /y templist.txt list.txt


::===== from here on your code is kind of wierd. I'll just iterate my interp.

set i=0
for /f "tokens=1*" %%a in (list.txt) do (
set /a i+=1
set A1[!i!]=%%a
set A2[!i!]=%%b
)
::----- display captured var.s and their values
for /L %%a in (1 1 %i%) do (
echo !A1[%%a]! = !A2[%%a]!
)
pause
::-------- display file
more list.txt
pause
goto :eof

::===== now from here the rest is yours...
set i=0
for /f "delims=" %%c in (templist.txt) do (
set /a i+=1
set A1[!i!]=%%c
)

set i=0
for /f "delims=" %%d in (templist2.txt) do (
set /a i+=1
set A2[!i!]=%%d
)

del list.txt
for /l %%i in (1,1,%i%) do (
echo !A1[%%i]! !A2[%%i]!>>list.txt
)

del templist.txt
del templist2.txt

:list
color 0a
cls
type list.txt
pause,

message edited by nbrane


Report •

#2
August 12, 2013 at 07:40:24
Just an aside:

You don't have anything to ensure the user enters Y or N. If the user enters something else, such as the word "no", your code will treat it as if the user had entered Y.

Better to use the CHOICE command instead of SET /P. Type CHOICE /? for help.


Report •

#3
August 12, 2013 at 17:44:09
@klint Ye, i'm well aware of it. Just creating something for myself though, because it's hard to keep track of 500 people and knowing how long they have been in the guild.

Edit:

@nbrane Oh wow, it seems to be working. Thank you so much :D!!

message edited by Tygrtraxx


Report •

Related Solutions

#4
August 12, 2013 at 18:54:07
@Klint: good suggestion. I was in "damage-control" mode, and didn't consider the finer points.
@op: thanks :-)

Report •


Ask Question