Batch - if statements and negative values - batch-file

I am trying to work with negative numbers, but obviosly I am doing something stupid.
The attached script will always display:
testA = 15
testB = -15
testA GTR 0
testA GEQ 0
testB GTR 0
testB GEQ 0
Until now i was under the impression that -15 is <= 0.
What am I doing wrong?
Batch script:
#echo off
set /a testA = 15
set /a testB = -15
echo testA = %testA%
echo testB = %testB%
echo(
if %testA% GTR "0" echo testA GTR 0
if %testA% GEQ "0" echo testA GEQ 0
if %testA% LEQ "0" echo testA LEQ 0
if %testA% LSS "0" echo testA LSS 0
echo(
if %testB% GTR "0" echo testB GTR 0
if %testB% GEQ "0" echo testB GEQ 0
if %testB% LEQ "0" echo testB LEQ 0
if %testB% LSS "0" echo testB LSS 0
echo(
pause

You are indeed right. The Association of Mathematical Bods has not enacted any recent changes to the laws governing sequencing of integers, so your problem must lay elsewhere :-)
In fact, it's the " quote marks around your 0 values that is causing this problem. Get rid of them. When you do so, you get the rather more sensible output of:
testA = 15
testB = -15
testA GTR 0
testA GEQ 0
testB LEQ 0
testB LSS 0
The reason your "0" case doesn't work if because gtr and associated operators are actually string comparisons rather than numeric ones. The reason they work for numerics is buried deep within the help for the if statement:
These comparisons are generic, in that if both string1 and string2 are both comprised of all numeric digits, then the strings are converted to numbers and a numeric comparison is performed.
Since " can in no way be considered a "numeric digit", the comparison performed was a string-based one and, in ASCII, " (code point 0x22) is less than any digit (0x30 through 0x39) or the - sign (0x2d) in the case of testB.

Related

Batch file to check IP address, and set location based octet

I've pieced together a batch file based on info I've found here. Oddly, an earlier version appeared to work correctly at home, once I took it to work, it didn't. And all my modifications are failing to fix it. My boss and I are stuck.
#echo off
setlocal
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do (
for /f "tokens=1-4 delims=. " %%a in ("%%f") do (
set octetA=%%a set octetB=%%b set octetC=%%c set octetD=%%d
if %octetB% equ 10 goto :setschool
)
)
:setschool
if %octetC% geq 0 if %octetC% leq 3 set school=DIC
if %octetC% geq 16 if %octetC% leq 19 set school=AT
if %octetC% geq 48 if %octetC% leq 51 set school=BE
if %octetC% geq 64 if %octetC% leq 67 set school=BH
if %octetC% geq 80 if %octetC% leq 83 set school=CN
if %octetC% geq 112 if %octetC% leq 115 set school=LC
if %octetC% geq 128 if %octetC% leq 131 set school=LX
if %octetC% geq 144 if %octetC% leq 147 set school=RG
if %octetC% geq 160 if %octetC% leq 163 set school=UN
if %octetC% geq 176 if %octetC% leq 179 set school=WA
if %octetC% geq 192 if %octetC% leq 195 set school=WI
if %octetC% geq 208 if %octetC% leq 211 set school=BOE
echo %school%
goto :eof
It errors out at the first 'if', because octetB hasn't been set to anything yet. Yet the code just before it should have set it. All of our wired DHCP addresses start with 10.10., which is why it's checking for 10, which should mean it ignores our wireless, auto configed IPs, and virtual nics. If I manually run the lines before the 'if', it produces what I expect, with the octets being set correctly.
I suspect that the output of ipconfig is different at home and at work. Do you run the same operating systems on them? With the same language setting? Do the ipconfig outputs on both machines contain a IPv4 Address line when checked manually?
But I cannot tell anything without knowing the "earlier version that worked" you mentioned.
You can try using a simpler search String that is common on both machines, like IPv4. And maybe you want to add the /I switch to the findStr call to set it to "ignore case".
Try checking for equality with if "%%b" == "10" goto setschool (remove the colon, replace %octetB%). I think %octetB% is only available inside the for loop if you work with delayed expansion of variables.
You need to put each set command on its own line inside the for loop.
With these changes it works for me at least under Win10.
This is how I find which IP subnet our machines are using:
ipconfig^|findstr /i /c:"ip address"^|findstr /c:"10.11.1"&&set loc=AAA&&goto :continue
ipconfig^|findstr /i /c:"ipv4 address"^|findstr /c:"10.11.1"&&set loc=AAA&&goto ipconfig^|findstr /i /c:"ip address"^|findstr /c:"192.168.4."&&set loc=BBB&&goto :continue
:continue
ipconfig^|findstr /i /c:"ipv4 address"^|findstr /c:"192.168.4."&&set loc=BBB&&goto :continue
set loc=unknown
:continue
I think "ip address" is from WinXP or Win7.
You need delayed expansion.
untested
#echo off
setlocal enabledelayedexpansion
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do (
for /f "tokens=1-4 delims=. " %%a in ("%%f") do (
set octetA=%%a & set octetB=%%b & set octetC=%%c & set octetD=%%d
if "!octetB!" equ "10" goto :setschool
)
)
Here's a Remarked batch file example, showing the fix for your issues, as per my initial comment:
#Echo Off
Rem Undefine any existing octet variables.
For /F "Delims==" %%G In ('Set octet 2^>NUL') Do Set "%%G="
Rem Retrieve the IPv4 string from the IPConfig command.
Rem And set each octet to individual variables.
For /F "Tokens=2 Delims=:" %%G In (
'"%__AppDir__%ipconfig.exe 2>NUL | %__Appdir__%find.exe "IPv4""'
) Do For /F "Tokens=1-4 Delims=. " %%H In ("%%G"
) Do Set "octet1=%%H" & Set "octet2=%%I" & Set "octet3=%%J" & Set "octet4=%%K"
Rem Check to see if any octet variables are defined, i.e. IPv4 found.
Rem If not print message, wait for response and end the script.
Set octet >NUL 2>&1 || (Echo IPConfig failed to retrieve an IP address.
Pause & GoTo :EOF)
Rem From here you can make your comparisons:
Rem First ensure that a variable named school is not defined.
Set "school="
Rem Check to see if your second octet was 10.
Rem Then make your comparisons using the value of the third octet.
If %octet2% Equ 10 If Defined octet3 (
If %octet3% GEq 0 If %octet3% LEq 3 Set "school=DIC"
If %octet3% GEq 16 If %octet3% LEq 19 Set "school=AT"
If %octet3% GEq 48 If %octet3% LEq 51 Set "school=BE"
If %octet3% GEq 64 If %octet3% LEq 67 Set "school=BH"
If %octet3% GEq 80 If %octet3% LEq 83 Set "school=CN"
If %octet3% GEq 112 If %octet3% LEq 115 Set "school=LC"
If %octet3% GEq 128 If %octet3% LEq 131 Set "school=LX"
If %octet3% GEq 144 If %octet3% LEq 147 Set "school=RG"
If %octet3% GEq 160 If %octet3% LEq 163 Set "school=UN"
If %octet3% GEq 176 If %octet3% LEq 179 Set "school=WA"
If %octet3% GEq 192 If %octet3% LEq 195 Set "school=WI"
If %octet3% GEq 208 If %octet3% LEq 211 Set "school=BOE")
Rem As your comparisons do not currently cover all possible octet values,
Rem This is a check to see if the school variable was actually defined.
Rem If not print message, otherwise print the content of the school variable.
If Not Defined school (Echo The school variable was not defined.
Echo Reason: & If %octet2% Neq 10 (Echo The second octet was not 10.
) Else If Defined octet3 (Echo The third octet failed the comparisons.
) Else Echo Your returned IP address was corrupted.) Else Echo %school%
Rem Wait to ensure that any messages can be read, if not run from cmd.exe.
Pause
Rem End script.
GoTO :EOF
The script includes some additions designed to provide reasonable feedback if errors occur.
Feel free to use this unRemarked version for completion or in your production environment.
#Echo Off
For /F "Delims==" %%G In ('Set octet 2^>NUL')Do Set "%%G="
For /F "Tokens=2 Delims=:" %%G In (
'"%__AppDir__%ipconfig.exe 2>NUL|%__Appdir__%find.exe "IPv4""'
)Do For /F "Tokens=1-4Delims=. " %%H In ("%%G"
)Do Set "octet1=%%H"&Set "octet2=%%I"&Set "octet3=%%J"&Set "octet4=%%K"
Set octet>NUL 2>&1||(Echo IPConfig failed to retrieve an IP address.
Pause&GoTo :EOF)
Set "school="
If %octet2% Equ 10 If Defined octet3 (
If %octet3% GEq 0 If %octet3% LEq 3 Set "school=DIC"
If %octet3% GEq 16 If %octet3% LEq 19 Set "school=AT"
If %octet3% GEq 48 If %octet3% LEq 51 Set "school=BE"
If %octet3% GEq 64 If %octet3% LEq 67 Set "school=BH"
If %octet3% GEq 80 If %octet3% LEq 83 Set "school=CN"
If %octet3% GEq 112 If %octet3% LEq 115 Set "school=LC"
If %octet3% GEq 128 If %octet3% LEq 131 Set "school=LX"
If %octet3% GEq 144 If %octet3% LEq 147 Set "school=RG"
If %octet3% GEq 160 If %octet3% LEq 163 Set "school=UN"
If %octet3% GEq 176 If %octet3% LEq 179 Set "school=WA"
If %octet3% GEq 192 If %octet3% LEq 195 Set "school=WI"
If %octet3% GEq 208 If %octet3% LEq 211 Set "school=BOE")
If Not Defined school (Echo The school variable was not defined.
Echo Reason:&If %octet2% Neq 10 (Echo The second octet was not 10.
)Else If Defined octet3 (Echo The third octet failed the comparisons.
)Else Echo Your returned IP address was corrupted.)Else Echo %school%
Pause
GoTO :EOF

Is it possible to make a randomly generated variable in batch? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Currently I am making a game in batch that has randomly generated worlds. I'm currently working on biome generation, and I'm trying to figure out a way to have each variable generated, so the variables will look like %biomenum[0,0]%, %biomenum[0,1]%, and going maybe as far as %biomenum[512,512]% and as low as %biomenum[-512,-512]
The code that I have currently generates the biome for 9 different chunks, but it doesn't make the variables itself, and typing this out for each chunk between -512, -512 and 512, 512 doesn't seem all that plausible
rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem 0x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0x1y = biomenum0x0y - 1
rem 1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x1y = biomenum0x0y - 1
rem 1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1x0y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1x0y = biomenum0x0y - 1
rem 1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum1xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum1xneg1y = biomenum0x0y - 1
rem 0x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenum0xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenum0xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenum0xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenum0xneg1y = biomenum0x0y - 1
rem -1x -1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1xneg1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1xneg1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1xneg1y = biomenum0x0y - 1
rem -1x 0y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x0y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x0y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x0y = biomenum0x0y - 1
rem -1x 1y
set /A nextbiomenum = %RANDOM% * 4 / 32768 + 1
if /I "%nextbiomenum%" EQU "1" set /A biomenumneg1x1y = biomenum0x0y + 1
if /I "%nextbiomenum%" EQU "2" set /A biomenumneg1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "3" set /A biomenumneg1x1y = biomenum0x0y + 0
if /I "%nextbiomenum%" EQU "4" set /A biomenumneg1x1y = biomenum0x0y - 1
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1
How would I be able to automate the creation of variables, so I don't have to type out all 1,048,576 chunks?
*Note: This question was edited so it made more sense. The original was a very long explanation that I made on 3 hours of sleep, and I apologize for that. Hopefully this is a bit more easy to understand.
Your explanation is pretty confusing, both in what you want to get and the way to obtain it...
When you have a series of variables with the same name and you want to select one variable based on the value of another variable (called index), then the name for such a data structure is array. If the array elements are selected by just one index variable (one dimensional array), then it is usually called vector. If the array elements are selected by two or more indices (multi-dimensional array), then it is called matrix.
In computer programs the usual way to repeat the same code over a range of values that are all processed in the same way is via the FOR statement/construct/command. The FOR control variable is placed in the code in the place of the varying value. The usual way to process a series of array elements is via a FOR command and place its control variable as the index of the array element.
The way to process array elements in Batch file programs is described at this answer.
The next Batch file obtain a result equivalent than your example code, but using a two-dimensional array and two nested FOR commands:
#echo off
setlocal EnableDelayedExpansion
rem 0x 0y
set /A biomenum0x0y = %RANDOM% * 16 / 32768 + 1
rem The rest!
for %%x in (neg1 0 1) do (
for %%y in (neg1 0 1) do (
set /A nextbiomenum = !RANDOM! * 4 / 32768 - 1, biomenum%%xx%%yy = biomenum0x0y + nextbiomenum %% 2
)
)
echo %biomenumneg1xneg1y% is the biome for -1,-1
echo %biomenumneg1x0y% is the biome for -1, 0
echo %biomenumneg1x1y% is the biome for -1, 1
echo %biomenum0xneg1y% is the biome for 0, -1
echo %biomenum0x0y% is the biome for 0, 0 -the starting chunk-
echo %biomenum0x1y% is the biome for 0, 1
echo %biomenum1xneg1y% is the biome for 1, -1
echo %biomenum1x0y% is the biome for 1, 0
echo %biomenum1x1y% is the biome for 1, 1
The results obtained by this code are not exactly the same than your code. In your code you add 1 when random is 1 and subtract 1 when random is 4 (and ignore 2 and 3). In my simplification I generate a number between -1 and 2 (instead of 1 to 4) and add 1 when random is 1 and subtract 1 when random is -1 (and ignore 0 and 2); this is done taking the remainder (mod, % operator) of the number divided by 2. The statistical result is, however, the same...
Antonio
PS - I suggest you to use the standard array notation with the subscripts enclosed in square braquets, (like %biomenum[1,0]% or %biomenum[1][0]%instead of %biomenum1x0y% ) for the reasons explained at this post.

Batch - Find Out Permutation

I am new to Batch and I would like to know if I can find out all combinations of numbers in order.
In this case I have 49 Numbers from 1 - 49 , and I have to pick 6 Numbers to be the results.
For example:
1 2 3 4 5 6
1 2 3 4 5 7
...
1 2 3 4 5 49
1 2 3 4 6 7
1 2 3 4 6 8
etc...
This is my old code:
#echo off > NEWFILE & setLocal EnableDelayedExpansion
set a=44
set b=45
set c=46
set d=47
set e=48
set f=49
for /L %%a in (1 1 !a!) do (
for /L %%b in (2 1 !b!) do (
for /L %%c in (3 1 !c!) do (
for /L %%d in (4 1 !d!) do (
for /L %%e in (5 1 !e!) do (
for /L %%f in (6 1 !f!) do (
echo.%%a %%b %%c %%d %%e %%f
))))))) >> NEWFILE
goto :EOF
However it returns:
1 2 3 4 5 6
1 2 3 4 5 7
...
1 2 3 4 5 49
1 2 3 4 6 6
Two 6's appeared.
I don't seem to be able to fix it, please help, thanks very much!
When you post a question you should post your efforts to solve it, describe the method used and the problems you had; otherwise you may get similar answers with no explanations at all, like this one:
EDIT: As users dbenham and aschipfl indicated, my original code have a small bug: the set /A i=M-1 line should be placed after the :nextSet label. This is the right code:
#echo off
setlocal EnableDelayedExpansion
set "N=%1"
set "M=%2"
set "line="
for /L %%i in (1,1,%M%) do (
set "C[%%i]=%%i"
set "line=!line! ^!C[%%i]^!"
)
:nextSet
set /A i=M-1
for /L %%j in (!C[%M%]!,1,%N%) do (
set "C[%M%]=%%j"
echo %line%
)
:nextPos
set "C=!C[%i%]!"
if %C% equ %N% (
set /A i-=1
if !i! equ 0 goto :EOF
goto nextPos
)
for /L %%i in (%i%,1,%M%) do (
set /A C+=1,C[%%i]=C
)
if !C[%M%]! gtr %N% goto nextPos
goto nextSet
Obviously, the corrected code generate a much larger number of results and this version is particularly slow... :(
The new version below use the exact same code of dbenham's solution; its only advantage is that you may change the parameters used to generate the result in a very easy way:
#echo off
setlocal EnableDelayedExpansion
set "N=%1"
set "M=%2"
set /A j=N-M, prev=0
set "for=" & set "line=" & set "endfor="
for /L %%i in (1,1,%M%) do (
set /A j+=1
set "for=!for! set /A start=!prev!+1 & for /L %%%%i in (^!start^!,1,!j!) do ("
set "line=!line! %%%%i"
set "endfor=!endfor!)"
set "prev=%%%%i"
)
REM ECHO !FOR! echo !LINE! %ENDFOR%
%for% echo %line% %endfor%
Output example:
C:\> test.bat 6 4
1 2 3 4
1 2 3 5
1 2 3 6
1 2 4 5
1 2 4 6
1 2 5 6
1 3 4 5
1 3 4 6
1 3 5 6
1 4 5 6
2 3 4 5
2 3 4 6
2 3 5 6
2 4 5 6
3 4 5 6
To get your results, use: test.bat 49 6
2ND EDIT: Faster method added
When the problem to solve is the excessive time a process takes, an obvious alternative is to use a faster programming language. The solution below use JScript, that is somewhat similar to Batch file programming:
#if (#CodeSection == #Batch) #then
#echo off
echo Start: %time%
cscript //nologo //E:JScript "%~F0" > result.txt
echo End: %time%
goto :EOF
#end
// JScript code section
for ( var A=1; A <= 44; ++A ) {
for ( var B=A+1; B <= 45; ++B ) {
for ( var C=B+1; C <= 46; ++C ) {
for ( var D=C+1; D <= 47; ++D ) {
for ( var E=D+1; E <= 48; ++E ) {
for ( var F=E+1; F <= 49; ++F ) {
WScript.Echo(A,B,C,D,E,F);
}
}
}
}
}
}
This is a Batch-JScript hybrid script; save it with .BAT extension. This program took a little less than 9 minutes in my cheap-and-slow lap-top computer to generate a 239 MB file with 13983816 lines.
The problem is compute intensive, given that there are 13,983,816 unique permutations. (See https://en.wikipedia.org/wiki/Lottery_mathematics#Calculation_explained_in_choosing_6_from_49.)
The Rojo answer should work, but the GOTO and repetitive FOR /F parsing and IF logic will slow things down considerably.
The code is much faster if you use nested FOR /L loops.
#echo off
setlocal enableDelayedExpansion
for /l %%A in (1 1 44) do (
set /a start=%%A+1
for /l %%B in (!start! 1 45) do (
set /a start=%%B+1
for /l %%C in (!start! 1 46) do (
set /a start=%%C+1
for /l %%D in (!start! 1 47) do (
set /a start=%%D+1
for /l %%E in (!start! 1 48) do (
set /a start=%%E+1
for /l %%F in (!start! 1 49) do (
echo %%A %%B %%C %%D %%E %%F
)
)
)
)
)
)
This will still be unbearably slow to let this script print the results to the screen. I estimate it will take 1.25 hours on my machine. Redirecting the output to a file is about 5 times faster, around 15 minutes.
In the future, please show some code demonstrating that you've attempted to solve the problem yourself, showing where you got stuck, where the output is not as expected, etc. Questions resembling "Here are my requirements. Code this for me" generally aren't well-received around here. How you got an upvote without showing any code is beyond me, but c'est la vie.
In this instance, I found the problem interesting, so I thought I'd go ahead and get you started. Challenge: accepted. Here's one way to do it.
#echo off
setlocal enabledelayedexpansion
set "series=1 2 3 4 5 6"
:loop
echo %series%
if "%series%"=="44 45 46 47 48 49" goto :EOF
for /f "tokens=1-6" %%a in ("%series%") do (
set /a i1=%%a, i2=%%b, i3=%%c, i4=%%d, i5=%%e, i6=%%f+1
if !i6! gtr 49 set /a i5+=1, i6=i5+1
if !i5! gtr 48 set /a i4+=1, i5=i4+1, i6=i5+1
if !i4! gtr 47 set /a i3+=1, i4=i3+1, i5=i4+1, i6=i5+1
if !i3! gtr 46 set /a i2+=1, i3=i2+1, i4=i3+1, i5=i4+1, i6=i5+1
if !i2! gtr 45 set /a i1+=1, i2=i1+1, i3=i2+1, i4=i3+1, i5=i4+1, i6=i5+1
set "series=!i1! !i2! !i3! !i4! !i5! !i6!"
)
goto loop
Here's another solution that should be more efficient.
#echo off
setlocal enabledelayedexpansion
set "series=1 2 3 4 5 6"
set total=0
for /L %%a in (1,1,44) do (
set /a i2 = %%a + 1
for /L %%b in (!i2!, 1, 45) do (
set /a i3 = %%b + 1
for /L %%c in (!i3!, 1, 46) do (
set /a i4 = %%c + 1
for /L %%d in (!i4!, 1, 47) do (
set /a i5 = %%d + 1
for /L %%e in (!i5!, 1, 48) do (
set /a i6 = %%e + 1
for /L %%f in (!i6!, 1, 49) do (
rem // Uncomment this echo to watch the progress (severely decreases efficiency)
rem echo %%a %%b %%c %%d %%e %%f
set /a total += 1
)
)
)
)
)
echo Total so far: !total!
)
rem // Should have gone through 13983816 iterations

Format a hexadecimal sequence in a cmd.exe batch file

In a Windows cmd script (aka bat script), I have a FOR /L loop from 1 to 8, where I need to do a bit shift and somehow format a variable as a hexadecimal number (which if you ask, is a single CPU identifier bit to feed into /AFFINITY).
I can't figure out how to do the last step. This is my loop.cmd file:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
SET /A "J=1<<%%i"
ECHO %%i and !J!
)
which does everything but format a hex number:
1 and 2
2 and 4
3 and 8
4 and 16
5 and 32
6 and 64
7 and 128
8 and 256
expected output is:
1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100
How do you format a hexadecimal number?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%i IN (1,1,8) DO (
SET /A "J=1<<%%i"
CALL :DECTOHEX J
ECHO %%i and !J!
)
GOTO :EOF
:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
SET /A DIGIT=DEC%%16, DEC/=16
SET "HEX=%DIGIT%%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B
EDIT: Reply to the comment
Previous solution works correctly when the shifted value have just one bit on, as stated in the question. If the shifted value may have several bits on then a more general decimal-to-hexadecimal conversion is required, like the one below:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM DEFINE THE HEXA DIGITS
SET "HEXA=0123456789ABCDEF"
FOR /L %%i IN (1,1,8) DO (
SET /A "J=3<<%%i"
CALL :DECTOHEX J
ECHO %%i and !J!
)
GOTO :EOF
:DECTOHEX VAR
SET "DEC=!%1!"
SET "HEX="
:NEXT
SET /A DIGIT=DEC%%16, DEC/=16
SET "HEX=!HEXA:~%DIGIT%,1!%HEX%"
IF %DEC% NEQ 0 GOTO NEXT
SET "%1=%HEX%"
EXIT /B
#echo off
setlocal enabledelayedexpansion
set x=2
set n=1
set /a result=n
for /l %%a in (1,1,10) do (
set /a result*=x
if "!result:~0,1!"=="1" set result=!result:16=10!
echo %%a and !result!
)
output:
1 and 2
2 and 4
3 and 8
4 and 10
5 and 20
6 and 40
7 and 80
8 and 100
9 and 200
10 and 400

Batch Alternate Data Streams [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am almost done with my game and have employed "Alternate Data Streams" to save some high scores in a ADS. Now i tried to do the same with a color option, to make the game be the same color scheme you set it every time you open it until you want to change it. Here is the code that i am working on:
echo.
echo Color Options - background/text
echo ------------------
echo 0) Black
echo 1) Blue
echo 2) green
echo 3) Aqua
echo 4) Red
echo 5) Purple
echo 6) Yellow
echo 7) White
echo 8) Grey
echo ------------------
set /p BcolorSetting=Background:
set /p TcolorSetting=Text:
echo %BcolorSetting%%TcolorSetting% >>"%~f0:colors"
color <"%~f0:colors"
pause
If you want to see the whole thing it's...
#echo off
REM Produced by Calder Hutchins
REM This is a game
title Memory Game
:begin
set point=0
cls
echo.
echo Memeory Game
echo ------------------
echo 1) Play
echo 2) Instructions
echo 3) High Scores
echo 4) Options
echo ------------------
set /p pick=^>
if %pick%==1 goto one
if %pick%==2 goto two
if %pick%==3 goto three
if %pick%==4 goto four
if %pick%==99 goto test
goto begin
:one
cls
REM Determines the number
if %point% LSS 6 set /a rand=%random% %% (100 - 1 + 1)+ 1
if %point% LSS 12 if %point% GTR 5 set /a rand=%random% %% (500 - 100 + 1)+ 100
if %point% LSS 18 if %point% GTR 11 set /a rand=%random% %% (1000 - 500 + 1)+ 500
if %point% LSS 24 if %point% GTR 17 set /a rand=%random% %% (2000 - 1000 + 1)+ 1000
if %point% LSS 30 if %point% GTR 23 set /a rand=%random% %% (9000 - 1500 + 1)+ 1500
if %point% LSS 36 if %point% GTR 29 set /a rand=%random% %% (19000 - 5000 + 1)+ 5000
if %point% LSS 42 if %point% GTR 35 set /a rand=%random% %% (32000 - 10000 + 1)+ 10000
if %point% LSS 48 if %point% GTR 41 set /a rand=%random% %% (999 - 100 + 1)+ 100
if %point% LSS 48 if %point% GTR 41 set /a randtwo=%random% %% (999 - 100 + 1)+ 100
if %point% GTR 47 set /a rand=%random% %% (9999 - 1000 + 1)+ 1000
if %point% GTR 47 set /a randtwo=%random% %% (9999 - 1000 + 1)+ 1000
echo.
REM Prints the number
if %point% LSS 42 echo %rand%
if %point% GTR 41 set rand=%rand%%randtwo%
if %point% GTR 41 echo %rand%
echo.
ping localhost -n 3 >nul
cls
echo.
echo.
echo.
set /p yourOption=Guess:
REM Determines correct or wrong
if %youroption%==%rand% set /a point=%point% +1 & goto one
cls
echo.
echo You scored: %point%
echo.
set /p name=Type name:
echo %name% - %point% >>"%~f0:scores"
goto begin
:two
cls
echo.
echo The objective of the game is to get as many points as possible. To get points you must correctly retype the numbers that appear on the screen. The numbers show for a short period of time. As you get more points the numbers get longer! When you have lost you will be prompted to enter your name. You can view the highscores too!
echo.
pause
goto begin
:three
cls
echo.
more<"%~f0:scores" | sort
echo.
pause
goto begin
:four
cls
echo.
echo Settings/Options
echo ------------------
echo 1) color
echo ------------------
set /p pickSetting=^>
if %pickSetting%==1 goto oneSetting
goto four
:oneSetting
cls
echo.
echo Color Options - background/text
echo ------------------
echo 0) Black
echo 1) Blue
echo 2) green
echo 3) Aqua
echo 4) Red
echo 5) Purple
echo 6) Yellow
echo 7) White
echo 8) Grey
echo ------------------
set /p BcolorSetting=Background:
set /p TcolorSetting=Text:
echo %BcolorSetting%%TcolorSetting% >>"%~f0:colors"
color <"%~f0:colors"
pause
goto begin
Thank-you in advance guys!
Fortunately the FOR /F can read ADS:
for /f "usebackq" %%C in ("%~f0:colors") do COLOR %%C
It appears that you will have to CD a certain directory. Then, write to a text file within the directory. In the start, read the text file.
Then, overwrite the text file and write the color in it at the point you are stuck in.
If you need to start with settings in any app, you always need a save file.
Writing can be like this:
echo %BcolorSetting%%TcolorSetting% >>"colorsetting.txt"
And when retrieving, do it in the very beginning. Before begin.
It is read like this
set /p %~f0:colors= <colorsetting.txt
Assuming you use those variables. I hope this helps.

Resources