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.
Related
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
)
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.
#echo off
set /p a=Enter 1,2,3:
if %a%==1
echo you entered 1
if %a%==2
echo you entered 2
if %a%==3
echo you entered 3
The conversion from a variabel that is a integer to a user input that is also an integer may require quotations but this is not the error that is preventing me from using this method in code. I've used this so many time it is embarrasing that i forgot how to do it. Thanks for help.
The entire IF statement must reside on one line
#echo off
set /p a=Enter 1,2,3:
if %a%==1 echo you entered 1
if %a%==2 echo you entered 2
if %a%==3 echo you entered 3
unless you use parentheses
#echo off
set /p a=Enter 1,2,3:
if %a%==1 (
echo you entered 1
)
if %a%==2 (
echo you entered 2
)
if %a%==3 (
echo you entered 3
)
or line continuation
#echo off
set /p a=Enter 1,2,3:
if %a%==1 ^
echo you entered 1
if %a%==2 ^
echo you entered 2
if %a%==3 ^
echo you entered 3
I'm trying to make a helpful batch file for work and for it, I'm trying to input notes/text to a .txt file via a variable. It works great with just entering text without spaces (ex: "test"), but once you type something with at least 1 space in it, the cmd closes out. (ex: "test test") I can't figure out why this is happening so I'm left with you guys here. Any/all help would be appreciated!
#echo off
color 9F
title Notes
CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> %UserProfile%\Documents\Notes.txt
:StartCallNotes
CLS
echo ================== Notes =================
type %UserProfile%\Documents\Notes.txt
echo ==========================================
set /p NotesEnter=Enter Notes:
set NewNote="%NotesEnter%"
if %NotesEnter% == CLS goTo :CLS
echo %NotesEnter% >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
:CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
exit
I think the problem is when you compare the user's input with CLS.
Try to put quotes in %NotesEnter% when compare the value like this :
#echo off
color 9F
title Notes
CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> %UserProfile%\Documents\Notes.txt
:StartCallNotes
CLS
echo ================== Notes =================
type %UserProfile%\Documents\Notes.txt
echo ==========================================
set /p NotesEnter=Enter Notes:
set NewNote="%NotesEnter%"
REM if user doesn't input the value echo a new line
if "%NotesEnter%" == "" echo. >> "%UserProfile%\Documents\Notes.txt"
if "%NotesEnter%" == CLS goTo :CLS
if NOT "%NotesEnter%" == "" echo %NotesEnter% >> "%UserProfile%\Documents\Notes.txt"
REM reset the variable value
set NotesEnter=
goTo :StartCallNotes
:CLS
del %UserProfile%\Documents\Notes.txt
echo Issue/Request: >> "%UserProfile%\Documents\Notes.txt"
goTo :StartCallNotes
exit
I am doing user input and checking to see if they typed n or y... and it's not working because it says both either way.
Here's what I have:
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if (%isit% == "y") goto :saidyes
if (%isit% == "n") goto :saidno
:saidyes
echo Hooray!
:saidno
echo Aww
PAUSE
First you can add a default goto after the two if's.
Then, in both tests you have to add the quotes around %isit% and to remove the parenthesis. You may also add the /I flag to do an insensitive string comparison.
Finally, add goto after each echo to jump over the next one.
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if /I "%isit%" == "Y" goto :saidyes
if /I "%isit%" == "N" goto :saidno
goto :error
:saidyes
echo Hooray!
goto :end
:saidno
echo Aww
goto :end
:error
echo ERROR
:end
PAUSE
Need change in syntax
Here is the modified code
#echo off
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if "%isit%" == "Y" GOTO saidyes
if "%isit%" == "N" GOTO saidno
:saidyes
echo Hooray!
GOTO paused
:saidno
echo Aww
:paused
PAUSE
....
In above example The Y/N is supposed to be capital letter only.
There are a few things I would do differently to this to ensure that it works properly...
First off the corrected code is the following, but at the bottom is my suggested code:
#echo off
setlocal
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
echo You typed: %isit%
if '%isit%'== 'Y' goto saidyes
if '%isit%'=='N' goto saidno
:saidyes
echo Hooray!
goto end
:saidno
echo Aww
goto end
:end
pause
endlocal
exit
However, to make sure that you only get Y or N there are a few things I would put in...
First I would make sure that you only grab the first letter by adding:
IF NOT '%isit%'=='' set isit=%isit:~0,1%
Next I would make sure that you only have Capital letters, so you make the swap if they are lowercase, because this will actually not work in some cases if the CaSe isn't correct:
if '%isit%'== 'y' set isit=Y
if '%isit%'== 'Y' goto :saidyes
if '%isit%'=='n' set isit=N
if '%isit%'=='N' goto :saidno
The final edit for this file could look like the following:
#echo off
:top
set theuserinput=
set /P theuserinput="Type your name: "
echo So your name is: %theuserinput%?
set /P isit="Y/N: "
IF NOT '%isit%'=='' set isit=%isit:~0,1%
IF '%isit%'=='y' set isit=Y
IF '%isit%'=='Y' goto saidyes
IF '%isit%'=='n' set isit=N
IF '%isit%'=='N' goto saidno
goto top
:saidyes
echo You typed: %isit%
echo Hooray!
goto end
:saidno
echo You typed: %isit%
echo Aww
goto end
:end
pause
exit
Quotes are commonly misused in DOS batch files. Unlike UNIX shell scripts, the DOS batch language is not thought-out. For example, when the isit variable contains quotes
set isit="Y"
then the above if-statements expand to
IF ""Y"" == "Y" GOTO saidyes
IF ""Y"" == "N" GOTO saidno
because cmd.exe does not remove the quotes in "%isit%"=="Y" before evaluating the expression.
Quotes in isit shall not occur with the original batch file posted here. But often you process path names in batch files, and Windows passes long file names in quotes to hide blanks. For example, as in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC". To work around this it is common practice to write if-statements so:
IF ["%isit%"] == ["Y"] GOTO saidyes
IF [%isit%] == ["Y"] GOTO saidyes
IF [%isit%] == [Y] GOTO saidyes
For set isit="Y" this becomes:
IF [""Y""] == ["Y"] GOTO saidyes
IF ["Y"] == ["Y"] GOTO saidyes
IF ["Y"] == [Y] GOTO saidyes
and for set isit=Y:
IF ["Y"] == ["Y"] GOTO saidyes
IF [Y] == ["Y"] GOTO saidyes
IF [Y] == [Y] GOTO saidyes
and for set isit=:
IF [""] == ["Y"] GOTO saidyes
IF [] == ["Y"] GOTO saidyes
IF [] == [Y] GOTO saidyes
which is far from being elegant, but at least works now for quoted and unquoted variables. It also works when isit is empty. Oftenly batch files stop because of empty variables:
set x=
REM ...
IF %x% == x GOTO x
because now the if-statement expands to:
IF == x GOTO x
and cmd.exe fails. These errors are usually hard to debug.
The trick is very old; I remember using it already in MSDOS. Note that the square brackets are not evaluated by cmd.exe; they're just some rarely used characters. But it's just a trick. Any advanced batch script needs a dequoting-function:
#echo off
setlocal EnableExtensions
setlocal EnableDelayedExpansion
REM ...
set /P isit="Y/N: "
call :dequote isit
REM ...
IF "!isit!" == "Y" GOTO saidyes
IF "!isit!" == "N" GOTO saidno
REM ...
goto done
:dequote
for /f "delims=" %%A in ('echo %%%1%%') do set %1=%%~A
goto :eof
:done
The deqote-subroutine removes any double quotes around the variable name passed to the function.
After dequoting the []-trick is not required anymore. Note also the use of ! instead of %. The exclamation marks force cmd.exe to reexpand isit.
I changed the code somewhat to help fix your problem. Instead of goto :(function) you just say goto (function)
#echo off
set /P theuserinput=Type your name:
echo So your name is: %theuserinput%?
set /p isit=Y/N:
echo You typed: %isit%
if %isit% == "y" goto saidyes
if %isit% == "n" goto saidno
:saidyes
echo Hooray!
:saidno
echo Aww
pause