i am working on a batch chat program... because it's fun.. but i have run into a problem.
I want the users to be able to chat, or use commands from the same prompt and this method works:
set /p m=Message:
set tm="%m%"
if %tm% == "KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
However to make the command work i need to have another step somewhere else in the batch file:
:kick
set /p person=Who to kick?
del %dir%\%person%
How can i make it so that someone can just type "KICK John" to kick someone?
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
del %dir%\%person%
Naturally, the destination label for the final if not defined is debatable.
Fix : remove quotes surrounding %m%
Additional 20130924T0736Z
Here's my test batch:
:: #ECHO OFF
SETLOCAL
SET u=whatever
SET log=u:\log.log
set /p m=Message:
set tm=%m%
if /i "%tm:~0,4%"=="KICK" goto kick
echo %time% ^<%u%^>: %m% >> %log%
TYPE %log%
GOTO :eof
:kick
set person=%tm:~5%
if not defined person set /p person=Who to kick?
if not defined person goto kick
ECHO del %dir%\%person%
GOTO :EOF
And run-report:
>SETLOCAL
>SET u=whatever
>SET log=u:\log.log
>set /p m=Message:
Message:
>set tm=Hello Friend :P
>if /I "Hell" == "KICK" goto kick
>echo 15:33:12.77 <whatever>: Hello Friend :P 1>>u:\log.log
>TYPE u:\log.log
15:33:12.77 <whatever>: Hello Friend :P
>GOTO :eof
So - given that I've no idea of what your setting for u or log or dir happens to be, and that it works for me as shown above, I can only conclude that the quotes ARE required somewhere in the rest of your code - which you haven't shown.
There's no ban on using your quotes - once the presence of "kick" is determined, so
set tm="%m%"
directly after the IF /i... line should fix your problem.
Well, you could try this:
Setlocal Enableextensions
set /p m=Message:
set tm="%m%"
if /i %m:~0,4% == "KICK" call kick "%m:~4%"
echo %time% ^<%u%^>: %m% >> %log%
:kick
del %dir%\%1
goto :eof
Im not too familiar with this sort of Batch scripting (I'd normally use C#), but that should work fine. Tell me if it doesn't work, along with the errormessage. There are other ways of doing it as well.
Mona
Related
In a batch file I'm getting information from the user for later database work.
I first ask if the user is using Windows Auth and then depending on the answer, I ask for username & password, or set a variable and continue on.
But for the life of me, I can't figure out why one of the SET /p lines is causing the batch file to crash, when another SET /p line isn't.
Here's my code:
#ECHO OFF
#SETLOCAL ENABLEDELAYEDEXPANSION
CHOICE /M "Will you be using Windows Authentication to connect to the database?"
ECHO ERRORLEVEL: %ERRORLEVEL%
#PAUSE
IF ERRORLEVEL 2 (
ECHO ERRORLEVEL should be 2: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=FALSE
SET /p _USERNAME=User name:
REM SET /p _PASSWORD=Password (NOTE: Password will be displayed as you type):
) ELSE (
ECHO ERRORLEVEL should be 1: %ERRORLEVEL%
#PAUSE
SET _USINGWINAUTH=TRUE
)
ECHO.
ECHO _USINGWINAUTH: %_USINGWINAUTH%
ECHO _USERNAME: %_USERNAME%
ECHO _PASSWORD: %_PASSWORD%
#PAUSE
As is, with the second SET /p commented out, regardless if you answer Y or N the script runs fine.
But if the second SET /p line is not commented, regardless of what the answer is, the script instantly closes right after hitting a key at the pause before the ECHO ERRORLEVEL.
I just don't see why that's happening!
As per last time, whilst using a caret works, it would be better if you were to rework the logic.
One example:
#ECHO OFF
CHOICE /M "Will you be using Windows Authentication to connect to the database"
IF "%ERRORLEVEL%"=="1" SET "_USINGWINAUTH=TRUE" & GOTO NEXT
SET "_USINGWINAUTH=FALSE"
SET /P "_USERNAME=User Name: "
SET /P "_PASSWORD=Password (will be displayed as you type): "
:NEXT
ECHO(
SET _USINGWINAUTH
SET _USERNAME 2>NUL
SET _PASSWORD 2>NUL
PAUSE
(This is my first post here, so bear with me)
Can you show the last user-input in a batch file? I'm gonna try to keep it simple here.
#echo off
:menu
echo Type 1 to proceed.
set /p example=
if "%example%" == "1" GOTO :proceed
GOTO :error
:proceed
pause
:error
cls
echo You wrote (last user input), that's not correct.
timeout 30
GOTO :menu
I know that I could replace the (last user input) with %example%, but then I'd have to make custom error messages for every category, and there are about 50 of them. It'd be easier with a last input command.
By the way, I've taught myself everything that I know about batch, so my example probably has major issues right now, but it works somehow.
You could centralize all user input into a function (user_input)
:menu1
echo Type 1 to proceed.
call :userInput example
if "%example%" == "1" GOTO :proceed
GOTO :error
:menu2
echo Type 42 to proceed.
call :userInput answer
if "%answer%" == "42" GOTO :proceed
GOTO :error
:userInput
set /p LAST_INPUT=
set "%1=%LAST_INPUT%"
exit /b
:proceed
pause
:error
cls
echo You wrote "%LAST_INPUT%", that's not correct.
timeout 30
GOTO :menu
I don't know how to do it without temp file. TO get the things written int the console you need the doskey /history (this will skip the running of the script itself):
#echo off
setlocal enableDelayedExpansion
set "last="
set "but_last="
doskey /history > log.txt
for /f "tokens=* delims=" %%# in (log.txt) do (
set "but_last=!last!"
set "last=%%#"
)
echo "%but_last%"
del /s /q log.txt >nul 2>nul
#echo off
title Music
color 0f
:main
cls
echo Welcome to you Music!
echo.
set /p var=What would you like to do:
if %var%==find goto find
if %var%==play goto play
goto main
:find
cls
set /p song=What song would you like me to check for (Case Sensitive):
if exist music\"*%song%*.mp3" goto exist
if not exist music\"*%song%*.mp3" goto notExist
:exist
cls
echo %song% is here.
echo.
pause
goto main
:notExist
cls
echo %song% is not here. Check the spelling and letter case for errors.
echo.
pause
goto main
:play
cls
set /p song=Enter the name of a song to play:
if exist music\"*%song%*.mp3" goto play2
if not exist music\"*%song%*.mp3" goto notExist
goto main
:play2
cls
set songName = [file name]
:playing
:: Will put more code later.
The purpose of this program is to search my music for a specific song and be able to play it from command line. I will later add code to control the music but for now I just want to be able to find the song and play it. I am using the '' so someone could enter "Another Brick in the Wall" and let it find "Another Brick in the Wall Part 2" The issue is when saving a variable, it can not use '' or they will be saved as part of the string.
your question isn't very clear. The following should answer what I guessed from your question and comments:
set /p "song=What song would you like me to check for (Case insensitive): "
REM if you are afraid of qoutes entered by the user, remove them:
set song=%song:"=%
<nul set /p "=Songs found: "
dir /b "music\*%song%*.mp3"|find /c /v ""
echo the names are:
dir /b "music\*%song%*.mp3"
echo executing them...
for /f "delims=" %%i in ('dir /b /s "music\*%song%*.mp3"') do (
echo ... executing %%i
REM whatever you use to play it
)
Ok, I have this batch file, and I would like to detect if the user's input includes a certain character, like ..
Here is what I have so far...
IF /I %option%== ATTRIB +h
(I would like it to detect if there is a . pressed)
I would like the .bat to detect If there is something other than Attrib +h pressed, like a ., for a file extension. So that if there was a file given, I would not ask for a file name again, because it wouldn't run properly!
Here is a part of the code, for Magoo.
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
echo %SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
rem IMPLEMENTED IT HERE!
rem Here we go
rem need to fix execution
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==pause goto pause
IF /I %SSL%==backup goto backup
Edit: Ok, the answers from magoo and john really helped, but if i wanted this to detect a folder name, as well as a file name, what would i change?
Edit: Ok Magoo, testing it now.
Edit: That doesnt really work well, as I am trying to find out if the variable INCLUDES a certain character, not if the whole variable is = to a valid file/folder name, but nice try. Will mess around with it a bit... Thanks for your time Magoo, I appreciate it:)
Edit: I think I worked it out! Thanks everyone who helped me!
Here is my new code...
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
%SSL%
cls
goto Seconds
:Seconds
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==attrib +h goto attrib+-
IF /I %SSL%==attrib -h goto attrib+-
IF /I %SSL%==attrib +r goto attrib+-
IF /I %SSL%==attrib -r goto attrib+-
The general formula would be
echo "%var%"|find "characterorstringrequired" >nul
if errorlevel 1 (echo notfound) else (echo found)
So, for your instance,
echo "%option%"|find "i" >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Beyond which, I have to be as vague as your question, I'm afraid...
After seeing part-code...
echo "%SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
I'm not sure whether cls affects errorlevel. The test should be executed directly after the command that establishes errorlevel - which is the echo %SSL... line.
echo "%SSL%"|find "." >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Serious damage can be done by cmd if the commands are not understood. The attrib command requires a target, so it should be
... else (attrib +h something)
to set something to hidden.
beyond that - it's not a good idea to use cmd keywords like attrib, pause and backup as labels. It can cause damage in the case of a tyop.
The benefit of sying what you want to do instead of how you want to do whatever-it-is.
To test for a file's existence, try
if exist "%SSL%" (echo "%SSL%" exists) else (echo "%SSL%" is not there)
where echo can be any legitimate (series of) command(s).
What about something like this for all special chars ?
#echo off
:_Start
set /p "FID="
set FID="%FID:"=%"
setlocal EnableDelayedExpansion
for %%I in (^| ^& ^< ^> ^^ ^( ^) ' ` ^%% ^") do set FID=!FID:%%I=!
setlocal DisableDelayedExpansion
set FID="%FID:!=%"
endlocal&set FID=%FID:~1,-1%
:_Back
set _Flag1=
for /f "tokens=1* delims=~=*;,?-+\/.##${}[]: " %%J in ('echo !FID!') do (
set FID=%%J%%K
set _Flag1=%%J
set _Flag2=%%K
)
if NOT "%_Flag2%"=="" goto :_Back
if NOT defined _Flag1 goto :_Start
endlocal&set FID=%FID%
echo %FID%
pause
The VBA command for finding a character in a string is INSTR so if you are looking for a . for example then in VBA could could use INSTR. There are two ways to do what you want:
Write a QBASIC or VBscript version of INSTR and then call that from a BATCH script
Write a batch script that does INSTR.
If you are interested in the second one look at http://www.dostips.com/forum/viewtopic.php?f=3&t=1986
It isn't as efficied as the first one would be because INSTR is pretty much a one liner in VBScript and the batch version does a character by character comparison, but if you are looking for a batch only solution that may do what you want.
I'm trying to make a program like clever bot but when the uses spaces it won't work. Please help.(:
#echo off
set text=Hello
:start
cls
echo %text%
set /p input=:
if %input%==a b set text=It worked
goto start
Use quotes:
#echo off
set text=Hello
:start
cls
echo %text%
set /p input=:
if "%input%"=="a b" set text=It worked
goto start
It will also handle null input.
Mona