I have a question. I have
set /p displaytext=Text to be displayed:
echo "%displaytext%"
pause
and if I input something with spaces it displays what i wrote in quotes. How to get rid of them?. Help! Thanks in advanced. Also if i write something in caps with/without spaces it closes. Basically I want it to display exactly what I type no madder what and without quotes.
Try like below. Just remove the " from "%displaytext%"
#echo off
set /p displaytext=Text to be displayed:
echo %displaytext%
pause
For your reference, see output below for test run
D:\>test.bat
Text to be displayed:hi there user3671588
hi there user3671588
Press any key to continue . . .
Related
Full code below, sorry if the question looks jumbled.
I'm trying to make a batch file that adds a help doc to a specified directory, but the doc would need to have new lines. The problem is, is that I'm saving the text that gets put in the txt doc using a var.
I would like the help doc to look like this:
set help=helper \n new line \n\n more space
Which would look like this: (sorry, had to use code because a new line couldn't be made normally)
helper
new line
more space
I have seen things like the example down below, but when I do use this, the text in the doc just says echo is ON or echo is OFF, depending on whether or not echo is on (code found here):
setlocal EnableDelayedExpansion
set LF=^
rem TWO empty lines are required
echo This text!LF!uses two lines
And on top of that, this code looks like its new line var makes a spacer, rather than JUST a new line, like the, "helper," and, "new line," in the example I put above. I would want, "\n," to put the following text in the next line, and, "\n\n," to put the text two lines after the previous text, like, "more space," in the example above. In short, I'd like, "\n," to be a new line, and, "\n\n," to be a line break.
Thank you for taking the time to read this, I appreciate all answers and/or tips. If you're confused on anything that I mean, please feel free to ask me in the comments. I am pretty new to this, so my usage of some terminology is probably pretty poor. As well as that, sorry for the run on sentences and/or bad grammar/punctuation. I'm just a bit tired and I'm forgetting my English 1 classes, I suppose.
My full code is:
#echo off
:start
cls
echo -create
echo -download
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%
:create
cls
REM saves the text types to the helpDir var
set /p helpDir=Where do you want to save the help file?:
REM makes the text used in the help.txt file, \n would be the new line,
set help=helper \n new line \n\n more space
REM I want to make it look like this:
REM helper
REM new line
REM
REM more space
REM Makes the "help" text to go to a .txt file, then saves the .txt file to the dir specified.
echo %help% > "%helpDir%\help.txt"
pause
goto start
REM not finished yet, please ignore
:download
title Custom Text File
cls
set /p help=Where do you want to save the help doc at?;
set /p txt=Made a command
echo %txt% > "dir
Pause
Your first sample works flawlessly, but today another style is prefered.
setlocal EnableDelayedExpansion
(set LF=^
%=DO NOT REMOVE THIS=%
)
echo This text!LF!uses two lines
If you see spaces in your output (instead of line feeds), check and remove trailing spaces in the LF definition.
From your comment, your problem is the percent expansion.
From your linked post:
The newline best works with delayed expansion, you can also use it
with the percent expansion, but then it's a bit more complex.
Simple rule: Always expand variables, containing line feed characters, only with delayed expansion.
set help=Line1 \n Line2
echo !help!>output.txt
I am trying to create a Batch File where you can get input from the user and i want it to look like this:
###################
# Batch File #
# #
# Enter Value #
# > #
###################
So i am using this code:
echo Batch Title
echo.
echo Enter Value
set /p input= >
But with this code when i open the CMD file it immediately close. And in my editor the ">" is highlighted. I also tried to escape it like this /> but it didn't work what am i supposed to do. Sorry if the question is not good but i am new here and also new in Batch Programming. Thanks for your time :)
Escape it using ^. I changed your example which now looks like this:
echo Batch Title
echo.
echo Enter Value
set /p input= ^>
This is a very nice collection of all the different escape character rules for batch files.
As well this overview remarks that it is also possible to doublequote the greater-than sign. Here is the quote:
May not always be required in doublequoted strings, but it won't hurt - Source
Just put it in Quotation marks ("") and you'll be fine:
echo Batch Title
echo.
echo Enter Value
set /p input= ">"
Use the escape character ^
echo ^>
I would like to find a way to use batch to change the keys a user types into something else. I'm not sure if there is a way to do this in batch, so if there is a better way to do this please let me know. Basically, I would like to change what is typed into something else, meaning when the program is run, (for example) any time the user types the letter "a" it could replace it with the letter "b". I'm not sure if I can do this, and to clarify I do not want the answer given to me, just some guidance on how to do it. Thanks
Perhaps you mean something like that in batch : - Variable Edit/Replace
#echo off
Title Variable Edit/Replace
:Main
cls
echo Type a word ...
set /p "Input="
set "StrToFind=a"
set "NewStr=b"
setlocal enabledelayedexpansion
set AfterReplaceInput=!Input:%StrToFind%=%NewStr%!
echo Before replace : !Input!
echo After replace : !AfterReplaceInput!
echo(
echo Hit any key to try again with another word...
pause>nul
Goto Main
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
I would like to echo a command onto the screen. I'm making something of a tutorial for someone and I want to display the command that is going to be running when they press enter.
For example, I have this so far:
echo off
echo Tutorial
pause
echo .
echo .
echo This will show how to read the first line of a text file and place into another text file
echo .
echo .
pause
set /p texte=< test.txt
echo FOR %P IN (%texte%) DO(echo blah >> test2.txt)
pause
However, it won't work when it reaches the last echo, because I'm echoing a command rather than just a text. Is there a way to echo a command?
EDIT: When I try to run something like this, it'll say there is an error once it reaches that last echo command, it says I'm trying to run something following the echo command. But in reality, what I'm trying to do is show the command I'm going to be using on the next line or something along those lines.
This is just an example of what I'm doing, I'm sorry if the actual echo statement just doesn't make sense in general. I'm just wondering if there was a way to echo a command.
> is a special symbol, so you need to escape it. The escape character in bash is the carat: ^ therefore ^>^> should fix that problem, however batch still interprets % differently. for that you need %%. This:
Echo FOR %%P IN (%%texte%%) DO(echo blah ^>^> test2.txt)
will output the command exactly as you want. Also if you add # before your echo off it won't echo echo off at the beginning of your script.
In most of the shells, there is a debug mode to achieve what you want. For example, in Korn shell, you can type set -x to achieve this.
Because no one has answered yet, I'm going to attempt this on my phone.
The reason its giving an error is because you need to escape some stuff.
Echo for ^%p in ^(^%texte^%^) do ^(echo Blah ^>^> test2.txt ^)
That took about 20 minutes so I better get at least an upvote.
Bit of a tricky one. How can I correctly escape the following in a batch file?
echo /? display this help text
This particular combination of characters is treated as an "ECHO /?" command:
C:\Batch>ECHO /? display this help text
Displays messages, or turns command-echoing on or off.
ECHO [ON | OFF]
ECHO [message]
Type ECHO without parameters to display the current echo setting.
It does not respond to caret (^) escaping, ie. I've tried ^/? /^? and ^/^?.
NB: As a workaround, I found that inserting other characters in between is enough to bypass the ECHO command line processor, eg:
echo ... /? display this help text
Still, this is not ideal and I wondered if there was a way to acheive the desired output, namely with /? at the start of the echoed message.
For escaping echo arguments exists many variants, like echo., echo:, echo=
But only echo( seems to be secure against any appended text.
These one fails, if files exists like echo, echo[, echo] or echo+
echo.
echo[
echo]
echo+
These one fails, if a file in the current directory exists named my.bat
echo\..\my.bat
echo:\..\my.bat
echo.\..\my.bat
These one fails independet of a file
echo/?
echo,/?
echo;/?
Only the echo( seems to be always safe against any content
For escaping echo arguments, you can use the alternative syntax echo.:
echo./?