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
Related
This is not the full code, since its about 1000 lines long, but here's the problem, when i come to this section of the game
choice /c abc1 /n
when i press "a" it's supposed to "medicalbag" and instead it acts like if i were to press "1" and goes back to start
when i press "b,c,1" they all go to "medicalbag".
i can't find a solution to this, i read about the command and apparently it supports these letters and numbers, when i change them out with just numbers they work just fine, but im really not sure what im doing wrong here.
:bag
cls
echo *****************************
echo a) Medical supplies
echo b) Consumables
echo c) Weaponry
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c abc1 /n
if %errorlevel% == a goto medicalbag
if %errorlevel% == b goto consumablebag
if %errorlevel% == c goto weaponrybag
if %errorlevel% == 1 goto start
:medicalbag
cls
echo *****************************
echo Bandages: %bandagecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:consumablebag
cls
echo *****************************
echo Canned food: %cannedfoodcount%
echo Purified water: %purifiedwatercount%
echo Dirty water: %dirtywatercount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:weaponrybag
cls
echo *****************************
echo a) combatknife: %combatknifecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c a1 /n
if %errorlevel% == a goto combatknifecheck
if %errorlevel% == 1 goto bag
the errorlevel of the choice keys defined using the /c switch is returned according to the keys position in the list.
with /c abc1:
keypress: errorlevels reuturned:
a 1
b 2
c 3
1 4
To get the literal keypress, you need to use a for /f loop to capture the keypress:
For /f "delims=" %%G in ('choice /n /c abc1')Do Echo(You pressed: %%G
Note - the /n switch MUST be used when using a for loop to capture the pressed key.
Here is an example of a dynamic menu system that will return the user selected value and the description of that value.
#echo off
setlocal EnableDelayedExpansion
CALL :MENU "Medical supplies" "Consumables" "Weaponry" "Back"
CALL :CHOSE
pause
goto :eof
REM ONLY FUNCTIONS BELOW HERE
:MENU
set /a "OptionNo=0"
set "choices="
FOR %%I IN (%*) do (
set /a OptionNo+=1
call :ResolveChar !OptionNo!
set "Option!retval!=%%~I"
set "choices=!choices!!retval!"
echo [!retval!] %%~I
)
GOTO :EOF
:ResolveChar
SETLOCAL
set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_startchar=%1"
CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof
:CHOSE
choice /c !choices! /cs /m "Select Your Option: " /n
call :ResolveChar %errorlevel%
set "Option=!Option%retval%!"
echo You Selected: %retval% - %Option%
GOTO :EOF
I enjoy coding, scripting and other stuff, and today my friend showed me how to make a batch file. I made one, so I run it. it works well, until I get to the one part, where it just closes.
It's supposed to go through the slides, it does that well. But when it gets to the part that it does goto t1win it just closes the .bat file.
Here is my code:
#echo off
cls
:loop
:menu
echo hello
color 0a
color 0
cls
echo This is for scripture stuff.
set /p=
goto scrn1
:scrn2
cls
echo Whats your partner's name?
set /p p2=
goto scrn3
:scrn5
cls
echo Time for the questions!
set /p=
goto scrn6
:t1win
cls
echo Yay! %p1% and %p2%, you did it!
set /p=
exit
:scrn7
cls
echo 2.
echo.
echo %p2%, How did the barges have light? (a= The barges had windows b= Jesus touched stones and the stones gave off light c= The people used lanterns)
echo.
set /p t1q2=
if %q1t2% == a goto scrn7
if %q1t2% == b goto t1win
if %q1t2% == c goto scrn7
if not defined start (
cls
goto loop
)
:scrn4
cls
echo Open the book 'Book of Mormon Stories', to page #145. Read until page #148, but still read #148. Press enter when you are done reading.
set /p=
goto scrn5
:scrn6
cls
echo 1.
echo.
echo Hey %p1%! What did the brother of Jared build? (a=barges b=boats c=planes)
echo.
set /p t1q1=
if %t1q1% == a goto scrn7
if %t1q1% == b goto scrn6
if %t1q1% == c goto scrn6
:scrn1
cls
echo Whats your name?
set /p p1=
goto scrn2
:scrn3
cls
echo Hello, %p1% and %p2%!
set /p=
goto scrn4
Please help.
BTW I don't really care if it jumps around like the sample.
this is the first question I asked, so don't judge meh!
here's the code! :D
#echo off
cls
:menu
echo hello
color 0a
color 0
cls
echo This is for scripture stuff.
set /p=
goto scrn1
:scrn2
cls
echo Whats your partner's name?
set /p p2=
goto scrn3
:scrn5
cls
echo Time for the questions!
set /p=
goto scrn6
:t1win
cls
echo Yay! %p1% and %p2%, you did it!
set /p=
exit
:scrn7
cls
echo 2.
echo.
echo %p2%, How did the barges have light? (a= The barges had windows b= Jesus touched stones and the stones gave off light c= The people used lanterns)
echo.
set /p t1q2=
if %t1q2% == a goto scrn7
if %t1q2% == b goto t1win
if %t1q2% == c goto scrn7
if not defined start (
cls
goto loop
)
:scrn4
cls
echo Open the book 'Book of Mormon Stories', to page #145. Read until page #148, but still read #148. Press enter when you are done reading.
set /p=
goto scrn5
:scrn6
cls
echo 1.
echo.
echo Hey %p1%! What did the brother of Jared build? (a=barges b=boats c=planes)
echo.
set /p t1q1=
if %t1q1% == a goto scrn7
if %t1q1% == b goto scrn6
if %t1q1% == c goto scrn6
:scrn1
cls
echo Whats your name?
set /p p1=
goto scrn2
:scrn3
cls
echo Hello, %p1% and %p2%!
set /p=
goto scrn4
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.
Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)
I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem
:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL
The program starts to loop again even if i give a string.
I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.
Any help is appreciated. Thanks
You can try something like that :
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" (
Cls & Color 0C
echo You must enter something
Timeout /T 2 /NoBreak>nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
Cls & Color 0C
echo You must enter something
choice /d y /t 2 > nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause