GajanaN Chalke
GajanaN Chalke

Reputation: 527

I'm not able to create 10 files with following batch code

Following is the code in which I want to create 10 files if the counter reach 10 but the comparison is not working am I missing something or am I doing something wrong? It creates only one file and prints as following in that one file

10 == 0 set

@echo off
set limit=10
set count=0
:start
set count = %count% + 1
echo %limit% == %count% set > YouAreAnIdiot%random%.txt
if %count%==%limit% 
    exit 0
else 
    goto start

Upvotes: 0

Views: 48

Answers (1)

Stephan
Stephan

Reputation: 56188

two errors in one line: set count = %count% + 1:

a) the space between count and = is part of your variable name. (It would be %count %)

b) to calculate with set, you need the /a parameter:

set /a count=%count% + 1

Surprisingly, set /a doesn't care for the additional space, but get used to the syntax without spaces around the = - this keeps life simple.

set /a doesn't need the percent signs with variables, so set /a count=count+1 also works.

There is a short form to do that:

set /a count+=1

Also your if statement will not work. The complete construct has to be on one (logical) line:

if %count%==%limit% (
    exit 0
) else ( 
    goto start
)

(note the spaces around the parantheses - they are critical)

Upvotes: 2

Related Questions