If else + Press any key

Microsoft Windows xp professional w/serv...
May 26, 2010 at 09:14:28
Specs: Macintosh
Hi guru,
How to do this in a batch file on winxp or win2003:
If users Press any key then goto :A
else goto :B

Thanks in advance


See More: If else + Press any key

Report •

#1
May 26, 2010 at 11:46:30
A quick, dirty and possibly dangerous script. Tested only on xp home:

@echo off
for %%a in (cmd.exe) do copy "%%~f$path:a" __killtrickcmd__.exe >nul


set flag=
for /l %%a in (1 1 5) do (
    rem for /l will keep going after a goto...
    if not defined flag (
        start /b __killtrickcmd__.exe /c "pause>nul"
        ping 0.0.0.0 -n 2 >NUL
        2>nul tskill __killtrickcmd__ || (
            set flag=1
            goto a
        )
    )
)
 


:b
echo It timed out!
goto :cleanup

:a
echo You pressed a key before timeout!



:cleanup
del __killtrickcmd__.exe
pause


Report •

#2
May 26, 2010 at 19:45:11
Excellent! but why it is dangerous?

And i think i didnt make my statement clearly,
This is what i am looking for:
When users execute the script, it will always show a flashing welcome banner. when users press any key it will goto goodbye.
below is my code:

@echo off

:welcome
cls
color c
echo Welcome!
ping 127.0.0.1 -n 2 > NUL

cls
color e
echo Welcome!
ping 127.0.0.1 -n 2 > NUL
goto :banner

:goodbye
echo Goodbye
pause


Report •

#3
May 26, 2010 at 21:20:41
It's not particularly dangerous, it's really about clobbering:

1, Unlikely as it is, if there was another process called "__killtrickcmd__" it would be killed.

2. Another file named "__killtrickcmd__.exe" would provoke a similar problem.

3. Killing processes is undesirable.


That said cmd in xp has a very limited array of built in ways to obtain user input, and none with a time-out. The main option is "set /p" which requires the user to hit enter to return. An option for y/n is to create a file then "del /p file > nul" and use "if exist" to test the condition.

If the choice command had been included(or if you are willing to bring the executable with the script) then it would be fairly simple - run choice over ascii with a default option/time-out to a extended ansi character in a loop. The only problem with this method would be the loss of fx, arrow, del, ect. keys.

If it's only for 32bit systems then generating a .com with debug would be an option, but it's not a possibility on most 64bit systems. If you choose to go with the option someone else would have to write it because I'm not very good with debug...

Here is my "killtrick" version:

@echo off
for %%a in (cmd.exe) do copy "%%~f$path:a" __killtrickcmd__.exe >nul

rem higher for slower
set change=1
set next=0
set text=c
:loop
cls
color %text%
echo.welcome

start /b __killtrickcmd__.exe /c "pause>nul"
ping 0.0.0.0 -n 2 >NUL
2>nul tskill __killtrickcmd__ || (
    >nul del __killtrickcmd__.exe
    goto :startstuff
)
set /a next+=1
if %next%==%change% (set next=0) else goto :loop
if %text%==c (set text=e) else set text=c
goto loop

:startstuff
pause


Report •

Related Solutions

#4
May 27, 2010 at 00:02:17
I got bored so I improved it a bit, less chance of clobbering...

@echo off
:setproc
set process=killtrick%random%
for %%a in (tasklist.exe) do if not "%%~f$path:a"=="" tasklist|>nul find "%process%.exe"&&goto setproc
for %%a in (cmd.exe) do copy "%%~f$path:a" "%temp%\%process%.exe" >nul
rem higher for slower
set change=1
set next=0
set text=1
set backg=0
:loop
cls
color %backg%%text%
echo.welcome %backg%%text%
start /b "" "%temp%\%process%.exe" /c "pause>nul"
ping 0.0.0.0 -n 2 >NUL
2>nul tskill %process% || (
    >nul del "%temp%\%process%.exe"
    goto :startstuff
)
set /a next+=1
if %next%==%change% (set next=0) else goto :loop
:text
set /a text=^(0x%text% + 1^) %% 16
if %text%==10 set text=a
if %text%==11 set text=b
if %text%==12 set text=c
if %text%==13 set text=d
if %text%==14 set text=e
if %text%==15 set text=f
if not %text%==0 if not %text%==%backg% (
   goto loop
) else if not %text%==0 goto text

:backg
set /a backg=^(0x%backg% + 1^) %% 16
if %backg%==10 set backg=a
if %backg%==11 set backg=b
if %backg%==12 set backg=c
if %backg%==13 set backg=d
if %backg%==14 set backg=e
if %backg%==15 set backg=f
if not %text%==%backg% goto loop
if not %backg%==0 goto backg
goto text


:startstuff
pause


Report •

#5
May 27, 2010 at 12:41:11
Judago,
This is AMAZING!! WOW This is exactly what i want!!!
Thank you very much for your help

hmm..but there is some code i cant figure out myself
would you kindly explain for me?
why this code can goto :startstuff when press any key:

start /b "" "%temp%\%process%.exe" /c "pause>nul"
ping 0.0.0.0 -n 2 >NUL
2>nul tskill %process% || (
>nul del "%temp%\%process%.exe"
goto :startstuff
)


Report •

#6
May 27, 2010 at 23:36:21
>> start /b "" "%temp%\%process%.exe" /c "pause>nul"

At the start of the script I make a copy of cmd.exe under another name, "start /b" invokes it as a separate process in the same console window. All it does is a "pause" with no output so if a key is hit the process will end.

>>ping 0.0.0.0 -n 2 >NUL

Just a short delay to make it less processor intensive.


>>2>nul tskill %process% || (

Ignoring error messages try to kill the process of the copied cmd.exe, if tskill returns an errorlevel greater than 0 (it will return 1 if it can't find the process) execute the following code block. The process shouldn't exist if a key has been pressed. http://judago.webs.com/batchoperato...


>>>nul del "%temp%\%process%.exe"
>>goto :startstuff
>>)

Delete the copy and jump to :startstuff(inside the code block).


Report •

#7
May 28, 2010 at 07:19:30
thanks for sharing, I cant do this without your help.

Report •

#8
May 28, 2010 at 08:48:43
No problem ;)

Report •

Ask Question