I have a simple (I hope it doesn't become complicated) batch file in which you enter in a series of letters, (possibly seperated by spaces) and it 'decrypts them' similar to those old 'decoder wheels', in this case where A=1, B=2, and so forth
This is what I have so far, but I wish to make it so it can find what you put in all together, rather than having to enter in each letter one at a time. Thanks
echo off
cls
:1
echo Enter in letters to decrypt
set /p let=
if %let%==A echo 1
if %let%==B echo 2
if %let%==C echo 3
if %let%==D echo 4
if %let%==E echo 5
if %let%==F echo 6
if %let%==G echo 7
if %let%==H echo 8
if %let%==I echo 9
if %let%==J echo 10
if %let%==K echo 11
if %let%==L echo 12
if %let%==M echo 13
if %let%==N echo 14
if %let%==O echo 15
if %let%==P echo 16
if %let%==Q echo 17
if %let%==R echo 18
if %let%==S echo 19
if %let%==T echo 20
if %let%==U echo 21
if %let%==V echo 22
if %let%==W echo 23
if %let%==X echo 24
if %let%==Y echo 25
if %let%==Z echo 26
CHOICE /C ABCDEFGHIJKLMNOPQRSTUVWXYZ /N
echo %errorlevel%
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
is there a way to make a If query for specifig
number sections in Batch?
Something Like this:
IF "Var1"=="1-10" (
do something
)
the 1-10 should stand for 1,2,3,4,5,6,7,8,9,10.
I want to make 10 queries (1-10,11-20,21-30, ... , 91-100)
Is that possible?
You can do this:
#echo off
set Var1=1
for /l %%i in (0,1,10) do if %%i==%Var1% echo 10 or below
for /l %%i in (11,1,20) do if %%i==%Var1% echo 11 - 20
for /l %%i in (21,1,30) do if %%i==%Var1% echo 21 - 30
for /l %%i in (31,1,40) do if %%i==%Var1% echo 31 - 40
for /l %%i in (41,1,50) do if %%i==%Var1% echo 41 - 50
for /l %%i in (51,1,60) do if %%i==%Var1% echo 51 - 60
for /l %%i in (61,1,70) do if %%i==%Var1% echo 61 - 70
for /l %%i in (71,1,80) do if %%i==%Var1% echo 71 - 80
for /l %%i in (81,1,90) do if %%i==%Var1% echo 81 - 90
for /l %%i in (91,1,100) do if %%i==%Var1% echo 91 - 100
Here you can change set Var1=1 to any other number and it will correspond. You can replace echo N - N with your commands.
Also, set Var1=1 can be removed from the above example if used with your code as I simply set it to demonstrate the behaviour. Here is an extract from the help when running for /? so you understand the numeric behaviour.
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)
The following batch file works perfectly for the first 10 options. However I do not know how to extend the functionality of the batch file more than 10 option 0- 9. I have 35 possible options.
#ECHO OFF
MODE CON:COLS=100 LINES=50
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
(SET sourceFolder=C:\Users\Administrator\Desktop\test)
](SET targetFolder=C:\Users\Administrator\Desktop\test2)
SET /P "customerID=Enter Customer ID: "
:MENU
CLS
ECHO=
ECHO= ..................................................
ECHO= PLEASE SELECT THE OPTION FROM THE EVENT MENU BELOW
ECHO= ..................................................
ECHO Option Number Event Number Event Name
ECHO.
ECHO. 0. 101 NewCustomer
ECHO. 1. 102 ExisingCustomer
ECHO. 2. 103 LockedAccount
ECHO. 3. 104 DeleteAccount
ECHO. 4. 105 ExpireSession
ECHO. 5. 106 PasswordUpdated
ECHO. 6. 107 PasswordReset
ECHO. 7 108 HackedAccount
ECHO. 8. 109 UpdateProfile
ECHO. 9. 110 UpdateLists
ECHO. A. 111 NewPurchase
ECHO. B. 112 ExisingSale
ECHO. C. 113 AmendOrder
ECHO. D. 114 AmendDelivery
ECHO. E. 115 CancelOrder
ECHO. F. 116 OvernightFiles
ECHO. G. 117 DailyFiles
ECHO. H. 118 HelpFlies
ECHO. I. 119 FraudAccount
ECHO. J. 120 DeadCustomer
ECHO. K. 121 WelcomeEmail
ECHO. L. 122 OrderEmail
ECHO. M. 123 OrderAmmended
ECHO. N. 124 NoRemainingStock
ECHO. O. 125 ReplenishStock
ECHO. P. 126 SalesQuery
ECHO. Q. 127 UpdateFirstName
ECHO. R. 128 UpdateSurname
ECHO. S. 129 UpdateAddress
ECHO. T. 130 UpdateDateOfBirth
ECHO. U. 131 UpdateContactNo
ECHO. V. 132 UpdateEmail
ECHO. W. 133 UpdatePreferences
ECHO. X. 134 UpdateEmailPreferences
ECHO. Y. 135 UpdateBankDetails
CHOICE /C 0123456789ABCDEFGHIJKLMNOPQRSTUVWXY /M "CHOOSE AN OPTION"
SET "Option=%ERRORLEVEL%"
FOR /F "TOKENS=1-4 DELIMS=. " %%A IN ('FINDSTR/BC:"ECHO. " "%~f0"'
) DO IF "%%B"=="%Option%" (SET "Name=%%C" & SET "Code=%%D")
FOR /F "DELIMS=" %%A IN (
'FINDSTR/MISC:"%customerID%" "%sourceFolder%\*"^|FINDSTR/MIF:/ /C:"%Name%"'
) DO COPY "%%A" "%targetFolder%"
PAUSE
Took me some time to understand what your problem was:
when you type a choice, you get the index of the choice and not the relevant letter, making it impossible to link back to the selected item of the menu if not 0-9.
So I propose to convert back your index to the entered letter like this (extract):
SET "Option=%ERRORLEVEL%" <== this is your code
rem here scan for correct letter
FOR /L %%a IN (0,1,35) DO if %%a==%Option% set idx=%%a
rem small correction of index
set /A idx-=1
set Option=!Choice:~%idx%,1!
Full test code, pressing a digit or a letter prints the letter and the code & title of the menu:
#echo off
set CHOICE=0123456789ABCDEFGHIJKLMNOPQRSTUVWXY
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:MENU
CLS
ECHO=
ECHO= ..................................................
ECHO= PLEASE SELECT THE OPTION FROM THE EVENT MENU BELOW
ECHO= ..................................................
ECHO Option Number Event Number Event Name
ECHO.
ECHO. 0. 101 NewCustomer
ECHO. 1. 102 ExisingCustomer
ECHO. 2. 103 LockedAccount
ECHO. 3. 104 DeleteAccount
ECHO. 4. 105 ExpireSession
ECHO. 5. 106 PasswordUpdated
ECHO. 6. 107 PasswordReset
ECHO. 7 108 HackedAccount
ECHO. 8. 109 UpdateProfile
ECHO. 9. 110 UpdateLists
ECHO. A. 111 NewPurchase
ECHO. B. 112 ExisingSale
ECHO. C. 113 AmendOrder
ECHO. D. 114 AmendDelivery
ECHO. E. 115 CancelOrder
ECHO. F. 116 OvernightFiles
ECHO. G. 117 DailyFiles
ECHO. H. 118 HelpFlies
ECHO. I. 119 FraudAccount
ECHO. J. 120 DeadCustomer
ECHO. K. 121 WelcomeEmail
ECHO. L. 122 OrderEmail
ECHO. M. 123 OrderAmmended
ECHO. N. 124 NoRemainingStock
ECHO. O. 125 ReplenishStock
ECHO. P. 126 SalesQuery
ECHO. Q. 127 UpdateFirstName
ECHO. R. 128 UpdateSurname
ECHO. S. 129 UpdateAddress
ECHO. T. 130 UpdateDateOfBirth
ECHO. U. 131 UpdateContactNo
ECHO. V. 132 UpdateEmail
ECHO. W. 133 UpdatePreferences
ECHO. X. 134 UpdateEmailPreferences
ECHO. Y. 135 UpdateBankDetails
CHOICE /C %CHOICE% /M "CHOOSE AN OPTION"
SET "Option=%ERRORLEVEL%"
rem here scan for correct letter
FOR /L %%a IN (0,1,35) DO if %%a==%Option% set idx=%%a
rem small correction of index
set /A idx-=1
set Option=!Choice:~%idx%,1!
FOR /F "TOKENS=1-4 DELIMS=. " %%A IN ('FINDSTR/BC:"ECHO. " "%~f0"'
) DO (IF "%%B"=="%Option%" (SET "Name=%%C" & SET "Code=%%D" & echo activated %Option%: !Name!,!Code!)
)
Notes:
I needed to enable delayed expansion back
I set CHOICE in a variable at the start, nicer to propose the choice and then scan into it for correct letter/digit
May I suggest a different approach based on arrays that run faster and is simpler to keep updated?
#ECHO OFF
MODE CON:COLS=100 LINES=50
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
(SET sourceFolder=C:\Users\Administrator\Desktop\test)
(SET targetFolder=C:\Users\Administrator\Desktop\test2)
SET /P "customerID=Enter Customer ID: "
REM Assemble the data arrays
set i=0
set "options="
FOR /F "TOKENS=1-4 DELIMS=. " %%A IN ('FINDSTR/BC:"ECHO. " "%~f0"') DO (
set /A i+=1
set "options=!options!%%B"
SET "Name[!i!]=%%C"
SET "Code[!i!]=%%D"
)
:MENU
CLS
ECHO=
ECHO= ..................................................
ECHO= PLEASE SELECT THE OPTION FROM THE EVENT MENU BELOW
ECHO= ..................................................
ECHO Option Number Event Number Event Name
ECHO/
ECHO. 0. 101 NewCustomer
ECHO. 1. 102 ExisingCustomer
ECHO. 2. 103 LockedAccount
ECHO. 3. 104 DeleteAccount
ECHO. 4. 105 ExpireSession
ECHO. 5. 106 PasswordUpdated
ECHO. 6. 107 PasswordReset
ECHO. 7 108 HackedAccount
ECHO. 8. 109 UpdateProfile
ECHO. 9. 110 UpdateLists
ECHO. A. 111 NewPurchase
ECHO. B. 112 ExisingSale
ECHO. C. 113 AmendOrder
ECHO. D. 114 AmendDelivery
ECHO. E. 115 CancelOrder
ECHO. F. 116 OvernightFiles
ECHO. G. 117 DailyFiles
ECHO. H. 118 HelpFlies
ECHO. I. 119 FraudAccount
ECHO. J. 120 DeadCustomer
ECHO. K. 121 WelcomeEmail
ECHO. L. 122 OrderEmail
ECHO. M. 123 OrderAmmended
ECHO. N. 124 NoRemainingStock
ECHO. O. 125 ReplenishStock
ECHO. P. 126 SalesQuery
ECHO. Q. 127 UpdateFirstName
ECHO. R. 128 UpdateSurname
ECHO. S. 129 UpdateAddress
ECHO. T. 130 UpdateDateOfBirth
ECHO. U. 131 UpdateContactNo
ECHO. V. 132 UpdateEmail
ECHO. W. 133 UpdatePreferences
ECHO. X. 134 UpdateEmailPreferences
ECHO. Y. 135 UpdateBankDetails
CHOICE /C %options% /M "CHOOSE AN OPTION"
SET "Option=%ERRORLEVEL%"
SET "Name=!Name[%Option%]!" & SET "Code=!Code[%Option%]!"
FOR /F "DELIMS=" %%A IN (
'FINDSTR/MISC:"%customerID%" "%sourceFolder%\*"^|FINDSTR/MIF:/ /C:"%Name%"'
) DO COPY "%%A" "%targetFolder%"
PAUSE
As an alternative:
#ECHO OFF
MODE CON:COLS=50 LINES=50
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
(SET sourceFolder=C:\test)
(SET targetFolder=C:\test2)
SET /P "customerID=Enter Customer ID: "
:MENU
"SET _="
CLS
ECHO=
ECHO= ................................................
ECHO= PLEASE SELECT THE EVENT CODE FROM THE MENU BELOW
ECHO= ................................................
ECHO=
ECHO= Event Code -=- Event Name
ECHO=
ECHO. 101 --- NewCustomer
ECHO. 102 --- ExisingCustomer
ECHO. 103 --- LockedAccount
ECHO. 104 --- DeleteAccount
ECHO. 105 --- ExpireSession
ECHO. 106 --- PasswordUpdated
ECHO. 107 --- PasswordReset
ECHO. 108 --- HackedAccount
ECHO. 109 --- UpdateProfile
ECHO. 110 --- UpdateLists
ECHO. 111 --- NewPurchase
ECHO. 112 --- ExisingSale
ECHO. 113 --- AmendOrder
ECHO. 114 --- AmendDelivery
ECHO. 115 --- CancelOrder
ECHO. 116 --- OvernightFiles
ECHO. 117 --- DailyFiles
ECHO. 118 --- HelpFlies
ECHO. 119 --- FraudAccount
ECHO. 120 --- DeadCustomer
ECHO. 121 --- WelcomeEmail
ECHO. 122 --- OrderEmail
ECHO. 123 --- OrderAmmended
ECHO. 124 --- NoRemainingStock
ECHO. 125 --- ReplenishStock
ECHO. 126 --- SalesQuery
ECHO. 127 --- UpdateFirstName
ECHO. 128 --- UpdateSurname
ECHO. 129 --- UpdateAddress
ECHO. 130 --- UpdateDateOfBirth
ECHO. 131 --- UpdateContactNo
ECHO. 132 --- UpdateEmail
ECHO. 133 --- UpdatePreferences
ECHO. 134 --- UpdateEmailPreferences
ECHO. 135 --- UpdateBankDetails
ECHO=
SET/P "PICK= > "
FOR /F "EOL=F TOKENS=2-3 DELIMS=.- " %%A IN ('FIND " --- " "%~f0"') DO (
IF %PICK%==%%A (SET "_=T"
FOR /F "DELIMS=" %%C IN (
'FINDSTR/MISC:"%customerID%" "%sourceFolder%\*"
^|FINDSTR/MIF:/ /C:"%%B"') DO COPY "%%C" "%targetFolder%"))
IF NOT DEFINED _ GOTO :MENU
TIMEOUT -1
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)
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.