I am writing a basic batch script and I need to acquire user input.
The script appears to crash when I call the user input vars in an echo command.
(This is probably something very basic but I can't see the error...)
#echo off
echo ********************************
echo SHUTDOWN/RESTART
echo ********************************
echo.
set /p answer="Restart (R) or Shutdown(S)? "
set /p time="How soon ? "
echo you have selected %answer% in %time% seconds.
if "%answer%"=="" goto error
if "%time%"=="" goto error
if "%answer%"=="R" (
goto restart
) else (
goto shutdown
)
echo.
echo.
:restart
FOR /L %%AA IN (1,1,%1) DO (
echo Restarting in %%AA ...
)
The FOR command requires a single alphabetic character for its controlled variable. %AAA is invalid.
Related
So i have this simple homework of doing a batch file that shows 3 options, and an error message for the entry for the option. so far it seems simple for me. I saw a youtube video and according to what i know it suppose to run ok but the problem is that when I run it and choose option 2 it opens also option 1, and when I choose option 3 it doesn't exit the program. And when I choose a parameter that is not specify the error message do not show. I am gonna copy paste my batch file because is easy to understand.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p Op=Write the option:
if %op%==1 (
start %windir%\system32\cleanmgr.exe
)
else if %op%==2 (
start %windir%\system32\dfrgui.exe
)
else if %op%==3 (
exit
)
else (
cls
echo Error not defined
)
pause
Is there something that I am missing or wrote wrong please let me know. If you can copy paste and run it in your computer and tell me if it is working fine because something tell me that in the process of learning I try a few times and i think I messed up with something that suppose to run correctly the file since my instructor told me that we have to be careful when using the commands in a batch file.
Your code formatted correctly but is susceptible to command injection because of the usage of the SET /P command.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p Op=Write the option:
if "%op%"=="1" (
start "" "%windir%\system32\cleanmgr.exe"
) else if "%op%"=="2" (
start "" "%windir%\system32\dfrgui.exe"
) else if "%op%"=="3" (
exit /b
) else (
cls
echo Error not defined
pause
)
A best practice solution which handles invalid input options.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
choice /C 123 /N /M "Select an Option:"
set "op=%errorlevel%"
if "%op%"=="1" start "" "%windir%\system32\cleanmgr.exe"
if "%op%"=="2" start "" "%windir%\system32\dfrgui.exe"
if "%op%"=="3" exit /b
I did it with this code.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk Cleanup Utility
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p op/3=Write the number option:
if %op/3%==1 (
start %windir%\system32\cleanmgr.exe
)
if %op/3%==2 (
start %windir%\system32\dfrgui.exe
)
if %op/3%==3 (
exit
)
else (
cls
echo Error not defined
)
pause
the only problem that I have is that the error message appears even though options 1 and 2 are selected, it executes the options but it also shows the error message. I only want that the error message to show if any of those parameters aren't chosen.
I know that else is responding because if I choose option 1 else it executes because is not number 2 or 3. What it comes to my mind is if it is possible to define the op variable in an enumeration format like a group set of options.
I'm trying to echo a variable into an autoexec.cfg file but I'm having some trouble with echoing a string and a variable to a file simultaneously.
I've tried putting quotes around the echo statement like
echo "sv_maxplayers %answer%">%location%
and, while it echos the string correctly, it includes the quotations marks.
("sv_maxplayers 4")
My script is currently
#ECHO OFF
set location=".\Risk of Rain 2_Data\Config\autoexec.cfg"
set /p answer="Please enter a number 1-16:"
if %answer% GTR 0 (
if %answer% LSS 17 (
goto set
)
)
echo Invalid argument.
goto exit
:set
echo sv_maxplayers %answer%>%location%
echo Done!
goto exit
:exit
pause
exit
Running that returns
Please enter a number 1-16:4
sv_maxplayers
Done!
Press any key to continue . . .
in the console and clears autoexec.cfg.
It should output as
sv_maxplayers 4
Any idea what I'm doing wrong?
Found the solution! I was missing some spaces.
echo sv_maxplayers %answer%>%location%
needs to be
echo sv_maxplayers %answer% > %location%
I am new to batch coding and I am having an issue where whenever I select an option, it goes to 1 (which is :MEMBERS) even if I enter 2 or 3. I am unsure why this is happening.
#echo off
cls
color A
:MENU
cls
echo ===============================
echo Mavast Client v0.01
echo ===============================
echo Enter a number from the list and hit enter
echo to run that program.
echo 1. Members
echo 2. Support us
echo 3. Exit
set /p var= Run:
if %var%=="1" goto MEMBERS
if %var%=="2" goto SUPPORT
if %var%=="3" goto EXIT
:MEMBERS
cls
echo Owner/Rifler = C0MpL3T3
echo Admin/Stratcaller = TDGR (The Dutch Grim Reaper)
echo Awper = NONE
echo Lurker = NONE
echo Entry Fragger = NONE
echo press any key to continue
timeout -1 > NUL
goto MENU
:SUPPORT
cls
echo Support us by donating BitCoins or skins
echo to donate skins, just trade them to TDGR or C0MpL3T3.
echo to donate bitcoins, send any amount you want to this address:
echo 1DSRHe7L842MZjwTWYPzgGzbsZ8zU97w9g
echo thank you!
echo press any key to continue
timeout -1 > NUL
goto MENU
:EXIT
cls
exit
try with
if "%var%"=="1" goto MEMBERS
if "%var%"=="2" goto SUPPORT
if "%var%"=="3" goto EXIT
Quotes are also compared in IF conditions.
I have this code:
loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
%Lua% >> LuaCode.lua
if %Lua% == "LuaIDE.Exit()" ( echo Thanks for using LuaIDE!
timeout /t 3 /nobreak >nul
exit )
goto loop
and what i want it to do is get User-Input and put it into a file and if the user input is "LuaIDE.Exit()" I would like it to say a message for 3 seconds and exit but it just says:
'whatever you type in' is not recognized as an internal or external command,
operable program or batch file.
Why does it do this?
You have a few bugs. Try this:
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
:loop
set /p Lua=Lua:
echo %Lua% >> LuaCode.lua
if %Lua%==LuaIDE.Exit() goto exit
goto loop
:exit
echo Thanks for using LuaIDE!
ping 1.1.1.1 -n 3 -w 1 >nul
exit
Try this:
#echo off
:loop
echo -- File Created With LuaIDE. Do NOT Remove This Line > LuaCode.lua
set /p Lua=Lua:
echo %Lua%>>LuaCode.lua
if /i "%Lua%" == "LuaIDE.Exit()" echo Thanks for using LuaIDE & timeout /t 3 /nobreak >nul & exit
goto loop
Let me know how it works.
stop complicating a simple task....
the code is as simple as
#echo off
: start
echo.
set /p input= type your text here:
echo %input%>>myfile.extension
timeout /t 5 >null
cls
goto start
replace myfile with name of your file you want to send text.
replace extension with any that you want txt,log , etc
also you can specify path of your file e,g
echo %input%>>path/directory
Basically I have created an choice batch that every so often I can archive the contents of my boot data since I change it quite often and it works absolutely perfect, however I face the problem that every time I compile the batch from .BAT to.EXE with Advanced BAT to EXE converter the command 'bcdedit' never works and says “Not recognised as an internal or external command, operable program or batch file.”
Now the first thing I did was to make sure if I had the environment variables directed on my hard drive and it seemed fine:
Path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Users\Support\DOCUME~1\MYFILE~1\Programs\SYSTEM~1\DISKEE~1\
It has all the variables needed to work perfectly plus a few external ones which is ok then I thought that maybe I should try to make it direct on my folder, by doing some research I found on another forum:
Setting or Modifying a System Wide Environment Variable In CMD.EXE
I made a separate .BAT and I called it and it seems it did create the separate variable:
C:\Users\???\Documents\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Users\Support\DOCUME~1\MYFILE~1\Programs\SYSTEM~1\DISKEE~1" /f
But It Still Didn't Work!
I read upon another form:
Syswow64 Redirection
The person solution was to use Create Process () to create a program that automatically redirects him to the folder using sysnative (System32) but I'm a total beginner at C#, CMD is my strongest area when it comes to coding.
Please please I beg of you to help me as soon as possible and if you do have an answer please state in the easiest way possible and why. Here's my script just in case:
:START
echo.
echo Call apath.bat
echo.
echo.
echo (E) - Start Process.
echo (C) - Launch Part 2 Of Process.
echo (N) - Load NoDrives Manager.
echo (D) - Display Currect Account and Computer Information
echo (X) - Exit EDIM.
echo.
echo NOTE - It will not work if not started with Admin Privillages.
echo.
:Choice
set/p Option=Please enter your option:
if '%Option%' == 'E' goto :Incognito
if '%Option%' == 'C' goto :Touch
if '%Option%' == 'N' goto :Manager
if '%Option%' == 'D' goto :Data
if '%Option%' == 'X' goto :Exit
echo.
echo.
echo Invalid Option - Please Reselect.
goto :Choice
:RetryE
Echo An Error was found!
set/p RetryE=Retry? Y/N:
if '%RetryE%' == 'Y' goto :Incognito
if '%RetryE%' == 'N' goto :Exit
:Incognito
Timeout 5
echo.
echo.
echo.
Echo Saving OriginalBCD...
bcdedit /export "Data/BCD/OriginalBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Checking presence of BCD...
IF NOT EXIST "Data/BCD/OriginalBCD" goto :RetryE
Echo Deleting Boot Entry...
bcdedit /delete {current}
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Saving EditedBCD...
bcdedit /export "Data/BCD/IncogBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryE
IF %ERRORLEVEL% LSS 0 goto :RetryE
Echo Checking presence of BCD...
IF NOT EXIST "Data/BCD/IncogBCD" goto :RetryE
Echo Allowing User Control For Assigning System Reserved Partitions...
Echo -Commands-
Echo Diskpart
Echo List volume
Echo Select Volume "" (The one that has no Letter and remember number)
Echo Assign Letter=Z
Echo Select Volume "" (The one that has Letter E and remember number)
Echo Remove Letter=E (This is the new system reserved partition)
Echo Then exit Diskpart to finish Part 1.
Diskpart
Echo Ready To Restart!
Timeout 5
Shutdown /r /t 30
Goto :Start
:RetryC
set/p RetryE=Retry? Y/N:
if '%RetryC%' == 'Y' goto :Touch
if '%RetryC%' == 'N' goto :Exit
:Touch
echo.
echo.
echo.
Echo Loading NDM...
Echo NOTE - Store the password somewhere safe!!!
Start "" "Data/NDM.exe"
Echo Loading EditedBCD...
bcdedit /import "Data/BCD/IncogBCD"
IF %ERRORLEVEL% GTR 0 goto :RetryC
IF %ERRORLEVEL% LSS 0 goto :RetryC
Echo Process Complete!
Timeout 5
Echo Returning to menu...
goto :Start
:Manager
echo.
Echo NOTE - Store the password somewhere safe!!!
Start "" /WAIT "Data/ndm.exe"
Echo Returning to Menu...
goto :Start
:Data
echo.
echo.
echo.
echo Processing Data...
Systeminfo
diskpart /s "Data/discpart.txt"
Echo Returning to Menu...
goto :Start
:Exit
Exit
Thanks So Much!
bcdedit is on the path by default so unless you are reusing the PATH variable then the problem is in the batch compiler.
You can test this by using the plain batch file - and if it works then you know where the problem is.