What is wrong with the following code?
#echo off
:fromFirst
set /p TN="Enter your name: ":
echo %TN%
set tim= %TIME%
echo %tim%
set det=%TN%%tim%
echo %det% >> List.txt
set /p ch="Do you want to continue(y/n):" :
IF "%ch%"=="y"
(
goto :fromFirst
)
ELSE(
exit
)
Could you please help me in solving this?
Why do you need to set up variable in det when you have already setup the same in earlier in TN for name tim for time?
The below batch should work fine:
#echo off
:fromFirst
cls
set /p TN=Enter Your Name :
echo %TN%
set tim=%TIME%
echo %tim%
echo %TN% %tim% >>List.txt
set /p ch=Do You Wish to Continue? (y/n)
if "%ch%"=="y" goto fromFirst else exit
There must be a separator before the opening parenthesis of the IF true-condition target
That opening parenthesis must be on the same physical line as the if
Where an 'else' clause is used, the ending parenthesis of the "true" block, a separator and the else keyword must be on the same physical line
Where an 'else' block is used, the else keyword, a separator and the opening parenthesis of the "else" block must be on the same physical line
ie
if condition (
something
) else (
someotherthing
)
Related
When I run the script Console prints goto was unexpected time, (I have bad English)
#echo on
set versiyon=0.1
if %komut1%=="metin" goto metinapi
if %komut1%=="internet" goto internetapi
if %komut1%=="dosya" goto dosyaapi
if %komut1%=="sistem" goto sistem
if %komut1%=="versiyon"(
echo %versiyon%
pause
)
color c
echo Hata:Aranan sey bulunamadi!Aradiginiz seyi dogru yazdiginizdan emin
olun!ya da frameworku guncelleyin. suanki verisyon:%versiyon% KOD:1
pause
:metinapi
if %komut2%=="degistir"(
if %komut3%=="" goto null
if %komut4%=="" goto null
if %komut5%=="" goto null
setlocal ENABLEDELAYEDEXPANSION
set str=%komut3%
set str=%str:!komut5!=!komut4!%
echo %str%
pause
)
if %komut2%=="bul"(
if %komut3%=="" goto null
if %komut4%=="" goto null
Echo.%komut3%| findstr /C:%komut4%>nul && (Echo.EVET) || (Echo.HAYIR)
pause
)
:null
color c
echo Deger null olamaz!KOD:2
pause
You can say why komuts are empty beacause I want user input them like api.bat set komut1=metin set komut2=bul ...
You haven't declared %komut1%, and therefore the if command failed as well. And if the command if executed (right at the line #3), then it would be:
if =="metin" goto metinapi
^
Error here, because "%komut1%" is equal to ""
To fix this, you may have to include double quotes " before and after %komut1%:
if "%komut1"=="metin" goto metinapi
Now not only that, but you may have to edit more commands in order to make it working, like after you fix line #3, you'll have to fix line #4 and #5, etc...
I have been having problems with making a batch script that can write into a old text file. When i try to run it with the old text file it says that "access is denied" (so when i try to put any text into the existing file)
Anyways here is the code:
#echo off
title file editor/creator
set lineNR=0
:start
set /p ANS= Do you want to access a 1:"old file" or 2"create a new" [1/2]
if %ANS% EQU 1 ( goto old
) ELSE if %ANS% EQU 2 ( goto new
) ElSE ( echo invalid input & goto start)
:old
set /p name = what is the name of the file
set /p ending = what type of file is it
goto loop
:new
set /p name= what do you want the name of the file to be
set /p ending= what type of file do you want the file to be
echo %name%.%ending%
:Q1
set /p echo_off= do you want echo to be off? [y/n]
if %echo_off% EQU y (
echo #echo off >%name%.%ending%
goto loop
) ELSE IF %echo_off% EQU n (
goto loop
) ELSE (
goto Q1
)
:loop
echo press CTRL+LSHIF+C to end loop
goto loop1
:loop1
set /a lineNR=%lineNR% + 1
set /p line= %lineNR%:
echo %line% >>%name%.%ending%
// this is where it says that access is denied
goto loop1
It is just a simple, but a common issue.
set a = b
Creates a variable named a (with the space) and a value of b(with the space).
Remove the spaces and it will work.
I am writing a batch script as follows:
#echo off
setlocal enabledelayedexpansion
set Current_Node="Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)
Now,according to above code it should not go to the else part but in actual it is always entering to the else part.
I am not getting whether the problem is in syntax or the way i am comparing the strings.
So, please help me out.
You have too many quotes. Don't use quotes when setting the value or do not use quotes on "%Current_Node%".
When you use Current_Node="Node1", %Current_Node% will be equal to "Node1", but "%Current_Node%" will be equal to ""Node1"".
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
echo %Current_Node%
if "%Current_Node%" == "Node1" (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)
As suggested, there are quotes used where not necessary. I believe you need to use EQU instead of == etc. Try below if still having trouble.
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
if %Current_Node% EQU Node1 (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)
Double quotes are sometimes needed when setting a variable, so the solution can be simply to wrap the term in double quotes as seen in line two below. They do not become part of the variable.
#echo off
set "Current_Node=Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)
I have spend lot of time but could not understand why entered value is showing blank in the echo command
Here is the execution:
Enter Name ssss
Entered name is ""
Thanks for your help
#echo off
:Input_cname
echo .
set c_name=
set /p c_name = Enter Name
echo Entered name is "%c_name%"
if not defined c_name goto Input_cname
if /i "%c_name:"=%" == "end" GOTO End
:End
#echo off
:Input_cname
echo .
set c_name=
set /p c_name= Enter Name
echo Entered name is "%c_name%"
if not defined c_name goto Input_cname
if /i "%c_name:"=%" == "end" GOTO End
:End
remove space before the equal sign because it will became part of the variable name.
Whats wrong with this code?
IF "%language%" == "de" (
goto languageDE
) ELSE (
IF "%language%" == "en" (
goto languageEN
) ELSE (
echo Not found.
)
I'm not really good in Batch..
#echo off
title Test
echo Select a language. (de/en)
set /p language=
IF /i "%language%"=="de" goto languageDE
IF /i "%language%"=="en" goto languageEN
echo Not found.
goto commonexit
:languageDE
echo German
goto commonexit
:languageEN
echo English
goto commonexit
:commonexit
pause
The point is that batch simply continues through instructions, line by line until it reaches a goto, exit or end-of-file. It has no concept of sections to control flow.
Hence, entering de would jump to :languagede then simply continue executing instructions until the file ends, showing de then en then not found.
#echo off
set "language=de"
IF "%language%" == "de" (
goto languageDE
) ELSE (
IF "%language%" == "en" (
goto languageEN
) ELSE (
echo Not found.
)
)
:languageEN
:languageDE
echo %language%
This works , but not sure how your language variable is defined.Does it have spaces in its definition.
batchfiles perform simple string substitution with variables.
so, a simple
goto :language%language%
echo notfound
...
does this without any need for if.
Recommendation. Do not use user-added REM statements to block batch steps. Use conditional GOTO instead.
That way you can predefine and test the steps and options. The users also get much simpler changes and better confidence.
#Echo on
rem Using flags to control command execution
SET ExecuteSection1=0
SET ExecuteSection2=1
#echo off
IF %ExecuteSection1%==0 GOTO EndSection1
ECHO Section 1 Here
:EndSection1
IF %ExecuteSection2%==0 GOTO EndSection2
ECHO Section 2 Here
:EndSection2
#echo off
color 0a
set /p language=
if %language% == DE (
goto LGDE
) else (
if %language% == EN (
goto LGEN
) else (
echo N/A
)
:LGDE
(code)
:LGEN
(code)