I got the following code elsewhere on stackoverflow:
#echo off
rem throw the first parameter away
shift
set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop
:afterloop
#echo %params%
#echo on
Result: Running shifttest 1 2 3 4 5 6 7 8 yields 2 3 4 5 6 7 8
After further testing, I've discovered I can throw the first two parameters away by doing the following:
#echo off
rem throw the first two parameters away
shift
set params=%2
:loop
shift
if [%2]==[] goto afterloop
set params=%params% %2
goto loop
:afterloop
#echo %params%
#echo on
Result: Running shifttest 1 2 3 4 5 6 7 8 yields 2 3 4 5 6 7 8
I've also discovered that I can replace the %2 with %3 to throw away the first three, and so on...
My question:
Is there a way to specify how many parameters to throw away?
Example: Running shifttest 5 2 3 4 5 6 7 8 yields 6 7 8, i.e. the first parameter specifies how many parameters to throw away, including the first one.
I was thinking something along the lines of %(%1), but that obviously doesn't work.
Given this simple batch-file (test.bat), which uses a for loop to shift the command-line params %1 number of times:
#echo off
for /L %%i in (1,1,%1) do shift
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
I get the following:
c:\>test 1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
c:\>test 5 2 3 4 5 6 7 8 9
6 7 8 9
You could easily adapt this technique to set params to the remaining parameters. For instance:
#echo off
for /L %%i in (1,1,%1) do shift
set params=
:loop
if [%1]==[] goto afterloop
if defined params (set params=%params% %1) else (set params=%1)
shift
goto loop
:afterloop
echo %params%
Now running it yields this:
c:\>test 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
c:\>test 5 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
6 7 8 9 10 11 12 13 14 15 16 17 18
There is not a direct way to do that. This is a simple work-around:
#echo off
setlocal EnableDelayedExpansion
for /L %%i in (1,1,%1) do shift
:loop
if [%1] == [] goto afterloop
set params=%params% %1
shift
goto loop
:afterloop
echo %params%
The construct that you indicated as %(%1) would be equivalent to the following construct, if such a construct would be possible!
echo The parameter indicated by first one (the fifth): !%1
that is, expand %1 to 5 and then, with a delayed replacement. expand !5 to the fifth parameter, but this construct does not work on Batch fle parameters, just on variables. The usual way to achieve complex managements is first store the parameters in an array, and then access the array elements. For example:
#echo off
setlocal EnableDelayedExpansion
set n=0
:loop
if [%1] == [] goto afterloop
set /A n+=1
set param[%n%]=%1
shift
goto loop
:afterloop
echo The parameter indicated by first one (the fifth): !param[%param[1]%]!
#ECHO OFF
SETLOCAL
SET val=%1 %*
FOR /l %%i IN (1,1,%1) DO CALL SET "val=%%val:* =%%"
FOR /f %%j IN ("%val%") DO SET "val=%%j"
ECHO val=+%val%+
GOTO :EOF
will set val to the nth parameter when executed with thisbatch n 2 3 4 5 6 7
If n>#parameters, returns the last.
Remove the %1 in %1 %* to yield parameters numbered thisbatch n 1 2 3 4 5 6 7 (ie n=1 produces 1st after n)
Related
I wrote a small timer-script as a .bat-file, which reminds me (16 times) every 30 minutes (1800 sec) in a pop-up messagebox to "Move!". The script works fine on Windows 7 (32 bit) Systems, but it seems that the "msg" command canĀ“t be used or is not existent for 64-bit Systems. Is there any alternative to this command or way to replace that command easily?
set TIMER=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
set USERS=(%username%)
set MESSAGE=Move!
for %%i in %TIMER% do call :doit
:doit
for %%i in %USERS% do msg %%i %MESSAGE%
timeout /t 1800 /nobreak
goto:eof
You could create a temporary VBS script, run it then delete it.
See Example MessageBox
At the bottom of your program include:
exit /b
:msg
set tempPath=%temp%\msgbox.vbs
echo msgbox "%message%" > %tempPath% && %tempPath% && del %tempPath%
goto:eof
You can then use it through your script like so:
set message=Hello World
call:msg
So in your case:
set TIMER=(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
set USERS=(%username%)
set MESSAGE=Move!
for %%i in %TIMER% do call :doit
:doit
set message=%MESSAGE%
call:msg
timeout /t 1800 /nobreak
goto:eof
exit /b
:msg
set tempPath=%temp%\msgbox.vbs
echo msgbox "%message%" > %tempPath% && %tempPath% && del %tempPath%
goto:eof
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
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
Ive look over many threads and cannot seem to put together a solution to my problem.
What i would like to do is use two lists to create one output.
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
for %%A in (%Client_ID%) do (
for %%B in (%PartNo%) do (
echo %%A %%B
)
)
But the output I get is:
BJCH 1
BJCH 2
BJCH 9
BJCH 10
BJCH 12
BJCH 20
BORG 1
BORG 2
BORG 9
BORG 10
BORG 12
BORG 20
FGMS 1
FGMS 2
FGMS 9
etc........
What i need is
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20
Any idea what I'm doing wrong? Any help is much appreciated.
Your two lists are separated ones: if you nest one for into the other one, you are multiplying the number of results. There is no way to process both lists in the same for, unless you convert the lists into two arrays and then process both arrays in the same for, that is, process the elements of the two arrays with same index. For example:
setlocal EnableDelayedExpansion
set Client_ID[1]=BJCH
set Client_ID[2]=BORG
etc...
set PartNo[1]=1
set PartNo[2]=2
etc...
for /L %%i in (1,1,6) do echo !Client_ID[%%i]! !PartNo[%%i]!
You may also simulate previous processing ("two elements with same index") this way:
#echo off
setlocal EnableDelayedExpansion
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
set i=0
for %%A in (%Client_ID%) do (
set /A i+=1, j=0
for %%B in (%PartNo%) do (
set /A j+=1
if !i! equ !j! echo %%A %%B
)
)
EDIT: Output example added
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20
I am coding a store generator for a table top RPG. I have it set up to generate different content based on which faction you select, a random roll to determine how well the shop is doing (poor, average, good, excellent), and then randomly determines the amount of an item in the shop.
However to make certain items more or less likely to show up I set it to have say 3d6-5 Arrows, this results in a minimum of -2 and a maximum of 13 arrows. Naturally a store cannot have -2 arrows. Is there any way to be able to still set a minimum and maximum range for a variable, but have 0 be the lowest, or make any negative value display as 0?
if %val% lss 0 set /a val=0
is one way
Here's a more comprehensive version:
#ECHO OFF
SETLOCAL
SET val=5
ECHO ==== :setlt =====
CALL :setlt val 3&CALL :showval %val% val
CALL :setlt val 6&CALL :showval %val% val
CALL :setlt val 5&CALL :showval %val% val
CALL :setlt val 3 27&CALL :showval %val% val
CALL :setlt val 6 1&CALL :showval %val% val
CALL :setlt val 5 11&CALL :showval %val% val
ECHO ==== :setgt =====
CALL :setgt val 3&CALL :showval %val% val
CALL :setgt val 6&CALL :showval %val% val
CALL :setgt val 5&CALL :showval %val% val
CALL :setgt val 3 27&CALL :showval %val% val
CALL :setgt val 6 1&CALL :showval %val% val
CALL :setgt val 5 11&CALL :showval %val% val
ECHO ==== :setra =====
CALL :setra val 3 8&CALL :showval %val% val
CALL :setra val 8 11&CALL :showval %val% val
CALL :setra val 2 4&CALL :showval %val% val
CALL :setra val 3 8 6&CALL :showval %val% val
CALL :setra val 8 11 6&CALL :showval %val% val
CALL :setra val 2 4 6&CALL :showval %val% val
CALL :setra val 3 8 6 7 &CALL :showval %val% val
CALL :setra val 8 11 6 7&CALL :showval %val% val
CALL :setra val 2 4 6 7&CALL :showval %val% val
CALL :setra val 3 8 "" 6&CALL :showval %val% val
CALL :setra val 8 11 "" 6&CALL :showval %val% val
CALL :setra val 2 4 "" 6&CALL :showval %val% val
ECHO ==== add some extras =====
SET val=20&CALL :disp val 1 8 "" "" "The strange die rolls "
SET val=-4&CALL :disp val 0 8 "" "" "" "ghouls flee in fear"
SET val=-4&CALL :disp val 0 8 "" "" "There are " "arrows in stock"
SET val=-4&CALL :dispgz val 0 8 "" "" "There are " "arrows in stock"
SET val=14&CALL :dispgz val 0 8 "" "" "There are " "arrows in stock"
GOTO :EOF
:: set a variable %1 to %3 if it is less than %2
:setlt
CALL SET $1=%%%1%%
IF %$1% lss %2 SET "%1=%~3"
IF NOT DEFINED %1 SET "%1=%~2"
goto :eof
:: set a variable %1 to %3 if it is greater than %2
:setgt
CALL SET $1=%%%1%%
IF %$1% gtr %2 SET "%1=%~3"
IF NOT DEFINED %1 SET "%1=%~2"
goto :eof
:: set a variable %1 to %4 if it is less than %2 or %5 if it is greater than %3
:setra
CALL ECHO if %%%1%% .lt. %2 SET to "%~4" IF %%%1%% .gt. %3 to "%~5"
CALL :setlt %1 %2 %~4
CALL :setgt %1 %3 %5
goto :eof
:showval
CALL ECHO value was %1 is now %%%2%%
SET %2=%1
GOTO :eof
:: Display with limits.
:disp
setlocal
CALL SET $2=%%%1%%
CALL :setra $2 %2 %3 "%~4" "%~5"
ECHO %~6%$2% %~7
endlocal
GOTO :eof
:: Display with limits - but only if resolved value is greater than zero
:dispgz
setlocal
CALL SET $2=%%%1%%
CALL :setra $2 %2 %3 "%~4" "%~5"
IF %$2% gtr 0 ECHO %~6%$2% %~7
endlocal
GOTO :eof
Here's the output:
==== :setlt =====
value was 5 is now 5
value was 5 is now 6
value was 5 is now 5
value was 5 is now 5
value was 5 is now 1
value was 5 is now 5
==== :setgt =====
value was 5 is now 3
value was 5 is now 5
value was 5 is now 5
value was 5 is now 27
value was 5 is now 5
value was 5 is now 5
==== :setra =====
if 5 .lt. 3 SET to "" IF 5 .gt. 8 to ""
value was 5 is now 5
if 5 .lt. 8 SET to "" IF 5 .gt. 11 to ""
value was 5 is now 8
if 5 .lt. 2 SET to "" IF 5 .gt. 4 to ""
value was 5 is now 4
if 5 .lt. 3 SET to "6" IF 5 .gt. 8 to ""
value was 5 is now 5
if 5 .lt. 8 SET to "6" IF 5 .gt. 11 to ""
value was 5 is now 6
if 5 .lt. 2 SET to "6" IF 5 .gt. 4 to ""
value was 5 is now 4
if 5 .lt. 3 SET to "6" IF 5 .gt. 8 to "7"
value was 5 is now 5
if 5 .lt. 8 SET to "6" IF 5 .gt. 11 to "7"
value was 5 is now 6
if 5 .lt. 2 SET to "6" IF 5 .gt. 4 to "7"
value was 5 is now 7
if 5 .lt. 3 SET to "" IF 5 .gt. 8 to "6"
value was 5 is now 5
if 5 .lt. 8 SET to "" IF 5 .gt. 11 to "6"
value was 5 is now 8
if 5 .lt. 2 SET to "" IF 5 .gt. 4 to "6"
value was 5 is now 6
==== add some extras =====
if 20 .lt. 1 SET to "" IF 20 .gt. 8 to ""
The strange die rolls 8
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
0 ghouls flee in fear
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
There are 0 arrows in stock
if -4 .lt. 0 SET to "" IF -4 .gt. 8 to ""
if 14 .lt. 0 SET to "" IF 14 .gt. 8 to ""
There are 8 arrows in stock
And now I'm free to waffle a bit...
After setting val to 5, there are many lines of the basic format
CALL :setlt val 3&CALL :showval %val% val
The routine :showval shows the current value of val and then resets it to its original value. In that way, I could report the result of the preceding CALL and reset the value to whatever value I chose (but I didn't use that feature...)
The parameters of the :setlt calls are variable, limit, forced_value - where forced_value is optional. If the forced value is not specified, the limit will be used.
So :setlt with parameters of val 3 will set the value of val to 3 if its current value is less than 3 - but if the parameters were val 6 1 then if the current value of val is less than 6, its value will be changed to 1.
So the :showval routine shows the result in each case, then resets val to 5.
:setgt is similar, just setting the value if the current value is greater than rather than less than.
Next group is the :setra which has parameters var minval maxval forceifltmin forceifgtmax - the only kink being that if forceifltmin is not used but forceiflmax is used, then forceifltmin must be an empty parameter ("").
I've added the call echo... line at the start of :setra purely to show what's happening - it's useful for debugging. Just REM or :: that line so you can turn debugging on or off.
(Tip: with debug-lines, you could try
set "debug=REM "
%debug%echo this is a debug line
Simply set debug to either REM or nothing to turn your debugging lines off or on)
Last is the :disp and :dispgz routines. These are very slightly different. They deliberately don't change the value of the variable and are designed to display the value as if it was in the range, There are an additional two parameters - both optional being the text-before and text-after value. Both should be supplied "quoted".
This time, there's simply a display of text-beforevalue text-after so you can build messages as shown.
If you don't want to display a line containing a value unless the value after applying the range-checks is >0 then use :dispgz