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
Related
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 want to check if count exists in mycountry or not, and then do some operations according.
My code snippet :
rem #ECHO OFF
cls
SET FILE="mycountry"
If true I want to run 3 statements and if false I want to run 3 other statements.
I have tried this combination:
Echo.%FILE% | findstr /C:"count">nul && (Echo.TRUE) || (Echo.FALSE)
But how to write multiple statements if the condition gets true? I don't wanna use any flag variable.
Below snippet is not working.
Echo.%FILE% | findstr /C:"count">nul &&
(
Echo.TRUE
echo "ran correct."
)
|| (Echo.FALSE)
You can use the %errorlevel% value combined with an if/else.
See the example below:
REM #echo off
cls
SET FILE="mycountry"
SET STR="TEST"
findstr %STR% %FILE% >nul
if %errorlevel% equ 1 (
goto searchError
) else (
goto searchSucces
)
:searchSucces
echo String %STR% found in file %FILE%
pause
exit
:searchError
echo String %STR% not found in file %FILE%
pause
exit
Your code, (integrated into a batch file), appears to work as expected:
#Echo Off
Set "FILE="
Set /P "FILE=Enter String: "
If Not Defined FILE Exit /B
Echo.%FILE% | findstr /C:"count">nul && (Echo.TRUE) || (Echo.FALSE)
Pause
In addition, the following two methods both appear to work as expected:
Using Echo and FindStr (as in your code):
#Echo Off
Set "FILE="
Set /P "FILE=Enter String: "
Echo=%FILE%|FindStr /IC:"count">Nul 2>&1&&(Echo TRUE
Echo Ran correct.
Timeout 3 /NoBreak>Nul
Echo Still running!)||Echo FALSE
Pause
Using variable substitution:
#Echo Off
Set "FILE="
Set /P "FILE=Enter String: "
If /I "%FILE:count=%"=="%FILE%" (Echo FALSE) Else (Echo TRUE
Echo Ran correct.
Timeout 3 /NoBreak>Nul
Echo Still running!)
Pause
If the examples above do not work for you, you should edit your question to include the actual code and strings you're using in your real world scenario. We cannot fix something we cannot see, especially if you don't fully explain the issue, (snippet is not working is a statement only, not an explanation).
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.
I looked for quite a while and couldn't find a good solution to my problem. I want a batch program to "look" in a .txt file for the command "" and if that word is in it, then to execute a different command. If the command existed, I would want to do something like set %textfile%=text.txt and if that worked I would then do if %textfile%==update goto update which would be an easy way to start an automatic update if this was in a loop. So basically, is there a command that sets a text file in %text%? This is the code that I am trying to add this into:
#echo off
color 0f
:start
echo Welcome to Master control pannel
ping 127.1 -n 4 >nul
cls
:options
cls
echo What would you like to do first? (Type the number of the operation you want to start)
echo.
ping 127.1 -n 2 >nul
echo 1. Run a command off of all computers
::(I want to run a command by sending a message to a text file but want recieving computor to be able to read it and execute it, how could I read the command and then do what it say, for example, if the command says "echo Hello" I would want recieving computor to say "Hello" )
echo 2. Stops the current command
echo 3. List all computers
echo 4. Open remote shutdown program
echo 5. Delete a computor (in progress)
echo 6. (Unfinished)
echo 7. (Unfinished)
echo 8. (Unfinished)
echo 9. (Unfinished)
echo 10. Exit
set /p choose=(1-9):
if %choose%==1 goto o1
if %choose%==2 goto o2
if %choose%==3 goto o3
if %choose%==4 goto o4
if %choose%==5 goto o5
if %choose%==6 goto close
if %choose%==7 goto close
if %choose%==8 goto close
if %choose%==9 goto close
if %choose%==10 goto o10
goto options
:close
cls
goto start
:o1
echo Stopping current command
del command.txt
echo. 2>command.txt
echo Command stopped!
pause
cls
goto start
I would greatly appreciate some help or comments to what I could do or add to this. Thanks!
Not an answer but several hints.
a variable can hold only single lines not a whole file.
if you want to get the first line of a file into a var use `Set /P "var="
set %textfile%=text.txt would store test.txt literally into a var whose name is the content of the var textfile.
you are mixing goto o1 and :01 with the label
`
So, how I have it done right now, is that it that it calls another bat file to update it, and then that batch file updates, and sets %ERRORLEVEL% to 1. At the start of the original program, it checks if errorlevel is 1, if yes, it goes to the main menu, but right now, it doesn't call the update file, it just goes to the menu. This is my code
Main program
IF %errorlevel% EQU 1 goto begin
call updater.bat
:begin
echo MENU
Updater
set=errorlevel 1
wget (updatelink here)
call mainprogram.bat
Right now, sometimes it works, sometimes it doesn't, which leads me to believe that some command is somehow increasing the errorlevel, but the only code before the errorlevel check is
#echo off
color 0f
cls
set currentver=v0.5.6
(check code)IF %errorlevel% EQU 1 goto begin
https://code.google.com/p/flashcart-helper/source/browse/trunk/0.6/FlashcartHelperRobocopy.bat
Here is what I have right now.
Don't play around with errorlevel. It's an internal variable. At the start of a batch, errorlevel will be 0 because all you've done is set a local variable. This will almost always ( never say never ) succeed. Also, if errorlevel is 1, and I'm reading this correctly you also seem to have an infinite loop? From what I understand of what you've said your batches are like this:
Main
#echo off
color 0f
cls
set currentver=v0.5.6
IF %errorlevel% EQU 1 goto begin
call updater.bat
:begin
echo MENU
Updater
set=errorlevel 1
wget (updatelink here)
call mainprogram.bat
As errorlevel get's overwritten each time you do anything you're asking for trouble. Change %errorlevel% to %error% and it should solve your problems. As it's a local environment variable it should also be passed between batch files. Just be careful not to use error elsewhere.
Here is a solution using Dropbox Public Folders and no wget. It uses PowerShell that in on Win7+ machines.
Update the below https://dl.dropboxusercontent.com/u/12345678/ url with your own.
It auto creates a .conf file for configuration.
Set __deploy_mode to 1 for the file on dropbox so the version file can be updated but the script not accidentally executed.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET time_start=%time%
SET time_choice_wait=20
SET script_ver=1.00
SET script_name=%~n0
SET server_url=https://dl.dropboxusercontent.com/u/12345678/
SET script_name_bat=%~dp0%script_name%.bat
SET script_name_cfg=%~dp0%script_name%.conf
SET script_name_latest_ver=%~dp0%script_name%.latest.ver
ECHO %script_name% v%script_ver%
ECHO %script_ver% > %script_name%.current.ver
IF NOT EXIST "%script_name_cfg%" CALL :SCRIPT_MISSING_CFG
FOR /f "delims=" %%x IN (%script_name%.conf) DO (SET "%%x")
IF %__deploy_mode% EQU 1 GOTO :EOF
IF %auto_update_compare% EQU 1 CALL :SCRIPT_COMPARE_VER
:SCRIPT_MAIN
REM =======================================
REM === EDIT BELOW THIS LINE ==
REM TODO Add main content
ECHO.
ECHO Waiting for content...
REM === EDIT ABOVE THIS LINE ==
REM =======================================
GOTO END
:SCRIPT_MISSING_CFG
ECHO Creating new %script_name%.conf file...
ECHO __deploy_mode=0 > "%script_name_cfg%"
ECHO repository_base_url=%server_url% >> "%script_name_cfg%"
ECHO auto_update_compare=1 >> "%script_name_cfg%"
ECHO auto_update_download=1 >> "%script_name_cfg%"
ECHO Update %script_name%.conf as needed, then save and close to continue.
ECHO Waiting for notepad to close...
NOTEPAD "%script_name_cfg%"
GOTO :EOF
:SCRIPT_COMPARE_VER
ECHO Please wait while script versions are compared...
Powershell -command "& { (New-Object Net.WebClient).DownloadFile('%server_url%%script_name%.current.ver', '%script_name_latest_ver%') }"
IF NOT EXIST "%script_name_latest_ver%" GOTO END
SET /p script_latest_ver= < "%script_name_latest_ver%"
IF %script_ver% EQU %script_latest_ver% CALL :SCRIPT_COMPARE_VER_SAME
IF %script_ver% NEQ %script_latest_ver% CALL :SCRIPT_COMPARE_VER_DIFF
GOTO :EOF
:SCRIPT_COMPARE_VER_SAME
ECHO Versions are both %script_name% v%script_ver%
GOTO :EOF
:SCRIPT_COMPARE_VER_DIFF
ECHO Current Version:%script_ver% ^| Server Version:%script_latest_ver%
IF %auto_update_download% EQU 1 GOTO SCRIPT_DOWNLOAD_SCRIPT
ECHO.
ECHO Would you like to download the latest %script_name% v%script_latest_ver%?
ECHO Defaulting to N in %time_choice_wait% seconds...
CHOICE /C YN /T %time_choice_wait% /D N
IF ERRORLEVEL 2 GOTO SCRIPT_DOWNLOAD_NOTHING
IF ERRORLEVEL 1 GOTO SCRIPT_DOWNLOAD_SCRIPT
IF ERRORLEVEL 0 GOTO SCRIPT_DOWNLOAD_NOTHING
:SCRIPT_DOWNLOAD_SCRIPT
ECHO Please wait while script downloads...
Powershell -command "& { (New-Object Net.WebClient).DownloadFile('%server_url%%script_name%.bat', '%script_name_bat%') }"
ECHO Script Updated to v%script_latest_ver%^^!
REM User must exit script. Current batch is stale.
GOTO :END
:SCRIPT_DOWNLOAD_NOTHING
GOTO :EOF
:END
SET time_end=%time%
ECHO.
ECHO Script started:%time_start%
ECHO Script ended :%time_end%
:END_AGAIN
pause
ECHO.
ECHO Please close this window
ECHO.
GOTO END_AGAIN
You can do that through these steps:
1.put two files in server,a config file, a higher version bat file which need to update; set last version num. in config file.
2.client bat should be checked update at every startup time. you can read the news version in server config file, then compared to local bat file version. if not equal, so do update, else other wise.
Do you have any problems?