I want to know if there any solution to this:
Main.bat:
#echo off
goto 'input'
: 'input'
cls
set "inp="
set /p inp=What would you like to do?
set firstresponse=%inp:~0,5%
if %firstresponse%==help goto 'help'
pause
if /I %firstresponse%==check set firstresponse=dir && set
executeparttwo=%inp:~5%
if /I %firstresponse%==remov goto 'remove'
%firstresponse%%executeparttwo%
pause
goto 'input'
: 'remove'
set "firstresponse=" && set firstresponse=%inp:~0,6%
if /I %firstresponse%==remove set firstresponse=del
set executeparttwo=%inp:~6%
%firstresponse%%executeparttwo%
pause
goto 'input'
: 'help'
cls
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
pause
goto 'input'
if the User typed an invalid command, it will show like what CMD does ( 'command' is not recongnized...)
What i want to do is to replace the CMD invalid command text to my own one like "command" is an invalid command, but to do that i need to "hide" the CMD one (because if the user typed a invalid command it will not show him a "custom message")
I tried to use some Batch plugins like batbox, CursorPos etc... To replace the cursor position but i didn't get what i wanted. So if anyone have a solution i will be very appreciated!
Have a nice day, and thanks for reading!
Your splitting the command and parameters is not ideal, there is a much easier and safer way. Also, the method of an own subroutine for each command is suboptimal (especially, when you add more and more commands).
#echo off
call :commandlist REM build translation table
:input
REM get input line:
set /p "commandline=Enter Command: "
REM split to command and parameters
for /f "tokens=1,*" %%a in ("%commandline%") do (
set "command=_%%a"
set "params=%%b"
)
REM check for valid command:
set _|findstr /bi "%command%=" >nul || (
echo invalid command: '%command:~1%'.
goto :input
)
REM execute the command:
call %%%command%%% %params%
goto :input
:Commandlist
set "_check=dir /b"
set "_remove=del"
set "_help=:help"
set "_where=call echo %%cd%%"
set "_change=cd"
set "_apt-get=:apt"
set "_bye=exit /b" 'secret' exit command ;)
goto :eof
:help
echo Check = Dir in regular command prompt, checks a directory.
echo Remove = del in regular command prompt, deletes something.
echo Where = echo %%cd%% in regular command prompt, print working folder.
echo Change = cd in regular command prompt, change working folder
goto :eof
:apt
if /i "%~1" == "update" echo updating... & goto :eof
if /i "%~1" == "whatever" echo whatevering... & goto :eof
echo invalid command: '%command:~1% %1'
goto :eof
(Note to experienced batch users: yes I know there is a possibility for some "code injection")
Related
So basically, I have a script we use out on the field to do various functions. Part of the script will include a 'Debug Terminal' (For setting and checking variables, running functions directly etc.). I ran into some problems initially with echo and set commands (echo Hi would result in Hi is not recognized..; set would not process).
Here's what I wound up with:
:DebugConsole
%title%Debug Console %end%
:DebugConsoleTop
echo.
echo Enter a command or type 'exit' to finish.
set /p ConsoleInput=
if "%ConsoleInput%"=="exit" (
goto DebugConsoleEnd
)
:if set used
if "%ConsoleInput:~0,3%"=="set" (
echo %ConsoleInput%> "%datapath%\debugcmd.txt"
set /p ConsoleOutput=<"%datapath%\debugcmd.txt"
del /f "%datapath%\debugcmd.txt"
) else (
:if echo
if "%ConsoleInput:~0,4%"=="echo" (
set "echofix=echo echo"
::set ConsoleInput=%ConsoleInput:%echofix%=%
call set ConsoleInput=%%ConsoleInput:echo=%echofix%%%
%ConsoleInput%
goto DebugConsoleTop
)
:all else
FOR /F "delims=" %%i IN ('%ConsoleInput%') DO (
SET ConsoleOutput=%%i
echo %ConsoleOutput%
pause
)
)
%ConsoleOutput%
goto DebugConsoleTop
:DebugConsoleEnd
cls
exit /b
The problem I'm facing now is that you can not echo variables correctly. If I input "set var=test" and then "echo %var%" the output is "%var%" and not "test". However, if I input "%var%" i receive "test is not recognized.."
I made a simple BATCH file for notes, it just deletes a folder, makes a new one, and puts in a file called note.txt and then edits it... for some reason, it doesn't do ANYTHING at all! not even removing the folder, here's the code:
goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
del /q "D:\Users\Eldar\Desktop\Note Folder"
mkdir "Note Folder"
#echo>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
#echo %note%> "D:\Users\Eldar\Desktop\Note Folder\note.txt"
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
ECHO goto replace
) ELSE (
GOTO abort
)
There are few errors in your script:
del can delete only empty folders. Use rmdir /s /q folder_name to remove folder with files
Remove ECHO in ECHO goto replace
You are using full paths everywhere except mkdir. Is it correct?
My working version:
#echo off
goto start
:abort
echo Aborted!
pause
goto start
:replace
rmdir /s /q ".\TestF"
mkdir "TestF"
echo>".\TestF\note.txt"
echo %note%> ".\TestF\note.txt"
pause
:start
color e
cls
echo Put the note in here:
set /p LastNote=<".\TestF\note.txt"
set /p note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
set /p agree=
if "%agree%"=="y" (
goto replace
) else (
goto abort
)
There appears to be no reason to remove the Note Folder directory or even to delete the note.txt file.
How does it perform if you rewrite it like this?
#Echo Off
Color 0E
Set "LastNote="
If Exist "%UserProfile%\Desktop\Note Folder\note.txt" (
Set/P LastNote=<"%UserProfile%\Desktop\Note Folder\note.txt"
)
If Not Defined LastNote GoTo replace
:ask
ClS
Echo=The last note you saved was:
Echo=%LastNote%
Echo=
Choice /M "Would you like to replace this note with a new one"
If "%ErrorLevel%"=="1" GoTo replace
Echo=
Echo=Aborted!
Timeout -1
GoTo ask
:replace
ClS
Set/P "note=Put the note in here: "
If "%note%"=="" GoTo ask
Echo=Note has been saved!
If Not Exist "%UserProfile%\Desktop\Note Folder\" (
MD "%UserProfile%\Desktop\Note Folder"
)
(Echo=%note%)>"%UserProfile%\Desktop\Note Folder\note.txt"
Timeout -1
GoTo ask
For the benefit of the OP, here is your version of your script with the major flaws removed and with unnecessary lines kept in.
#goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
rmdir "D:\Users\Eldar\Desktop\Note Folder"
mkdir "D:\Users\Eldar\Desktop\Note Folder"
echo.>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
>"D:\Users\Eldar\Desktop\Note Folder\note.txt" echo %note%
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
goto replace
) ELSE (
GOTO abort
)
I can use ERRORLEVEL, but tried and with a loop it failed.
I am writing a batch "shell."
Since I have tried and tried, I am finally asking for help.
The reason I don't want to use errorlevel is because the loop.
(FULL) SHELL
#set /p build=<"C:\Users\%username%\Desktop\WellOS2\WellOS\Build".txt
#title WellOS V.%build%
#echo off
goto boot
:register
cls
echo You are registering...
echo If this is an error press CTRL + C NOW...
pause
cls
set /p user= Enter your username:
set /p passwordreg= Enter your password:
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%"
mkdir "C:\Users\%username%\Desktop\WellOS2\Users\%user%\Documents"
echo %passwordreg% >"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
echo 2 >"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
echo Your done.
pause
goto welloslog
:booterror
echo Sorry the boot file has an error. Check the user manual for BOOT$
pause
:boot
set /p boot=<"C:\Users\%username%\Desktop\WellOS2\OSfiles\bootset".txt
if %boot% == 1 goto register
if %boot% == 2 goto welloslog
goto booterror
cls
:ERROR
cls
echo ----------ERROR-------------------
echo %error%
pause
goto %back%
:welloslog
cls
echo Welcome to WellOS2!
echo ----------------LOGIN-------------
set /p user= Username:
if exist "C:\Users\%username%\Desktop\WellOS2\Users\%user%" goto pass
set error= Sorry that account doesn't exist.
set back=welloslog
goto welloslogerror
:pass
set /p password=<"C:\Users\%username%\Desktop\WellOS2\Users\%user%\password".txt
set /p passwordlog= Password:
if /i %passwordlog% == %password% goto wellos
set error= Sorry! wrong password.
set back= welloslog
goto error
:wellos
cls
:wellosnocls
echo --------------MAIN---------------
echo type help for help
set /p command= #:
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" set type=sys
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" set type=pro
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.sys" goto po
if exist "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.pro" goto po
set error= !Unreconized program/system program!
set back=wellos
goto error
:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode"
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep
PROGRAM OPENER (What I am talking about, and having problems with...)
:po
set lines=0
echo --------------%command%.%type%---------------
:porep
set /a lines=%lines% + 1
set /p "code="<"C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%".wellcode
if "%code%"=="GOWELL" goto wellosnocls
findstr /I /L "if" "C:\Users\%username%\Desktop\WellOS2\Programdata\%command%.%type%\%command%.%type%-%lines%.wellcode" goto iftl
:skip
call %code%
goto porep
::Tools
:iftl
%code%
goto porep
findstr "targetstring" datafilename >flagfilename
for %%a in (flagfilename) do if %%~za==0 echo not found
for %%a in (flagfilename) do if %%~za neq 0 echo found
beyond that, your question is too vague.
The following command returns all lines of a text file textfile.txt that contain the word word (remove the /I switch if you want the search to be case-sensitive):
findstr /I /L "word" "textfile.txt"
With for /F you can capture the output and test whether it is empty, as the loop does not iterate if no match is encountered:
set "FOUND="
for /F "delims=" %%F in ('
findstr /I /L "word" "textfile.txt"
') do (
set "FOUND=Yes"
)
if defined FOUND (
echo One or more matches found.
rem do something...
) else (
echo No match found.
rem do something else...
)
Type for /? and if /? in command prompt to get details about the used commands.
There is also a way to use ErrorLevel implicitly, meaning you do not have to query its value by something like %ErrorLevel%, !ErrorLevel! or if ErrorLevel, namely when using conditional command separators:
the && separator executes the following command only in case the previous one succeeded, that is, it returned an ErrorLevel of 0; (findstr returns 0 in case a match is encountered;)
the || separator executes the following command only in case the previous one failed, that is, it returned an ErrorLevel other than 0; (findstr returns a non-zero ErrorLevel in case no match is encountered;)
The following line of code demonstrates the usage:
findstr /I /L "word" "textfile.txt" && (echo One or more matches found.) || echo (No match found.)
I want to create a batch file that echoes text that it receives as part of a user-inputted command.
#echo off
Echo execute a command
Set /p command=
if "%command%"=="echo 'some text'" goto echo
::my aim is to make "echo" ignored by System and Only read "'some text'"
:: like "set text_to_echo='some text'
:echo
echo %text_to_echo%
Asking the user for the command first and then asking the user what to echo, like below, is not an option
#echo /p
set /p text=
if "%text%=="echo"
:echo
set /p tex1="Enter text to echo"
echo "%tex1%"
You could use this:
#echo off
Echo execute a command
Set /p "command="
if NOT "%command%"=="%command:echo =%" goto echo
pause
:echo
SET "text_to_echo=%command:~5%"
echo.%text_to_echo%
pause
Note that I also fixed your if, and put a pause after your if so you know whether it goes to :echo or not.
You want to split your Input into a first word (token) and the rest. You can do this with a for:
#echo off
Echo execute a command
Set /p "commandstring=# "
for /f "tokens=1,* delims= " %%a in ("%commandstring%") do (
set "command=%%a"
set "params=%%b"
)
if /i "%command%"=="echo" goto :_echo
...
:_echo
echo %params%
REM also possible (at least for "echo", but I guess, that's just an example):
%command% %params%
Put this in a file called "mybat.bat"
(put it in c:\temp\
do NOT put it in c:\windows)
set __rightFolder=.
del "%__rightFolder%\system.ini.copy"
If NOT exist "%__rightFolder%\system.ini.copy" (
copy "%windir%\system.ini" "%__rightFolder%\system.ini.copy"
ECHO "%__rightFolder%\system.ini.copy"
SET /P AREYOUSURE="Do you want to run the dir command on '%__rightFolder%\system.ini.copy'"
IF /I "%AREYOUSURE%" EQU "Y" (
dir "%__rightFolder%\system.ini.copy"
)
)
PAUSE
When I run this simple script, the "IF /I" is running before I type anything in.
NOTE, I have a long question with quotes around the question.
It's probably something simple, but I'm .bat skillzz stink.
Here is my output.
C:\Temp>set __rightFolder=.
C:\Temp>del ".\system.ini.copy"
Could Not Find C:\Temp\system.ini.copy
C:\Temp>If NOT exist ".\system.ini.copy" (
copy "C:\Windows\system.ini" ".\system.ini.copy"
ECHO ".\system.ini.copy"
SET /P AREYOUSURE="Do you want to run the dir command on '.\system.ini.copy'"
IF /I "" EQU "Y" (dir ".\system.ini.copy" )
)
1 file(s) copied.
".\system.ini.copy"
Do you want to run the dir command on '".\system.ini.copy"'y
C:\Temp>PAUSE
Press any key to continue . . .
So I put in 'y', but it doesn't do the "dir" command.
Neverending story... EnableDelayedExpansion
Delayed Expansion will cause variables to be expanded at execution
time rather than at parse time, this option is turned on with the
SETLOCAL command. When delayed expansion is in effect variables may
be referenced using !variable_name! (in addition to the normal
%variable_name% )
Patched:
set __rightFolder=.
del "%__rightFolder%\system.ini.copy"
If NOT exist "%__rightFolder%\system.ini.copy" (
copy "%windir%\system.ini" "%__rightFolder%\system.ini.copy"
ECHO "%__rightFolder%\system.ini.copy"
SET /P "AREYOUSURE=Do you want to run the dir command on '%__rightFolder%\system.ini.copy' "
SETLOCAL enabledelayedexpansion
IF /I "!AREYOUSURE!" EQU "Y" (
dir "%__rightFolder%\system.ini.copy"
)
ENDLOCAL
)
PAUSE