weird if problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chronogod
Posts: 5
Joined: 29 Apr 2017 13:27

weird if problem

#1 Post by chronogod » 18 May 2017 15:06

I'm creating a program that makes characters randomly turn into others, and I've run into a pretty weird problem.

In :main, I have a series of if statements within a for loop within a for loop, that's causing the program not run the for loops and crash. I don't really understand why the problem is that, so if someone could explain it, and or provide a fix that would be helpful. I do know that the problem is more specifically with the "if %bloche%" area.

Code: Select all

@echo off
title engine
setlocal enableExtensions

:pre
for %%G in (1,2,3,4,5,6,7,8,9,10) do (
   for %%H in (1,2,3,4,5,6,7,8,9,10) do (
      >"%%G'%%H.txt" echo o
   )
)
start screen.bat
goto main

:main
timeout /t 1 >nul
>"test.txt" echo two
for %%I in (1,2,3,4,5,6,7,8,9,10) do (
   >"text.txt" echo one
   for %%J in (1,2,3,4,5,6,7,8,9,10) do (
      set /p bloche=<"%%I'%%J.txt"
      if %random% geq 31948 (
         if %bloche% == o (
            >"%%I'%%J.txt" echo n
         )
         if %bloche% == n (
            >"%%I'%%J.txt" echo m
         )
         if %bloche% == m (
            >"%%I'%%J.txt" echo W
         )
         if %bloche% == W (
            >"%%I'%%J.txt" echo K
         )
         if %bloche% == K (
            >"%%I'%%J.txt" echo U
         )
         if %bloche% == U (
            >"%%I'%%J.txt" echo C
         )
      )
   )
)
goto main

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

Re: weird if problem

#2 Post by aGerman » 18 May 2017 15:40

As always - a normal environment variable in a command line or in a block of command lines (enclosed into parentheses) will be expanded to its value only once before the line or block will be even executed. To avoid this "early" expansion you have to enable the delayed variable expansion (setlocal EnableDelayedExpansion) and to replace the percent signs of the variables with exclamation marks.
Also:
- be sure there are no spaces next to your redirected letters
- don't use spaces around the == comparison operator.
- once a character was found you don't need to compare all the others; thats what "else" is for

Code: Select all

@echo off
title engine
setlocal enableExtensions

:pre
for %%G in (1,2,3,4,5,6,7,8,9,10) do (
   for %%H in (1,2,3,4,5,6,7,8,9,10) do (
      >"%%G'%%H.txt" echo o
   )
)
:: whatever screen.bat is for ...
:: start screen.bat

:: the important line:
setlocal EnableDelayedExpansion

:main
timeout /t 1 >nul
>"test.txt" echo two
for %%I in (1,2,3,4,5,6,7,8,9,10) do (
   >"text.txt" echo one
   for %%J in (1,2,3,4,5,6,7,8,9,10) do (
      set /p bloche=<"%%I'%%J.txt"
      if !random! geq 31948 (
         if !bloche!==o (
            >"%%I'%%J.txt" echo n
         ) else if !bloche!==n (
            >"%%I'%%J.txt" echo m
         ) else if !bloche!==m (
            >"%%I'%%J.txt" echo W
         ) else if !bloche!==W (
            >"%%I'%%J.txt" echo K
         ) else if !bloche!==K (
            >"%%I'%%J.txt" echo U
         ) else if !bloche!==U (
            >"%%I'%%J.txt" echo C
         )
      )
   )
)
goto main

Steffen

batnoob
Posts: 56
Joined: 19 Apr 2017 12:23

Re: weird if problem

#3 Post by batnoob » 18 May 2017 15:45

1. What are you trying to accomplish with this.
2. What is "Screen.bat".
3. It runs fine on my machine when I comment out the "start screen.bat", so it must be a problem with "screen.bat".

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

Re: weird if problem

#4 Post by aGerman » 18 May 2017 16:01

@batnoob

It creates the files but then dies immediatelly because %bloche% doesn't expand to value and thus, causes a syntax error in the IF statements. (See my comments.)
I also don't know what screen.bat is. Because it runs in another cmd process it's rather unlikely it causes problems in this code. (Only for us because we don't have it in our environment.)

Steffen

chronogod
Posts: 5
Joined: 29 Apr 2017 13:27

Re: weird if problem

#5 Post by chronogod » 18 May 2017 16:33

thanks guys, it works now!!!

this is the code for the screen, just in case you want it

Code: Select all

@echo off
title screen
setlocal enableExtensions

:one
timeout /t 1 >nul
for %%G in (1,2,3,4,5,6,7,8,9,10) do (
   for %%H in (1,2,3,4,5,6,7,8,9,10) do (
      set /p a%%G'%%H=<"%%G'%%H.txt"
   )
)

:two
cls
echo %a1'1%%a1'2%%a1'3%%a1'4%%a1'5%%a1'6%%a1'7%%a1'8%%a1'9%%a1'10%
echo %a2'1%%a2'2%%a2'3%%a2'4%%a2'5%%a2'6%%a2'7%%a2'8%%a2'9%%a2'10%
echo %a3'1%%a3'2%%a3'3%%a3'4%%a3'5%%a3'6%%a3'7%%a3'8%%a3'9%%a3'10%
echo %a4'1%%a4'2%%a4'3%%a4'4%%a4'5%%a4'6%%a4'7%%a4'8%%a4'9%%a4'10%
echo %a5'1%%a5'2%%a5'3%%a5'4%%a5'5%%a5'6%%a5'7%%a5'8%%a5'9%%a5'10%
echo %a6'1%%a6'2%%a6'3%%a6'4%%a6'5%%a6'6%%a6'7%%a6'8%%a6'9%%a6'10%
echo %a7'1%%a7'2%%a7'3%%a7'4%%a7'5%%a7'6%%a7'7%%a7'8%%a7'9%%a7'10%
echo %a8'1%%a8'2%%a8'3%%a8'4%%a8'5%%a8'6%%a8'7%%a8'8%%a8'9%%a8'10%
echo %a9'1%%a9'2%%a9'3%%a9'4%%a9'5%%a9'6%%a9'7%%a9'8%%a9'9%%a9'10%
echo %a10'1%%a10'2%%a10'3%%a10'4%%a10'5%%a10'6%%a10'7%%a10'8%%a10'9%%a10'10%
goto one

Post Reply