String with spaces in set /p (batch file)


  1. Posts : 32
    Windows 10 Pro x64
       #1

    String with spaces in set /p (batch file)


    Hi! I would like to let a user type in things with spaces in set /p. This is the code I currently have that does not work:

    Code:
    :newdoc2
    cls
    echo What do you want to say?
    set saynew=%randomnum%
    set /p saynew=
    if %saynew% == %randomnum% goto newdoc2
    echo "%saynew%" >%namenew%
    goto start
    This works if saynew has no spaces, but not if it does. If I type, for example, "hello people", it will briefly say "hello was not expected at this time" and then crash. What did I do wrong? I tried putting "" around %saynew%:

    Code:
    echo "%saynew%" >%namenew%
    but it did not work. Any ideas?
      My Computer


  2. Posts : 32
    Windows 10 Pro x64
    Thread Starter
       #2

    If you need the whole thing, here it is:

    Code:
    @echo off
    title Text Reader
    color 17
    goto start
    set randomnum=%random%
    
    :start
    cls
    title Text Reader
    echo Type the location of the file and the filename. Type "end" to close, or type "new" to make a new document.
    set /p file=
    if %file% == new goto newdoc
    if %file% == end goto end
    if not exist %file% goto start
    goto main
    
    :main
    cls
    title %file%
    type %file%
    echo.
    echo (End of Document)
    echo Press any key to go back to start...
    pause >nul
    goto start
    
    :newdoc
    cls
    title New Document
    echo Type a name, and location if necessary.
    set namenew=0
    set /p namenew=
    if %namenew% == 0 goto newdoc
    :newdoc2
    cls
    echo What do you want to say?
    set saynew=%randomnum%
    set /p saynew=
    if %saynew% == %randomnum% goto newdoc2
    echo "%saynew%" >%namenew%
    goto start
    
    :end
    exit
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:13.
Find Us




Windows 10 Forums