assinging `grep "string"|wc -l` into a variable in batch scripting - if-statement

I am trying to develop a batch equivalent (windows) code of below unix script:-
i=`grep "ora-error" *.txt|wc -l`
if [ i -gt 0 ]; then
echo "true"
else
echo "false"
fi
tried with "find /c "ora-error" *.txt" but was not able to take the o/p in to variable and redirect the same to if condition...
ie. when I use "find /c "ora-error" *.txt" below is the output
---------- xx.TXT: 0
---------- yy.TXT: 0
---------- zz.TXT: 0
I want to take the count and redirect valur into a IF condition.
I need a window script to solve this issue.
Can any body help on this...
Thanks in advance..
with regards
sree
Guys worked out a solution....
find /c "ora-error" *.txt>TEST1.txt
find /c "0" TEST1.TXT>TEST2.TXT
FOR /F "TOKENS=1* DELIMS=:" %%A IN (TEST2.TXT) DO SET a=%%B
set _count=%a%
IF %_count% EQU 4 goto matched
IF not %_count% EQU 4 goto notmatched
:matched
echo we are in equal to 4 loop
pause
exit
:notmatched
echo we are in not equal to 4 loop
pause
exit

grep "ora-error" *.txt>/dev/null
if [ $? -eq 0 ]; then
echo "true"
else
echo "false"
fi

Related

How do I get Batch to recognise variable is equal to '#' instead of 'a'

I want to create a batch file rogue like, however I need to fix a slight problem. When the character walks into a wall ( # ) I want the character to go back a step, and that works, however I cannot get the batch script to recognise if the character has walked into a wall!
#echo off
set drive=%~dp0%
:start
echo %~dp0%
echo Set name
set /p file=">"
if not exist "%drive%Locations\%file%.bat" goto mdfile
set x=12
set y=15
goto level
:level
set loc=x%x%y%y%
set x%x%y%y%=a
call %drive%Locations\%file%.bat
if '%loc%'=='a' goto start
if '%loc%'=='0' echo Entered shop && pause && goto %dire%
if '%loc%'=='~' echo You are drowning && pause && goto boucle
goto boucle
:mdfile
call %~dp0%Locations\make.bat
set x=12
set y=15
goto level
:boucle
cls
echo.
call :map
echo.
echo %loc%
if '%%!x%x%y%y%!%%' == !#! (echo Equal) else (echo Not Equal)
echo q w e a s d ///// %let%
echo.
set loc=x%x%x%y%
choice /c qweasd /n >nul
if %errorlevel%==1 goto :savenum
if %errorlevel%==2 goto :up
if %errorlevel%==3 goto :savenuma
if %errorlevel%==4 goto :left
if %errorlevel%==5 goto :down
if %errorlevel%==6 goto :right
goto boucle
:map
%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%
echo %c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%
echo %c0c0%%x24y1%%x24y2%%x24y3%%x24y4%%x24y5%%x24y6%%x24y7%%x24y8%%x24y9%%x24y10%%x24y11%%x24y12%%x24y13%%x24y14%%x24y15%%x24y16%%x24y17%%x24y18%%x24y19%%x24y20%%x24y21%%x24y22%%x24y23%%x24y24%%x24y25%%x24y26%%x24y27%%x24y28%%x24y29%%x24y30%%c0c0%
echo %c0c0%%x23y1%%x23y2%%x23y3%%x23y4%%x23y5%%x23y6%%x23y7%%x23y8%%x23y9%%x23y10%%x23y11%%x23y12%%x23y13%%x23y14%%x23y15%%x23y16%%x23y17%%x23y18%%x23y19%%x23y20%%x23y21%%x23y22%%x23y23%%x23y24%%x23y25%%x23y26%%x23y27%%x23y28%%x23y29%%x23y30%%c0c0%
echo %c0c0%%x22y1%%x22y2%%x22y3%%x22y4%%x22y5%%x22y6%%x22y7%%x22y8%%x22y9%%x22y10%%x22y11%%x22y12%%x22y13%%x22y14%%x22y15%%x22y16%%x22y17%%x22y18%%x22y19%%x22y20%%x22y21%%x22y22%%x22y23%%x22y24%%x22y25%%x22y26%%x22y27%%x22y28%%x22y29%%x22y30%%c0c0%
echo %c0c0%%x21y1%%x21y2%%x21y3%%x21y4%%x21y5%%x21y6%%x21y7%%x21y8%%x21y9%%x21y10%%x21y11%%x21y12%%x21y13%%x21y14%%x21y15%%x21y16%%x21y17%%x21y18%%x21y19%%x21y20%%x21y21%%x21y22%%x21y23%%x21y24%%x21y25%%x21y26%%x21y27%%x21y28%%x21y29%%x21y30%%c0c0%
echo %c0c0%%x20y1%%x20y2%%x20y3%%x20y4%%x20y5%%x20y6%%x20y7%%x20y8%%x20y9%%x20y10%%x20y11%%x20y12%%x20y13%%x20y14%%x20y15%%x20y16%%x20y17%%x20y18%%x20y19%%x20y20%%x20y21%%x20y22%%x20y23%%x20y24%%x20y25%%x20y26%%x20y27%%x20y28%%x20y29%%x20y30%%c0c0%
echo %c0c0%%x19y1%%x19y2%%x19y3%%x19y4%%x19y5%%x19y6%%x19y7%%x19y8%%x19y9%%x19y10%%x19y11%%x19y12%%x19y13%%x19y14%%x19y15%%x19y16%%x19y17%%x19y18%%x19y19%%x19y20%%x19y21%%x19y22%%x19y23%%x19y24%%x19y25%%x19y26%%x19y27%%x19y28%%x19y29%%x19y30%%c0c0%
echo %c0c0%%x18y1%%x18y2%%x18y3%%x18y4%%x18y5%%x18y6%%x18y7%%x18y8%%x18y9%%x18y10%%x18y11%%x18y12%%x18y13%%x18y14%%x18y15%%x18y16%%x18y17%%x18y18%%x18y19%%x18y20%%x18y21%%x18y22%%x18y23%%x18y24%%x18y25%%x18y26%%x18y27%%x18y28%%x18y29%%x18y30%%c0c0%
echo %c0c0%%x17y1%%x17y2%%x17y3%%x17y4%%x17y5%%x17y6%%x17y7%%x17y8%%x17y9%%x17y10%%x17y11%%x17y12%%x17y13%%x17y14%%x17y15%%x17y16%%x17y17%%x17y18%%x17y19%%x17y20%%x17y21%%x17y22%%x17y23%%x17y24%%x17y25%%x17y26%%x17y27%%x17y28%%x17y29%%x17y30%%c0c0%
echo %c0c0%%x16y1%%x16y2%%x16y3%%x16y4%%x16y5%%x16y6%%x16y7%%x16y8%%x16y9%%x16y10%%x16y11%%x16y12%%x16y13%%x16y14%%x16y15%%x16y16%%x16y17%%x16y18%%x16y19%%x16y20%%x16y21%%x16y22%%x16y23%%x16y24%%x16y25%%x16y26%%x16y27%%x16y28%%x16y29%%x16y30%%c0c0%
echo %c0c0%%x15y1%%x15y2%%x15y3%%x15y4%%x15y5%%x15y6%%x15y7%%x15y8%%x15y9%%x15y10%%x15y11%%x15y12%%x15y13%%x15y14%%x15y15%%x15y16%%x15y17%%x15y18%%x15y19%%x15y20%%x15y21%%x15y22%%x15y23%%x15y24%%x15y25%%x15y26%%x15y27%%x15y28%%x15y29%%x15y30%%c0c0%
echo %c0c0%%x14y1%%x14y2%%x14y3%%x14y4%%x14y5%%x14y6%%x14y7%%x14y8%%x14y9%%x14y10%%x14y11%%x14y12%%x14y13%%x14y14%%x14y15%%x14y16%%x14y17%%x14y18%%x14y19%%x14y20%%x14y21%%x14y22%%x14y23%%x14y24%%x14y25%%x14y26%%x14y27%%x14y28%%x14y29%%x14y30%%c0c0%
echo %c0c0%%x13y1%%x13y2%%x13y3%%x13y4%%x13y5%%x13y6%%x13y7%%x13y8%%x13y9%%x13y10%%x13y11%%x13y12%%x13y13%%x13y14%%x13y15%%x13y16%%x13y17%%x13y18%%x13y19%%x13y20%%x13y21%%x13y22%%x13y23%%x13y24%%x13y25%%x13y26%%x13y27%%x13y28%%x13y29%%x13y30%%c0c0%
echo %c0c0%%x12y1%%x12y2%%x12y3%%x12y4%%x12y5%%x12y6%%x12y7%%x12y8%%x12y9%%x12y10%%x12y11%%x12y12%%x12y13%%x12y14%%x12y15%%x12y16%%x12y17%%x12y18%%x12y19%%x12y20%%x12y21%%x12y22%%x12y23%%x12y24%%x12y25%%x12y26%%x12y27%%x12y28%%x12y29%%x12y30%%c0c0%
echo %c0c0%%x11y1%%x11y2%%x11y3%%x11y4%%x11y5%%x11y6%%x11y7%%x11y8%%x11y9%%x11y10%%x11y11%%x11y12%%x11y13%%x11y14%%x11y15%%x11y16%%x11y17%%x11y18%%x11y19%%x11y20%%x11y21%%x11y22%%x11y23%%x11y24%%x11y25%%x11y26%%x11y27%%x11y28%%x11y29%%x11y30%%c0c0%
echo %c0c0%%x10y1%%x10y2%%x10y3%%x10y4%%x10y5%%x10y6%%x10y7%%x10y8%%x10y9%%x10y10%%x10y11%%x10y12%%x10y13%%x10y14%%x10y15%%x10y16%%x10y17%%x10y18%%x10y19%%x10y20%%x10y21%%x10y22%%x10y23%%x10y24%%x10y25%%x10y26%%x10y27%%x10y28%%x10y29%%x10y30%%c0c0%
echo %c0c0%%x9y1%%x9y2%%x9y3%%x9y4%%x9y5%%x9y6%%x9y7%%x9y8%%x9y9%%x9y10%%x9y11%%x9y12%%x9y13%%x9y14%%x9y15%%x9y16%%x9y17%%x9y18%%x9y19%%x9y20%%x9y21%%x9y22%%x9y23%%x9y24%%x9y25%%x9y26%%x9y27%%x9y28%%x9y29%%x9y30%%c0c0%
echo %c0c0%%x8y1%%x8y2%%x8y3%%x8y4%%x8y5%%x8y6%%x8y7%%x8y8%%x8y9%%x8y10%%x8y11%%x8y12%%x8y13%%x8y14%%x8y15%%x8y16%%x8y17%%x8y18%%x8y19%%x8y20%%x8y21%%x8y22%%x8y23%%x8y24%%x8y25%%x8y26%%x8y27%%x8y28%%x8y29%%x8y30%%c0c0%
echo %c0c0%%x7y1%%x7y2%%x7y3%%x7y4%%x7y5%%x7y6%%x7y7%%x7y8%%x7y9%%x7y10%%x7y11%%x7y12%%x7y13%%x7y14%%x7y15%%x7y16%%x7y17%%x7y18%%x7y19%%x7y20%%x7y21%%x7y22%%x7y23%%x7y24%%x7y25%%x7y26%%x7y27%%x7y28%%x7y29%%x7y30%%c0c0%
echo %c0c0%%x6y1%%x6y2%%x6y3%%x6y4%%x6y5%%x6y6%%x6y7%%x6y8%%x6y9%%x6y10%%x6y11%%x6y12%%x6y13%%x6y14%%x6y15%%x6y16%%x6y17%%x6y18%%x6y19%%x6y20%%x6y21%%x6y22%%x6y23%%x6y24%%x6y25%%x6y26%%x6y27%%x6y28%%x6y29%%x6y30%%c0c0%
echo %c0c0%%x5y1%%x5y2%%x5y3%%x5y4%%x5y5%%x5y6%%x5y7%%x5y8%%x5y9%%x5y10%%x5y11%%x5y12%%x5y13%%x5y14%%x5y15%%x5y16%%x5y17%%x5y18%%x5y19%%x5y20%%x5y21%%x5y22%%x5y23%%x5y24%%x5y25%%x5y26%%x5y27%%x5y28%%x5y29%%x5y30%%c0c0%
echo %c0c0%%x4y1%%x4y2%%x4y3%%x4y4%%x4y5%%x4y6%%x4y7%%x4y8%%x4y9%%x4y10%%x4y11%%x4y12%%x4y13%%x4y14%%x4y15%%x4y16%%x4y17%%x4y18%%x4y19%%x4y20%%x4y21%%x4y22%%x4y23%%x4y24%%x4y25%%x4y26%%x4y27%%x4y28%%x4y29%%x4y30%%c0c0%
echo %c0c0%%x3y1%%x3y2%%x3y3%%x3y4%%x3y5%%x3y6%%x3y7%%x3y8%%x3y9%%x3y10%%x3y11%%x3y12%%x3y13%%x3y14%%x3y15%%x3y16%%x3y17%%x3y18%%x3y19%%x3y20%%x3y21%%x3y22%%x3y23%%x3y24%%x3y25%%x3y26%%x3y27%%x3y28%%x3y29%%x3y30%%c0c0%
echo %c0c0%%x2y1%%x2y2%%x2y3%%x2y4%%x2y5%%x2y6%%x2y7%%x2y8%%x2y9%%x2y10%%x2y11%%x2y12%%x2y13%%x2y14%%x2y15%%x2y16%%x2y17%%x2y18%%x2y19%%x2y20%%x2y21%%x2y22%%x2y23%%x2y24%%x2y25%%x2y26%%x2y27%%x2y28%%x2y29%%x2y30%%c0c0%
echo %c0c0%%x1y1%%x1y2%%x1y3%%x1y4%%x1y5%%x1y6%%x1y7%%x1y8%%x1y9%%x1y10%%x1y11%%x1y12%%x1y13%%x1y14%%x1y15%%x1y16%%x1y17%%x1y18%%x1y19%%x1y20%%x1y21%%x1y22%%x1y23%%x1y24%%x1y25%%x1y26%%x1y27%%x1y28%%x1y29%%x1y30%%c0c0%
echo %c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%
goto :eof
:LEFT
if '%y%'=='1' goto selecmap
set dire=right
set /a y=%y% - 1
set x%x%y%y%=a
goto level
:RIGHT
if '%y%'=='30' goto selecmap
set dire=left
set /a y=%y% + 1
set x%x%y%y%=a
goto level
:UP
if '%x%'=='24' goto selecmap
set dire=down
set /a x=%x% + 1
set x%x%y%y%=a
goto level
:DOWN
if '%x%'=='1' goto selecmap
set dire=up
set /a x=%x% - 1
set x%x%y%y%=a
goto level
:selecmap
goto boucle
set /p =
goto level
Essentially, the call :level command calls the set x1y1=. all the way through to set x24y30=. . This is the way the map is refreshed. after that, there is set x%x%y%y%=a to show the avatar. The walls are set after that line. I want the batch script to recognise it has walked into a wall because the avatar is not set to a, but rather the avatar is # instead. then it would goto :dire and reset where the avatar is.
any help?
Here is the "make.bat" file mentioned. to test, make sure it is in the same directory as this batch file, in a subdirectory folder called "Locations"
(
echo set c0c0=#
echo set cncn=%nd%
echo set cece=%ed%
echo set cscs=%sd%
echo set cwcw=%wd%
echo set x1y1=.
echo set x2y1=.
echo set x3y1=.
echo set x4y1=.
echo set x5y1=.
echo set x6y1=.
echo set x7y1=.
echo set x8y1=.
echo set x9y1=.
echo set x10y1=.
echo set x11y1=.
echo set x12y1=.
echo set x13y1=.
echo set x14y1=.
echo set x15y1=.
echo set x16y1=.
echo set x17y1=.
echo set x18y1=.
echo set x19y1=.
echo set x20y1=.
echo set x21y1=.
echo set x22y1=.
echo set x23y1=.
echo set x24y1=.
echo set x1y2=.
echo set x2y2=.
echo set x3y2=.
echo set x4y2=.
echo set x5y2=.
echo set x6y2=.
echo set x7y2=.
echo set x8y2=.
echo set x9y2=.
echo set x10y2=.
echo set x11y2=.
echo set x12y2=.
echo set x13y2=.
echo set x14y2=.
echo set x15y2=.
echo set x16y2=.
echo set x17y2=.
echo set x18y2=.
echo set x19y2=.
echo set x20y2=.
echo set x21y2=.
echo set x22y2=.
echo set x23y2=.
echo set x24y2=.
echo set x1y3=.
echo set x2y3=.
echo set x3y3=.
echo set x4y3=.
echo set x5y3=.
echo set x6y3=.
echo set x7y3=.
echo set x8y3=.
echo set x9y3=.
echo set x10y3=.
echo set x11y3=.
echo set x12y3=.
echo set x13y3=.
echo set x14y3=.
echo set x15y3=.
echo set x16y3=.
echo set x17y3=.
echo set x18y3=.
echo set x19y3=.
echo set x20y3=.
echo set x21y3=.
echo set x22y3=.
echo set x23y3=.
echo set x24y3=.
echo set x1y4=.
echo set x2y4=.
echo set x3y4=.
echo set x4y4=.
echo set x5y4=.
echo set x6y4=.
echo set x7y4=.
echo set x8y4=.
echo set x9y4=.
echo set x10y4=.
echo set x11y4=.
echo set x12y4=.
echo set x13y4=.
echo set x14y4=.
echo set x15y4=.
echo set x16y4=.
echo set x17y4=.
echo set x18y4=.
echo set x19y4=.
echo set x20y4=.
echo set x21y4=.
echo set x22y4=.
echo set x23y4=.
echo set x24y4=.
echo set x1y5=.
echo set x2y5=.
echo set x3y5=.
echo set x4y5=.
echo set x5y5=.
echo set x6y5=.
echo set x7y5=.
echo set x8y5=.
echo set x9y5=.
echo set x10y5=.
echo set x11y5=.
echo set x12y5=.
echo set x13y5=.
echo set x14y5=.
echo set x15y5=.
echo set x16y5=.
echo set x17y5=.
echo set x18y5=.
echo set x19y5=.
echo set x20y5=.
echo set x21y5=.
echo set x22y5=.
echo set x23y5=.
echo set x24y5=.
echo set x1y6=.
echo set x2y6=.
echo set x3y6=.
echo set x4y6=.
echo set x5y6=.
echo set x6y6=.
echo set x7y6=.
echo set x8y6=.
echo set x9y6=.
echo set x10y6=.
echo set x11y6=.
echo set x12y6=.
echo set x13y6=.
echo set x14y6=.
echo set x15y6=.
echo set x16y6=.
echo set x17y6=.
echo set x18y6=.
echo set x19y6=.
echo set x20y6=.
echo set x21y6=.
echo set x22y6=.
echo set x23y6=.
echo set x24y6=.
echo set x1y7=.
echo set x2y7=.
echo set x3y7=.
echo set x4y7=.
echo set x5y7=.
echo set x6y7=.
echo set x7y7=.
echo set x8y7=.
echo set x9y7=.
echo set x10y7=.
echo set x11y7=.
echo set x12y7=.
echo set x13y7=.
echo set x14y7=.
echo set x15y7=.
echo set x16y7=.
echo set x17y7=.
echo set x18y7=.
echo set x19y7=.
echo set x20y7=.
echo set x21y7=.
echo set x22y7=.
echo set x23y7=.
echo set x24y7=.
echo set x1y8=.
echo set x2y8=.
echo set x3y8=.
echo set x4y8=.
echo set x5y8=.
echo set x6y8=.
echo set x7y8=.
echo set x8y8=.
echo set x9y8=.
echo set x10y8=.
echo set x11y8=.
echo set x12y8=.
echo set x13y8=.
echo set x14y8=.
echo set x15y8=.
echo set x16y8=.
echo set x17y8=.
echo set x18y8=.
echo set x19y8=.
echo set x20y8=.
echo set x21y8=.
echo set x22y8=.
echo set x23y8=.
echo set x24y8=.
echo set x1y9=.
echo set x2y9=.
echo set x3y9=.
echo set x4y9=.
echo set x5y9=.
echo set x6y9=.
echo set x7y9=.
echo set x8y9=.
echo set x9y9=.
echo set x10y9=.
echo set x11y9=.
echo set x12y9=.
echo set x13y9=.
echo set x14y9=.
echo set x15y9=.
echo set x16y9=.
echo set x17y9=.
echo set x18y9=.
echo set x19y9=.
echo set x20y9=.
echo set x21y9=.
echo set x22y9=.
echo set x23y9=.
echo set x24y9=.
echo set x1y10=.
echo set x2y10=.
echo set x3y10=.
echo set x4y10=.
echo set x5y10=.
echo set x6y10=.
echo set x7y10=.
echo set x8y10=.
echo set x9y10=.
echo set x10y10=.
echo set x11y10=.
echo set x12y10=.
echo set x13y10=.
echo set x14y10=.
echo set x15y10=.
echo set x16y10=.
echo set x17y10=.
echo set x18y10=.
echo set x19y10=.
echo set x20y10=.
echo set x21y10=.
echo set x22y10=.
echo set x23y10=.
echo set x24y10=.
echo set x1y11=.
echo set x2y11=.
echo set x3y11=.
echo set x4y11=.
echo set x5y11=.
echo set x6y11=.
echo set x7y11=.
echo set x8y11=.
echo set x9y11=.
echo set x10y11=.
echo set x11y11=.
echo set x12y11=.
echo set x13y11=.
echo set x14y11=.
echo set x15y11=.
echo set x16y11=.
echo set x17y11=.
echo set x18y11=.
echo set x19y11=.
echo set x20y11=.
echo set x21y11=.
echo set x22y11=.
echo set x23y11=.
echo set x24y11=.
echo set x1y12=.
echo set x2y12=.
echo set x3y12=.
echo set x4y12=.
echo set x5y12=.
echo set x6y12=.
echo set x7y12=.
echo set x8y12=.
echo set x9y12=.
echo set x10y12=.
echo set x11y12=.
echo set x12y12=.
echo set x13y12=.
echo set x14y12=.
echo set x15y12=.
echo set x16y12=.
echo set x17y12=.
echo set x18y12=.
echo set x19y12=.
echo set x20y12=.
echo set x21y12=.
echo set x22y12=.
echo set x23y12=.
echo set x24y12=.
echo set x1y13=.
echo set x2y13=.
echo set x3y13=.
echo set x4y13=.
echo set x5y13=.
echo set x6y13=.
echo set x7y13=.
echo set x8y13=.
echo set x9y13=.
echo set x10y13=.
echo set x11y13=.
echo set x12y13=.
echo set x13y13=.
echo set x14y13=.
echo set x15y13=.
echo set x16y13=.
echo set x17y13=.
echo set x18y13=.
echo set x19y13=.
echo set x20y13=.
echo set x21y13=.
echo set x22y13=.
echo set x23y13=.
echo set x24y13=.
echo set x1y14=.
echo set x2y14=.
echo set x3y14=.
echo set x4y14=.
echo set x5y14=.
echo set x6y14=.
echo set x7y14=.
echo set x8y14=.
echo set x9y14=.
echo set x10y14=.
echo set x11y14=.
echo set x12y14=.
echo set x13y14=.
echo set x14y14=.
echo set x15y14=.
echo set x16y14=.
echo set x17y14=.
echo set x18y14=.
echo set x19y14=.
echo set x20y14=.
echo set x21y14=.
echo set x22y14=.
echo set x23y14=.
echo set x24y14=.
echo set x1y15=.
echo set x2y15=.
echo set x3y15=.
echo set x4y15=.
echo set x5y15=.
echo set x6y15=.
echo set x7y15=.
echo set x8y15=.
echo set x9y15=.
echo set x10y15=.
echo set x11y15=.
echo set x12y15=.
echo set x13y15=.
echo set x14y15=.
echo set x15y15=.
echo set x16y15=.
echo set x17y15=.
echo set x18y15=.
echo set x19y15=.
echo set x20y15=.
echo set x21y15=.
echo set x22y15=.
echo set x23y15=.
echo set x24y15=.
echo set x1y16=.
echo set x2y16=.
echo set x3y16=.
echo set x4y16=.
echo set x5y16=.
echo set x6y16=.
echo set x7y16=.
echo set x8y16=.
echo set x9y16=.
echo set x10y16=.
echo set x11y16=.
echo set x12y16=.
echo set x13y16=.
echo set x14y16=.
echo set x15y16=.
echo set x16y16=.
echo set x17y16=.
echo set x18y16=.
echo set x19y16=.
echo set x20y16=.
echo set x21y16=.
echo set x22y16=.
echo set x23y16=.
echo set x24y16=.
echo set x1y17=.
echo set x2y17=.
echo set x3y17=.
echo set x4y17=.
echo set x5y17=.
echo set x6y17=.
echo set x7y17=.
echo set x8y17=.
echo set x9y17=.
echo set x10y17=.
echo set x11y17=.
echo set x12y17=.
echo set x13y17=.
echo set x14y17=.
echo set x15y17=.
echo set x16y17=.
echo set x17y17=.
echo set x18y17=.
echo set x19y17=.
echo set x20y17=.
echo set x21y17=.
echo set x22y17=.
echo set x23y17=.
echo set x24y17=.
echo set x1y18=.
echo set x2y18=.
echo set x3y18=.
echo set x4y18=.
echo set x5y18=.
echo set x6y18=.
echo set x7y18=.
echo set x8y18=.
echo set x9y18=.
echo set x10y18=.
echo set x11y18=.
echo set x12y18=.
echo set x13y18=.
echo set x14y18=.
echo set x15y18=.
echo set x16y18=.
echo set x17y18=.
echo set x18y18=.
echo set x19y18=.
echo set x20y18=.
echo set x21y18=.
echo set x22y18=.
echo set x23y18=.
echo set x24y18=.
echo set x1y19=.
echo set x2y19=.
echo set x3y19=.
echo set x4y19=.
echo set x5y19=.
echo set x6y19=.
echo set x7y19=.
echo set x8y19=.
echo set x9y19=.
echo set x10y19=.
echo set x11y19=.
echo set x12y19=.
echo set x13y19=.
echo set x14y19=.
echo set x15y19=.
echo set x16y19=.
echo set x17y19=.
echo set x18y19=.
echo set x19y19=.
echo set x20y19=.
echo set x21y19=.
echo set x22y19=.
echo set x23y19=.
echo set x24y19=.
echo set x1y20=.
echo set x2y20=.
echo set x3y20=.
echo set x4y20=.
echo set x5y20=.
echo set x6y20=.
echo set x7y20=.
echo set x8y20=.
echo set x9y20=.
echo set x10y20=.
echo set x11y20=.
echo set x12y20=.
echo set x13y20=.
echo set x14y20=.
echo set x15y20=.
echo set x16y20=.
echo set x17y20=.
echo set x18y20=.
echo set x19y20=.
echo set x20y20=.
echo set x21y20=.
echo set x22y20=.
echo set x23y20=.
echo set x24y20=.
echo set x1y21=.
echo set x2y21=.
echo set x3y21=.
echo set x4y21=.
echo set x5y21=.
echo set x6y21=.
echo set x7y21=.
echo set x8y21=.
echo set x9y21=.
echo set x10y21=.
echo set x11y21=.
echo set x12y21=.
echo set x13y21=.
echo set x14y21=.
echo set x15y21=.
echo set x16y21=.
echo set x17y21=.
echo set x18y21=.
echo set x19y21=.
echo set x20y21=.
echo set x21y21=.
echo set x22y21=.
echo set x23y21=.
echo set x24y21=.
echo set x1y22=.
echo set x2y22=.
echo set x3y22=.
echo set x4y22=.
echo set x5y22=.
echo set x6y22=.
echo set x7y22=.
echo set x8y22=.
echo set x9y22=.
echo set x10y22=.
echo set x11y22=.
echo set x12y22=.
echo set x13y22=.
echo set x14y22=.
echo set x15y22=.
echo set x16y22=.
echo set x17y22=.
echo set x18y22=.
echo set x19y22=.
echo set x20y22=.
echo set x21y22=.
echo set x22y22=.
echo set x23y22=.
echo set x24y22=.
echo set x1y23=.
echo set x2y23=.
echo set x3y23=.
echo set x4y23=.
echo set x5y23=.
echo set x6y23=.
echo set x7y23=.
echo set x8y23=.
echo set x9y23=.
echo set x10y23=.
echo set x11y23=.
echo set x12y23=.
echo set x13y23=.
echo set x14y23=.
echo set x15y23=.
echo set x16y23=.
echo set x17y23=.
echo set x18y23=.
echo set x19y23=.
echo set x20y23=.
echo set x21y23=.
echo set x22y23=.
echo set x23y23=.
echo set x24y23=.
echo set x1y24=.
echo set x2y24=.
echo set x3y24=.
echo set x4y24=.
echo set x5y24=.
echo set x6y24=.
echo set x7y24=.
echo set x8y24=.
echo set x9y24=.
echo set x10y24=.
echo set x11y24=.
echo set x12y24=.
echo set x13y24=.
echo set x14y24=.
echo set x15y24=.
echo set x16y24=.
echo set x17y24=.
echo set x18y24=.
echo set x19y24=.
echo set x20y24=.
echo set x21y24=.
echo set x22y24=.
echo set x23y24=.
echo set x24y24=.
echo set x1y25=.
echo set x2y25=.
echo set x3y25=.
echo set x4y25=.
echo set x5y25=.
echo set x6y25=.
echo set x7y25=.
echo set x8y25=.
echo set x9y25=.
echo set x10y25=.
echo set x11y25=.
echo set x12y25=.
echo set x13y25=.
echo set x14y25=.
echo set x15y25=.
echo set x16y25=.
echo set x17y25=.
echo set x18y25=.
echo set x19y25=.
echo set x20y25=.
echo set x21y25=.
echo set x22y25=.
echo set x23y25=.
echo set x24y25=.
echo set x1y26=.
echo set x2y26=.
echo set x3y26=.
echo set x4y26=.
echo set x5y26=.
echo set x6y26=.
echo set x7y26=.
echo set x8y26=.
echo set x9y26=.
echo set x10y26=.
echo set x11y26=.
echo set x12y26=.
echo set x13y26=.
echo set x14y26=.
echo set x15y26=.
echo set x16y26=.
echo set x17y26=.
echo set x18y26=.
echo set x19y26=.
echo set x20y26=.
echo set x21y26=.
echo set x22y26=.
echo set x23y26=.
echo set x24y26=.
echo set x1y27=.
echo set x2y27=.
echo set x3y27=.
echo set x4y27=.
echo set x5y27=.
echo set x6y27=.
echo set x7y27=.
echo set x8y27=.
echo set x9y27=.
echo set x10y27=.
echo set x11y27=.
echo set x12y27=.
echo set x13y27=.
echo set x14y27=.
echo set x15y27=.
echo set x16y27=.
echo set x17y27=.
echo set x18y27=.
echo set x19y27=.
echo set x20y27=.
echo set x21y27=.
echo set x22y27=.
echo set x23y27=.
echo set x24y27=.
echo set x1y28=.
echo set x2y28=.
echo set x3y28=.
echo set x4y28=.
echo set x5y28=.
echo set x6y28=.
echo set x7y28=.
echo set x8y28=.
echo set x9y28=.
echo set x10y28=.
echo set x11y28=.
echo set x12y28=.
echo set x13y28=.
echo set x14y28=.
echo set x15y28=.
echo set x16y28=.
echo set x17y28=.
echo set x18y28=.
echo set x19y28=.
echo set x20y28=.
echo set x21y28=.
echo set x22y28=.
echo set x23y28=.
echo set x24y28=.
echo set x1y29=.
echo set x2y29=.
echo set x3y29=.
echo set x4y29=.
echo set x5y29=.
echo set x6y29=.
echo set x7y29=.
echo set x8y29=.
echo set x9y29=.
echo set x10y29=.
echo set x11y29=.
echo set x12y29=.
echo set x13y29=.
echo set x14y29=.
echo set x15y29=.
echo set x16y29=.
echo set x17y29=.
echo set x18y29=.
echo set x19y29=.
echo set x20y29=.
echo set x21y29=.
echo set x22y29=.
echo set x23y29=.
echo set x24y29=.
echo set x1y30=.
echo set x2y30=.
echo set x3y30=.
echo set x4y30=.
echo set x5y30=.
echo set x6y30=.
echo set x7y30=.
echo set x8y30=.
echo set x9y30=.
echo set x10y30=.
echo set x11y30=.
echo set x12y30=.
echo set x13y30=.
echo set x14y30=.
echo set x15y30=.
echo set x16y30=.
echo set x17y30=.
echo set x18y30=.
echo set x19y30=.
echo set x20y30=.
echo set x21y30=.
echo set x22y30=.
echo set x23y30=.
echo set x24y30=.
echo set x%%x%%y%%y%%=a)>%drive%Locations\%file%.bat
A.S.C
(Edit Stephan): this is make.bat that I created last week for testing (creates a random map) - maybe you find it useful:
#echo off
<nul set /p "=building random map "
for /l %%x in (24,-1,1) do (
<nul set /p "=."
for /l %%y in (1,1,30) do (
set "x%%xy%%y=."
call :setvar %%x %%y 20 "#"
call :setvar %%x %%y 10 "~"
)
)
set "x5y5=#"
set "c0c0=#"
goto :eof
:setvar x y threshhold char
if %random:~-2% lss %3 set "x%1y%2=%~4"
I suggest this code which I could not test as posted code is not a minimal, reproducible example.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BatchPath=%~dp0"
:Begin
setlocal EnableDelayedExpansion
echo !BatchPath!
endlocal
:PromptName
set "file="
set /P "file=Set name> "
rem Prompt the user again on no file name entered at all.
if not defined file goto PromptName
rem Remove all double quotes from user input string.
set "file=%file:"=%"
rem Prompt the user again on just double quotes entered by the user.
if not defined file goto PromptName
rem Contains the entered string a character not valid in a file name.
set "ValidName=?"
for /F "eol=| delims=*/:<>?\| " %%I in ("%file%") do set "ValidName=%%I"
if not "%ValidName%" == "%file%" goto PromptName
set "ValidName="
rem If the file name was entered with file extension .bat, remove
rem the file extension, except the entered string is just .bat.
for %%I in ("%file%") do if /I "%%~xI" == ".bat" if not "%%~nI" == "" set "file=%%~nI"
if not exist "%BatchPath%Locations\%file%.bat" call "%BatchPath%Locations\make.bat"
set "x=12"
set "y=15"
:level
set "loc=x%x%y%y%"
set "x%x%y%y%=a"
call "%BatchPath%Locations\%file%.bat"
if "%loc%" == "a" goto Begin
if "%loc%" == "0" echo Entered shop& pause & goto %dire%
if "%loc%" == "~" echo You are drowning& pause
cls
echo/
call :map
echo/
echo %loc%
setlocal EnableDelayedExpansion
set "Char=!x%x%y%y%!"
endlocal & if "%Char%" == "#" (echo Equal) else (echo Not Equal)
echo q w e a s d ///// %let%
echo/
set "loc=x%x%x%y%"
%SystemRoot%\System32\choice.exe /C QWEASD /N >nul
if errorlevel 6 goto RIGHT
if errorlevel 5 goto DOWN
if errorlevel 4 goto LEFT
if errorlevel 3 goto savenuma
if errorlevel 2 goto UP
if errorlevel 1 goto savenum
goto boucle
:map
%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%
echo %c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%
echo %c0c0%%x24y1%%x24y2%%x24y3%%x24y4%%x24y5%%x24y6%%x24y7%%x24y8%%x24y9%%x24y10%%x24y11%%x24y12%%x24y13%%x24y14%%x24y15%%x24y16%%x24y17%%x24y18%%x24y19%%x24y20%%x24y21%%x24y22%%x24y23%%x24y24%%x24y25%%x24y26%%x24y27%%x24y28%%x24y29%%x24y30%%c0c0%
echo %c0c0%%x23y1%%x23y2%%x23y3%%x23y4%%x23y5%%x23y6%%x23y7%%x23y8%%x23y9%%x23y10%%x23y11%%x23y12%%x23y13%%x23y14%%x23y15%%x23y16%%x23y17%%x23y18%%x23y19%%x23y20%%x23y21%%x23y22%%x23y23%%x23y24%%x23y25%%x23y26%%x23y27%%x23y28%%x23y29%%x23y30%%c0c0%
echo %c0c0%%x22y1%%x22y2%%x22y3%%x22y4%%x22y5%%x22y6%%x22y7%%x22y8%%x22y9%%x22y10%%x22y11%%x22y12%%x22y13%%x22y14%%x22y15%%x22y16%%x22y17%%x22y18%%x22y19%%x22y20%%x22y21%%x22y22%%x22y23%%x22y24%%x22y25%%x22y26%%x22y27%%x22y28%%x22y29%%x22y30%%c0c0%
echo %c0c0%%x21y1%%x21y2%%x21y3%%x21y4%%x21y5%%x21y6%%x21y7%%x21y8%%x21y9%%x21y10%%x21y11%%x21y12%%x21y13%%x21y14%%x21y15%%x21y16%%x21y17%%x21y18%%x21y19%%x21y20%%x21y21%%x21y22%%x21y23%%x21y24%%x21y25%%x21y26%%x21y27%%x21y28%%x21y29%%x21y30%%c0c0%
echo %c0c0%%x20y1%%x20y2%%x20y3%%x20y4%%x20y5%%x20y6%%x20y7%%x20y8%%x20y9%%x20y10%%x20y11%%x20y12%%x20y13%%x20y14%%x20y15%%x20y16%%x20y17%%x20y18%%x20y19%%x20y20%%x20y21%%x20y22%%x20y23%%x20y24%%x20y25%%x20y26%%x20y27%%x20y28%%x20y29%%x20y30%%c0c0%
echo %c0c0%%x19y1%%x19y2%%x19y3%%x19y4%%x19y5%%x19y6%%x19y7%%x19y8%%x19y9%%x19y10%%x19y11%%x19y12%%x19y13%%x19y14%%x19y15%%x19y16%%x19y17%%x19y18%%x19y19%%x19y20%%x19y21%%x19y22%%x19y23%%x19y24%%x19y25%%x19y26%%x19y27%%x19y28%%x19y29%%x19y30%%c0c0%
echo %c0c0%%x18y1%%x18y2%%x18y3%%x18y4%%x18y5%%x18y6%%x18y7%%x18y8%%x18y9%%x18y10%%x18y11%%x18y12%%x18y13%%x18y14%%x18y15%%x18y16%%x18y17%%x18y18%%x18y19%%x18y20%%x18y21%%x18y22%%x18y23%%x18y24%%x18y25%%x18y26%%x18y27%%x18y28%%x18y29%%x18y30%%c0c0%
echo %c0c0%%x17y1%%x17y2%%x17y3%%x17y4%%x17y5%%x17y6%%x17y7%%x17y8%%x17y9%%x17y10%%x17y11%%x17y12%%x17y13%%x17y14%%x17y15%%x17y16%%x17y17%%x17y18%%x17y19%%x17y20%%x17y21%%x17y22%%x17y23%%x17y24%%x17y25%%x17y26%%x17y27%%x17y28%%x17y29%%x17y30%%c0c0%
echo %c0c0%%x16y1%%x16y2%%x16y3%%x16y4%%x16y5%%x16y6%%x16y7%%x16y8%%x16y9%%x16y10%%x16y11%%x16y12%%x16y13%%x16y14%%x16y15%%x16y16%%x16y17%%x16y18%%x16y19%%x16y20%%x16y21%%x16y22%%x16y23%%x16y24%%x16y25%%x16y26%%x16y27%%x16y28%%x16y29%%x16y30%%c0c0%
echo %c0c0%%x15y1%%x15y2%%x15y3%%x15y4%%x15y5%%x15y6%%x15y7%%x15y8%%x15y9%%x15y10%%x15y11%%x15y12%%x15y13%%x15y14%%x15y15%%x15y16%%x15y17%%x15y18%%x15y19%%x15y20%%x15y21%%x15y22%%x15y23%%x15y24%%x15y25%%x15y26%%x15y27%%x15y28%%x15y29%%x15y30%%c0c0%
echo %c0c0%%x14y1%%x14y2%%x14y3%%x14y4%%x14y5%%x14y6%%x14y7%%x14y8%%x14y9%%x14y10%%x14y11%%x14y12%%x14y13%%x14y14%%x14y15%%x14y16%%x14y17%%x14y18%%x14y19%%x14y20%%x14y21%%x14y22%%x14y23%%x14y24%%x14y25%%x14y26%%x14y27%%x14y28%%x14y29%%x14y30%%c0c0%
echo %c0c0%%x13y1%%x13y2%%x13y3%%x13y4%%x13y5%%x13y6%%x13y7%%x13y8%%x13y9%%x13y10%%x13y11%%x13y12%%x13y13%%x13y14%%x13y15%%x13y16%%x13y17%%x13y18%%x13y19%%x13y20%%x13y21%%x13y22%%x13y23%%x13y24%%x13y25%%x13y26%%x13y27%%x13y28%%x13y29%%x13y30%%c0c0%
echo %c0c0%%x12y1%%x12y2%%x12y3%%x12y4%%x12y5%%x12y6%%x12y7%%x12y8%%x12y9%%x12y10%%x12y11%%x12y12%%x12y13%%x12y14%%x12y15%%x12y16%%x12y17%%x12y18%%x12y19%%x12y20%%x12y21%%x12y22%%x12y23%%x12y24%%x12y25%%x12y26%%x12y27%%x12y28%%x12y29%%x12y30%%c0c0%
echo %c0c0%%x11y1%%x11y2%%x11y3%%x11y4%%x11y5%%x11y6%%x11y7%%x11y8%%x11y9%%x11y10%%x11y11%%x11y12%%x11y13%%x11y14%%x11y15%%x11y16%%x11y17%%x11y18%%x11y19%%x11y20%%x11y21%%x11y22%%x11y23%%x11y24%%x11y25%%x11y26%%x11y27%%x11y28%%x11y29%%x11y30%%c0c0%
echo %c0c0%%x10y1%%x10y2%%x10y3%%x10y4%%x10y5%%x10y6%%x10y7%%x10y8%%x10y9%%x10y10%%x10y11%%x10y12%%x10y13%%x10y14%%x10y15%%x10y16%%x10y17%%x10y18%%x10y19%%x10y20%%x10y21%%x10y22%%x10y23%%x10y24%%x10y25%%x10y26%%x10y27%%x10y28%%x10y29%%x10y30%%c0c0%
echo %c0c0%%x9y1%%x9y2%%x9y3%%x9y4%%x9y5%%x9y6%%x9y7%%x9y8%%x9y9%%x9y10%%x9y11%%x9y12%%x9y13%%x9y14%%x9y15%%x9y16%%x9y17%%x9y18%%x9y19%%x9y20%%x9y21%%x9y22%%x9y23%%x9y24%%x9y25%%x9y26%%x9y27%%x9y28%%x9y29%%x9y30%%c0c0%
echo %c0c0%%x8y1%%x8y2%%x8y3%%x8y4%%x8y5%%x8y6%%x8y7%%x8y8%%x8y9%%x8y10%%x8y11%%x8y12%%x8y13%%x8y14%%x8y15%%x8y16%%x8y17%%x8y18%%x8y19%%x8y20%%x8y21%%x8y22%%x8y23%%x8y24%%x8y25%%x8y26%%x8y27%%x8y28%%x8y29%%x8y30%%c0c0%
echo %c0c0%%x7y1%%x7y2%%x7y3%%x7y4%%x7y5%%x7y6%%x7y7%%x7y8%%x7y9%%x7y10%%x7y11%%x7y12%%x7y13%%x7y14%%x7y15%%x7y16%%x7y17%%x7y18%%x7y19%%x7y20%%x7y21%%x7y22%%x7y23%%x7y24%%x7y25%%x7y26%%x7y27%%x7y28%%x7y29%%x7y30%%c0c0%
echo %c0c0%%x6y1%%x6y2%%x6y3%%x6y4%%x6y5%%x6y6%%x6y7%%x6y8%%x6y9%%x6y10%%x6y11%%x6y12%%x6y13%%x6y14%%x6y15%%x6y16%%x6y17%%x6y18%%x6y19%%x6y20%%x6y21%%x6y22%%x6y23%%x6y24%%x6y25%%x6y26%%x6y27%%x6y28%%x6y29%%x6y30%%c0c0%
echo %c0c0%%x5y1%%x5y2%%x5y3%%x5y4%%x5y5%%x5y6%%x5y7%%x5y8%%x5y9%%x5y10%%x5y11%%x5y12%%x5y13%%x5y14%%x5y15%%x5y16%%x5y17%%x5y18%%x5y19%%x5y20%%x5y21%%x5y22%%x5y23%%x5y24%%x5y25%%x5y26%%x5y27%%x5y28%%x5y29%%x5y30%%c0c0%
echo %c0c0%%x4y1%%x4y2%%x4y3%%x4y4%%x4y5%%x4y6%%x4y7%%x4y8%%x4y9%%x4y10%%x4y11%%x4y12%%x4y13%%x4y14%%x4y15%%x4y16%%x4y17%%x4y18%%x4y19%%x4y20%%x4y21%%x4y22%%x4y23%%x4y24%%x4y25%%x4y26%%x4y27%%x4y28%%x4y29%%x4y30%%c0c0%
echo %c0c0%%x3y1%%x3y2%%x3y3%%x3y4%%x3y5%%x3y6%%x3y7%%x3y8%%x3y9%%x3y10%%x3y11%%x3y12%%x3y13%%x3y14%%x3y15%%x3y16%%x3y17%%x3y18%%x3y19%%x3y20%%x3y21%%x3y22%%x3y23%%x3y24%%x3y25%%x3y26%%x3y27%%x3y28%%x3y29%%x3y30%%c0c0%
echo %c0c0%%x2y1%%x2y2%%x2y3%%x2y4%%x2y5%%x2y6%%x2y7%%x2y8%%x2y9%%x2y10%%x2y11%%x2y12%%x2y13%%x2y14%%x2y15%%x2y16%%x2y17%%x2y18%%x2y19%%x2y20%%x2y21%%x2y22%%x2y23%%x2y24%%x2y25%%x2y26%%x2y27%%x2y28%%x2y29%%x2y30%%c0c0%
echo %c0c0%%x1y1%%x1y2%%x1y3%%x1y4%%x1y5%%x1y6%%x1y7%%x1y8%%x1y9%%x1y10%%x1y11%%x1y12%%x1y13%%x1y14%%x1y15%%x1y16%%x1y17%%x1y18%%x1y19%%x1y20%%x1y21%%x1y22%%x1y23%%x1y24%%x1y25%%x1y26%%x1y27%%x1y28%%x1y29%%x1y30%%c0c0%
echo %c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%%c0c0%
goto :EOF
:LEFT
if "%y%" == "1" goto selecmap
set "dire=right"
set /A y-=1
set "x%x%y%y%=a"
goto level
:RIGHT
if "%y%" == "30" goto selecmap
set "dire=left"
set /A y+=1
set "x%x%y%y%=a"
goto level
:UP
if "%x%" == "24" goto selecmap
set "dire=down"
set /A x+=1
set "x%x%y%y%=a"
goto level
:DOWN
if "%x%" == "1" goto selecmap
set "dire=up"
set /A x-=1
set "x%x%y%y%=a"
goto level
:selecmap
goto boucle
ATTENTION: The delimiters are */:<>?\| and a TAB and not three spaces as displayed and copied by the browsers.
The command lines starting with rem can be removed as they explain only the checks applied on the string entered by the user of the batch file.
The most important fix is the replacement of
if '%%!x%x%y%y%!%%' == !#! (echo Equal) else (echo Not Equal)
by
setlocal EnableDelayedExpansion
set "Char=!x%x%y%y%!"
endlocal & if "%Char%" == "#" (echo Equal) else (echo Not Equal)
The improved code first defines a new environment with making a copy of current list of environment variables used in the next two command lines with delayed expansion explicitly enabled while the state of command extensions is kept as is. The command SETLOCAL pushes on stack the current states of command extensions (enabled), delayed expansion (disabled) and current working directory.
The second command line builds dynamically the name of an environment variable during execution. The value of this environment variable is referenced with delayed expansion and assigned to environment variable Char.
The Windows command processor replaces on third command line %Char% by the value assigned to this environment variable in current environment variables list. Then the command ENDLOCAL is executed which deletes the environment variables list created before by SETLOCAL with the environment variable Char, restores the previous environment variables list, pops from stack the state of command extensions and sets the state (still enabled), pops from stack the state of delayed expansion and sets the state (disabled again) and pops from stack the current directory and sets this directory again as current directory which does not change anything here.
Next is executed the command IF which makes the string comparison whereby the left string does not contain any more %Char%, but instead the character determined before in previous environment with enabled delayed expansion.
The other improvements are:
The execution environment required for this batch file is defined completely at top with the first two command lines which is command echo mode turned off, command extensions enabled and delayed expansion disabled. So the batch file processing does no longer depend on environment settings defined outside of the batch file.
The full path of the batch file is assigned to an environment variable BatchPath using correct %~dp0 with % just at beginning of the argument reference and no one at the end. %~dp0 references the drive and path of argument 0 which is currently processed the batch file. The syntax for referencing argument string without or with a modifier is explained by help output on running call /? in a command prompt window. The batch file path always ends with a backslash. Therefore the concatenation of %~dp0 with a file/folder name or wildcard pattern should be done always without an additional backslash.
On all command lines (re)defining an environment variable the entire argument string of command SET is enclosed in double quotes. The reason is explained in detail by my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line?
The recommended syntax for an arithmetic expression is used on all command lines with command SET and option /A as explained by help output on running set /? in a command prompt window. In this case there are (usually) no double quotes needed as trailing spaces/tabs are ignored on evaluation of an arithmetic expression.
The directory of the batch file is output without surround double quotes using delayed expansion to work correct even on batch file path containing one or more &.
It is good practice to enclose argument strings in double quotes to make sure that a space and the characters &()[]{}^=;!'+,`~<>| are interpreted literally my the Windows command processor on parsing a command line. This is especially important for the two strings to compare with an IF and operator ==, especially if one of the two strings is an environment variable reference which can be replaced by nothing on environment variable not defined at all. A string comparison like if %variable% == string could be after processing if == string which is a serious syntax error as string is not a valid operator resulting in an immediate exit of batch file processing by cmd.exe which is never funny for a user using a batch file.
The command IF compares strings always with the surrounding ". Therefore both strings must be enclosed in double quotes on comparing a variable string not containing a double quote with a fixed string. See also my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files for details on how exactly a string comparison is done by IF. The character ' has no special meaning for the Windows command processor cmd.exe in comparison to Linux shell interpreters with the exception of enclosing a set of a for /F loop in '. For that reason only " should be used around two strings to compare and not other characters.
The remaining improvements are explained in details by:
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
Single line with multiple commands using Windows batch file explaining the operators & and &&.
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Problems checking if string is quoted and adding quotes to string
Stephan suggested a more effective way to show the map:
:map
setlocal EnableDelayedExpansion
%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%%xay30%
for /L %%y in (1,1,32) do <nul set /P "=%c0c0%"
echo/
for /L %%x in (24,-1,1) do (
<nul set /P "=%c0c0%"
for /L %%y in (1,1,30) do (
<nul set /p "=!x%%xy%%y!"
)
echo %c0c0%
)
for /L %%y in (1,1,32) do <nul set /P "=%c0c0%"
endlocal
goto :EOF
Thank you, Stephan.

incrementing list variable and then loop on it

in a sh script, I am trying to make a list of filename in a folder, and then loop on it to check if two consecutive filename respond well to "expression criteria".
in a folder I have:
file1.nii
file1_mask.nii
file2.nii
file2_mask.nii
etc ...
undefined number of files. but if filex.nii exists, it must have filex_mask.nii
in a .txt file that the user modify.
it contains:
file1.nii tab some parameter \n
file2.nii tab some parameter \n
etc ...
the script take long hours after to run, and for example, the mask files are used only after few hours.
so I want at the beginning of the .sh to check if filenames are well spelled and if any files in the .txt is present in the folder.
and in case not, stop the .sh and warn the user. not wait hours before noticing the problem.
For now I tried:
test=""
for entry in "${search_dir}"/*
do
echo "$entry"
test="${test} $entry"
done
I have then a string variable with space between filenames, but it has the folder name as well.
./search_dir/file1.nii ./search_dir/file1_mask.nii
I wanted file1.nii file1_mask.nii etc ...
and now I read my .txt file and check if the filename specified in it are in my test variable.
while read -r line
do
set -- $line
stack=$1
check=False
check2=False
for i in $test; do
echo "$stack.nii"
echo "$i"
if "${stack}.nii" == "$i";
then
check=True
fi
if "${stack}_mask.nii"=="$i";
then
check2=True
fi
done
done < "$txt_file"
but it is not working.
"$stack_mask.nii"=="$i"
doesn't seems to be the good way to compare strings
it generates the error:
"file1.nii" not found
Here is my solution for now, based on glenn answer:
errs=0
while read -r line; do
set -- $line
prefix="${1}.nii"
prefix2="${1}.nii.gz"
if [ -e ${PATH}/$prefix2 ]; then
echo "File found: ${PATH}/$prefix2" >&2
elif [ -e ${PATH}/$prefix ]; then
echo "File found: ${PATH}/$prefix" >&2
else
echo "File not found: ${PATH}/$prefix" >&2
errs=$((errs + 1))
fi
prefixmask="${1}_brain_mask.nii"
prefixmask2="${1}_brain_maskefsd.nii.gz"
if [ -e ${PATH}/$prefixmask ]; then
echo "Mask file found for ${PATH}/$prefixmask" >&2
elif [ -e ${PATH}/$prefixmask2 ]; then
echo "Mask file found for ${PATH}/$prefixmask2" >&2
else
echo "Mask file not found: ${PATH}/$prefixmask" >&2
errs=$((errs + 1))
fi
done < "$INPUT"
echo $errs
if [ $errs > 0 ]; then
echo "Errors found"
exit 3
fi
then only problem now is that it always exit, even if errs is equal to 0 and I don't know why ...
I would do this:
errs=0
for f in "$search_dir"/*.mii; do
[[ $f == *_mask.mii ]] && continue # skip the mask files
prefix=${f%.mii} # strip off the extension
if [[ ! -f "${prefix}_mask.mii" ]]; then
echo "Error: $f has no mask file" >&2
((errs++))
fi
done
if [[ $errs -gt 0 ]]; then
echo "Aborting due to errors" >&2
exit 2
fi
That should be pretty efficient, since it just loops through the files once.
Now that we see the input file:
errs=0
while read -r mii_file other_stuff; do
prefix="${mii_file%.mii}"
if [[ ! -f ./"$mii_file" ]]; then # adjust your relative path accordingly
echo "File not found: $mii_file" >&2
((errs++))
elif [[ ! -f ./"${prefix}_mask.mii" ]]; then
echo "Mask file missing for $mii_file" >&2
((errs++))
fi
done < "$txt_file"
if (( errs > 0 )); then
echo "Errors found"
exit 2
fi

How to check if string contains characters in regex pattern in shell?

How do I check if a variable contains characters (regex) other than 0-9a-z and - in pure bash?
I need a conditional check. If the string contains characters other than the accepted characters above simply exit 1.
One way of doing it is using the grep command, like this:
grep -qv "[^0-9a-z-]" <<< $STRING
Then you ask for the grep returned value with the following:
if [ ! $? -eq 0 ]; then
echo "Wrong string"
exit 1
fi
As #mpapis pointed out, you can simplify the above expression it to:
grep -qv "[^0-9a-z-]" <<< $STRING || exit 1
Also you can use the bash =~ operator, like this:
if [[ ! "$STRING" =~ [^0-9a-z-] ]] ; then
echo "Valid";
else
echo "Not valid";
fi
case has support for matching:
case "$string" in
(+(-[[:alnum:]-])) true ;;
(*) exit 1 ;;
esac
the format is not pure regexp, but it works faster then separate process with grep - which is important if you would have multiple checks.
Using Bash's substitution engine to test if $foo contains $bar
bar='[^0-9a-z-]'
if [ -n "$foo" -a -z "${foo/*$bar*}" ] ; then
echo exit 1
fi

why is my batch if statement not working?

I have been having trouble with this code for a while. Please take a look and tell me what is wrong so i can fix it. This code will be a part of an xprize competion!
echo [Math]
echo.
echo.
echo.
echo Question 1
echo.
echo.
echo.
echo If Mary goes to the market and buys three
echo bananas for two dollars each, how much
echo did she spend altogether?
echo.
echo.
echo (A) $3
echo (B) $6
echo (C) $5
echo (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results
:results
set /a "COUNTER=0"
here is what i cant figure out:
i dont know how to change a value of a variable in an if statement
if %ANSWER01%==b(
set /a "COUNTER=COUNTER+1"
)
echo %COUNTER%
pause
You should use the choice command with:
Choice /c abcd /m "Answer: "
set ANSWER01=%errorlevel%
also for your if statement just do it as so:
if %ANSWER01% equ 2 set /a "COUNTER=COUNTER+1"
That would be easier and better.
Note: the equ 2 is if you use the choice command
You need a space between the b and the (
AAMOI, if /i will make the match case-insensitive.
Hi, do need enable a delayed expansion option and give an space to if sentence to a parenthesis:
Your code is something like:
set /a counter=0
setlocal ENABLEDELAYEDEXPANSION
echo [Math]
echo.
echo.
echo.
echo Question 1
echo.
echo.
echo.
echo If Mary goes to the market and buys three
echo bananas for two dollars each, how much
echo did she spend altogether?
echo.
echo.
echo (A) $3
echo (B) $6
echo (C) $5
echo (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results
:results
if %ANSWER01%==b (
set /a counter=counter+1
)
echo %counter%
pause
endlocal

Bash: One-liner to exit with the opposite status of a grep command?

How can I reduce the following bash script?
grep -P "STATUS: (?!Perfect)" recess.txt && exit 1
exit 0
It seems like I should be able to do it with a single command, but I have a total of 3 here.
My program should:
Read recess.txt
Exit 1 (or non-zero) if it contains a line with "STATUS: " of NOT "Perfect"
Exit 0 if no such line exists (i.e. all "STATUS: " lines are "Perfect")
The answer award goes to the tightest script. Thanks!
Example files
Program should have exit status 0 for this file:
FILE: styles.css
STATUS: Perfect!
FILE: contour-styles.css
STATUS: Perfect!
Program should have exit status 1 (or non-zero) for this file:
FILE: styles.css
STATUS: Perfect!
FILE: contour-styles.css
STATUS: Busted
FAILURES: 1 failure
Id's should not be styled
1. #asdf
Just negate the return value.
! grep -P "STATUS: (?!Perfect)" recess.txt
I came across this, needing an onlyif statement for Puppet. As such, Tgr's bash solution wouldn't work, and I didn't want to expand the complexity as in Christopher Neylan's answer.
I ended up using a version inspired by Henri Schomäcker's answer, but notably simplified:
grep -P "STATUS: (?!Perfect)" recess.txt; test $? -eq 1
Which very simply inverts the exit code, returning success only if the text is not found:
If grep returns 0 (match found), test 0 -eq 1 will return 1.
If grep returns 1 (no match found), test 1 -eq 1 will return 0.
If grep returns 2 (error), test 2 -eq 1 will return 1.
Which is exactly what I wanted: return 0 if no match is found, and 1 otherwise.
if anyone gets here looking for a bash return code manipulation:
(grep <search> <files> || exit 0 && exit 123;)
this will return 0 (success) when grep finds nothing, and return 123 (failure) when it does. The parenthesis are in case anyone test it as is on the shell prompt. with parenthesis it will not logout on the exit, but just exit the subshell with the same error code.
i use it for a quick syntax check on js files:
find src/js/ -name \*js -exec node \{\} \; 2>&1 | grep -B 5 SyntaxError || exit 0 && exit 1;
To make it work with set -e surround it in a sub-shell with ( and ):
$ cat test.sh
#!/bin/bash
set -ex
(! ls /tmp/dne)
echo Success
$ ./test.sh
+ ls /tmp/dne
ls: cannot access /tmp/dne: No such file or directory
+ echo Success
Success
$ mkdir /tmp/dne
$ ./test.sh
+ ls /tmp/dne
$
Just negating the return value doesn't work in a set -e context. But you can do:
! grep -P "STATUS: (?!Perfect)" recess.txt || false
You actually don't need to use exit at all. Logically, no matter what the result of grep, your script is going to exit anyway. Since the exit value of a shell script is the exit code of the last command that was run, just have grep run as the last command, using the -v option to invert the match to correct the exit value. Thus, your script can reduce to just:
grep -vqP "STATUS: (?!Perfect)" recess.txt
EDIT:
Sorry, the above does not work when there are other types of lines in the file. In the interest of avoiding running multiple commands though, awk can accomplish the entire shebang with something like:
awk '/STATUS: / && ! /Perfect/{exit 1}' recess.txt
If you decide you want the output that grep would have provided, you can do:
awk '/^STATUS: / && ! /Perfect/{print;ec=1} END{exit ec}' recess.txt
Use the special ? variable:
grep -P "STATUS: (?!Perfect)" recess.txt
exit $((1-$?))
(But note that grep may also return 2, so it's not clear what you'd want to occur in such cases.)
The problem with the grep answers is that if the file is empty you also get a clean response, as if the file had a perfect.
So personally I gave up on grep for this and used awk.
awk 'BEGIN{ef=2}; /STATUS: Perfect/{ ef=0;}; /STATUS: Busted/{ print;eff=3;}; END{exit (ef+eff)}' a.txt ; echo $?
This has exit status:
0 : Perfect and !Busted
2 : !Perfect and Busted
3 : Perfect and Busted
5 : !Perfect and !Busted
[ $(grep -c -P "STATUS: (?!Perfect)" recess.txt) -eq 0 ]
I also needed such a solution for writing puppet only if statements and came up with the following command:
/bin/grep --quiet 'root: root#ourmasterdomain.de' /etc/aliases; if [ $? -eq 0 ]; then test 1 -eq 2; else test 1 -eq 1; fi;
Since someone already posted a Puppet solution, I might as well add how to invert a shell command run by Ansible:
- name: Check logs for errors
command: grep ERROR /var/log/cassandra/system.log
register: log_errors
failed_when: "log_errors.rc == 0"
I.e. you just set the failed condition to the return code being 0. So this command fails if we do find the word ERROR in our logs.
I chose this rather than grep -v as that also inverts grep's output, so we would receive all DEBUG/INFO/WARN lines in log_errors.stdout_lines which we do not want.