batch file ERROR which says ECHO IS OFF [duplicate] - batch-file

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed last year.
i have written this batch file code but it has this error i appreciate u if answer me .
the error is that when i choose 2 it gone to ask me to enter desire site but when i write the address it has an error says ECHO IS OFF
HELP ME WHAT SHOULD I DO...
#echo off
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo %s%
pause >nul
)
pause >nul

It's not an error message, it's what happens when you run echo without arguments. In other words, you see this because %s% winds up being empty.

To fix your problem use enabledelayedexpansion as in a block % will expand to the value prior to the block:
#echo off
setlocal enabledelayedexpansion
title trace
:main
echo 1)TRACE GOOGLE
echo 2)TRACE YOUR SITE
set /p choice= Enter your choice:
echo %choice%
if %choice%==1 (
tracert www.google.com
goto main
pause >nul
)
if %choice%==2 (
set /p s=Enter your desired site:
echo !s!
pause >nul
)
pause >nul
Which should work

When you have #echo off at the begening, and you echo a empty var, would get this message. Try to check your echoed vars, ensure them not empty.

Related

trying to make an "OS" in batch

#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.

How do I JUST replace text in a file (Like all of the text)

I've been searching for a while now and Google's terrible algorithm gives me everything but the aching question I've been asking. I'm trying to create a sort of password lock script, and I'm trying to create a lock out function. Here's what I have so far:
#echo off
set tries=3
:type
set /p mytextfile=< zepassword.txt
if %mytextfile%==LOCKED goto locked
echo Please type in your key.
set /p okay=
if %okay%==yes goto good
if not %okay%==yes goto bad
:good
cls
echo Correct Password.
pause >nul
exit
:bad
cls
set /a tries=%tries%-1
echo This key is incorrect. %tries% tries remaining.
pause >nul
if %tries%==0 echo LOCKED >> zepassword.txt
goto type
:locked
cls
echo You are LOCKED OUT.
echo Press any key to unlock.
pause >nul
echo UNLOCKED >> zepassword.txt
pause >nul
SomethingDark answered this question for me.
echo text > [File name]

Batch variables don't contain anything? [duplicate]

This question already has answers here:
Example of delayed expansion in batch file
(5 answers)
Closed 4 years ago.
I made a a database script to store giftcardcodes in. However, in my script the variables somehow don't contain anything. When I removed things that didnt matter in this question, I realised it worked.
WORKING:
::making variables
#echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
::write to txt
#echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
#echo off
color 0c
#echo Successfully written to database!
pause
exit
NOT WORKING:
:PASSWORDPROMPT
#ECHO off
setlocal EnableDelayedExpansion
#echo off
cls
#echo off
title Insert Password:
#echo off
set /p Passwort=Insert your Password here:
if %Passwort%==mypassword goto YES
if not %Passwort%==mypassword goto NO
:YES
if exist %CD%\assets\database.txt (
goto EXISTS
) else (
md %CD%\assets\
copy /b NUL %CD%\assets\database.txt
attrib +h %CD%\assets
)
:EXISTS
#echo off
title ENTER OR READ?
#echo off
cls
set /p readwrite=Do you want to ENTER a code to the database or READ the database? [ENTER/READ]
if /i %readwrite%==ENTER (
#echo off
title Please insert your code here:
cls
set /p code=Please insert your code here:
cls
title Who is the code from?
set /p client=Who is the code from?
cls
title For what did you get the money?
set /p what=For what did you get the money?
cls
title What's the amount of money saved on the code?
set /p amount=What's the amount of money saved on the code?
cls
#echo Code: %code%; Who: %client%; What: %what%; Money [in Euro]:%amount%>> %CD%\assets\database.txt
#echo off
color 0c
#echo Successfully written to database!
#echo off
color 0b
<nul set /p "=Press a key to Close..."
pause >nul
title Closing...
#echo off
for /L %%A in (3,-1,0) do (
cls
echo Closing in %%A Seconds.
ping localhost -n 2 >nul
cls
)
) else (
cls
#echo off
color
cls
#echo off
#echo ________________________________________________________
#echo YOUR CODES:
#echo ________________________________________________________
color
more %CD%\assets\database.txt
#echo ________________________________________________________
<nul set /p "=Press a key to Close..."
pause >nul
)
exit
:NO
cls
color 0c
#echo WRONG PASSWORD!
#echo off
timeout 1 /nobreak >null
#echo off
for /L %%A in (3,-1,0) do (
cls
echo Reload in %%A seconds...
ping localhost -n 2 >nul
)
cls
goto PASSWORDPROMPT
Does someone know what makes the difference and how I could fix the code?
You are experiencing the delayed-expansion issue. It seems that you have already enabled delayed expansion.
However, to preserve compatibility, we have to change the variables to be updated at runtime like so:
%var% -> !var!
Please note that it only applies to some variable. The following examples are invalid: !!~A, !1
For your situation, you should modify your code as so:
#echo Code: !code!; Who: !client!; What: !what!; Money [in Euro]:!amount!>> %CD%\assets\database.txt

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

batch file for making query to use with youtube-dl

I want to build a batch program to work with youtube-dl, and I want to ask some questions (choice command I think it should be) and depending on answers to build query which will be executed by main youtube-dl command.
Example:
echo Do you want to extract audio from this clip? (y/n)
echo Write subtitle file? (y/n)
And do this at the end:
youtube-dl -x --write-sub some link
I hope I was clear, english is not my first language. Thanks
Try this:
#echo off
setlocal
:Input
set opt1=
set opt2=
set /p opt1=Do you want to extract audio from this clip (y/n)
if not defined opt1 goto :Invalid
if .%opt1% EQU . goto :Invalid
echo %opt1%|findstr /i /r /c:"y" /c:"n">nul||goto :Invalid
set /p opt2=Write subtitle file (y/n)
if not defined opt2 goto :Invalid
if .%opt1% EQU . goto :Invalid
echo %opt2%|findstr /i /r /c:"y" /c:"n">nul||goto :Invalid
if /i %opt1% EQU Y set opts=-x
if /i %opt2% EQU Y set opts=%opts% --write-sub
echo youtube-dl %opts% some link
goto :eof
:Invalid
Echo You have entered an invalid option. Please enter only y or n
goto :Input

Resources