I need help with this code!!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

I need help with this code!!

#1 Post by BatMaster » 01 Oct 2011 11:03

Code: Select all

@echo off
set /a n1=%random% %% 10
set /a n2=%random% %% 10
<file1.txt (
set /p 1=
set /p 2=
set /p 3=
set /p 4=
set /p 5=
set /p 6=
set /p 7=
set /p 8=
set /p 9=
set /p 10=
<file2.txt (
set /p 1a=
set /p 2a=
set /p 3a=
set /p 4a=
set /p 5a=
set /p 6a=
set /p 7a=
set /p 8a=
set /p 9a=
set /p 10a=
echo %n1%
echo %n2%a
pause

the code is supposed to output the lines imported from file1 and file2 but it wont :( plese help :?:

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need help with this code!!

#2 Post by aGerman » 01 Oct 2011 11:37

The result of %random% %% 10 is a number between 0 and 9. You should add 1.
Don't use numbers in the beginning of a variable name!
You forgot to close the parentheses.

Code: Select all

@echo off &setlocal
set /a n1=%random% %% 10 + 1
set /a n2=%random% %% 10 + 1

<file1.txt (
  for /l %%i in (1,1,%n1%) do set /p "line_file1="
)

<file2.txt (
  for /l %%i in (1,1,%n2%) do set /p "line_file2="
)

echo(%line_file1%
echo(%line_file2%
pause

Regards
aGerman

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: I need help with this code!!

#3 Post by !k » 01 Oct 2011 11:43

Code: Select all

@echo off
set /a n1=%random% %% 10 +1
set /a n2=%random% %% 10 +1
<file1.txt (
set /p _1=
set /p _2=
set /p _3=
set /p _4=
set /p _5=
set /p _6=
set /p _7=
set /p _8=
set /p _9=
set /p _10=
)
<file2.txt (
set /p _1a=
set /p _2a=
set /p _3a=
set /p _4a=
set /p _5a=
set /p _6a=
set /p _7a=
set /p _8a=
set /p _9a=
set /p _10a=
)
call echo var %%%%_%n1%%%%% = %%_%n1%%%
call echo var %%%%_%n2%a%%%% = %%_%n2%a%%

:mrgreen:

BatMaster
Posts: 28
Joined: 22 Dec 2010 12:53

Re: I need help with this code!!

#4 Post by BatMaster » 01 Oct 2011 13:34

@ !k why does your code echo

Code: Select all

var %_10% = %line%
var %_6a% = %line2%

line1 and 2 being whatever was on the lines 10 and 6a
@aGerman
thanks for your help
your code works perfectley
-=BatMaster=-

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: I need help with this code!!

#5 Post by !k » 01 Oct 2011 13:51

BatMaster, just erase red symbols

call echo var %%%%_%n1%%%%% = %%_%n1%%%
call echo var %%%%_%n2%a%%%% = %%_%n2%a%%

Post Reply