简体   繁体   中英

Batch File Issue - Save Input To Text With Spaces

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM