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 ^>
Related
I'm working on a simple batch file to save a text file for each JPG. The content of the txt file are input by user using set /p. For some reason when i used set /p with the for loop, the txt file did not show value input by user (it only shows: "ECHO is on").Any help would be highly appreciated! Thanks.
for %%Q in (*v.JPG) do (
set /p meas= "enter value "
echo %meas%>"%%~nQ.txt"
)
pause
Magoo is right, Thank you! I had to use Enable delayed expansion, and then referto variables as !variable! and not %variable%. Unfortunately, there isn't documentation on Microsoft website on this issue, at least not on the 'set' command webpage.
for %%Q in (v*.jpg) do ( set /p meas="enter value " && echo !meas! > %%~nQ.txt)
I tested this in just a cmd prompt but should work in a batch as well.
This will create a new file named <filename>.jpg.txt and will contain the value the user entered.
The key is to add && between set /p and echo.
I wanted to make a basic text editor in batch and I'm using set /p to get user input and write that to a file.
When I type in something like "hello" or "hello " it works fine, but as soon as I write something with a space and another character after than, like "hello world", the window closes.
My code was:
set /p append=Write what you want to append:
Then I tried:
set /p "append=Write what you want to append: "
which i found online, but it didn't work.
pause isn't of any help because the program crashes as soon as I press enter.
Help!
Here is the full code:
:append
set /p "append=Write what you want to append: "
if %append%==return (
cls
goto start
)
echo %append% >> %filename%
goto append
:override
cls
echo Name: %filename%
set /p override="Write new content: "
echo %override% > %filename%
pause
cls
goto start
:writefile
echo.
set /p filename=Write file name.extension:
choice /c AO /m "Append to file or override? "
if %ERRORLEVEL%==1 (
cls
echo Name: %filename%
goto append
) else (
goto override
)
Whenever the user of a batch file is prompted for a string using set /P the user has the freedom to
enter nothing at all by pressing simply just RETURN or ENTER which results in the environment variable is still not defined after prompt if it was not defined before, or the variable keeps its current value if it was already defined before, or
enter really anything including a string which could result in an exit of batch execution because of a syntax error somewhere in batch code later or an unwanted behavior.
Let us look on the line
if %append%==return (
If environment variable is not defined before with a default value and the batch user enters nothing, this line expands during preprocessing step to:
if ==return (
This command line is of course invalid and results in an exit of batch processing because of a syntax error.
Now let us assume the user enters the string:
I want to append this string.
Then the IF command line expands to:
if I want to append this string.==return (
And of course also this command line is invalid.
The other answerers suggested to enclose the two strings to compare in double quotes, i.e. use the command line:
if "%append%"=="return" (
But does that really solve all possible issues?
Is the code now really safe against any user input.
Let us look what happens if the user enters the string:
" is a double quote. It should be used around paths like "%ProgramFiles(x86)%".
The IF command line expands now to:
if "" is a double quote. It should be used around paths like "%ProgramFiles(x86)%""=="return" (
And this command line is again invalid and results in an exit of the batch file because of a syntax error.
So how to make batch code safe against really any user input?
The solution is using delayed environment variable expansion wherever the user entered string is referenced.
Example:
#echo off
setlocal EnableDelayedExpansion
echo.
echo Enter EXIT to exit this batch script.
:PromptUser
echo.
set "UserInput=Nothing^!"
set /P "UserInput=Please enter a string: "
if /I "!UserInput!" == "exit" goto EndBatch
echo You entered: !UserInput!
goto PromptUser
:EndBatch
echo.
echo Thanks for running this example batch code.
echo.
echo The batch file ends in 3 seconds ...
endlocal
%SystemRoot%\System32\ping.exe localhost -n 4 >nul
By using delayed expansion the user entered string can't result in a invalid or unexpected command line in later code. This is also important for the command line
echo !append!>>%filename%
It is important to have no space character left of >> or this space character is also written into the file as trailing space.
But delayed expansion is also important here in case of user enters for example just 2 which would result with echo %append%>>%filename% in
echo 2>>%filename%
which does not append the character 2 to the file, but would append STDERR to the file which results in an empty line written to the file.
Delayed expansion is also needed for this string entered by the user:
(Var1 > 0x2F) && (Var1 < 0x3A)
which should be written with ECHO into the file as entered and not what Windows command interpreter would produce after expanding the string when using echo %append%>>%filename%.
Change your set to put quotes in the right place around the prompt string, and your if line to put quotes around the string being tested.
set /p append="Write what you want: "
set append
echo xx%append%yy
if "%append%"=="return" (
echo Yes!
)
Your problem is with
if %append%==return (
which will give a syntax error if append contains spaces.
use
if "%append%"=="return" (
"quoting a string" makes cmd interpret it as a single element so the required syntax of if,
if string==string (
is no longer violated.
echo.
set /p textfileName=What would you like the file to be named?
echo.
echo On the line below, write what text you would like the file to contain:
echo.
set /p textfileContents=
echo %textfileWrite% >> %textfileWriteName%.txt
echo.
echo Your file has been created and is in the same directory as this batch file.
echo It is named %textfilename%.txt
echo.
goto inputCommand
Whenever I try to use this code, it comes up with "Echo is off" (from #echo off previously in the file) but not what the user inputted. Then, if I run it again in the same instance, it will create the previous file but not the one I just told it to create.
I've tried having > and it didn't work, >> didn't work either.
I think you have a few problems going on here. First, you mentioned #echo, but looking at your code, you're just using echo.
Also, I think there's some confusion with your variables. You capture the user's filename into textfilename, but then write to textfileWriteName. You capture the user's file contents in textfileContents, but then write textfileWrite to the file.
Finally, you specify a goto label that doesn't exist. Maybe this is part of a larger batch file that you just partially copied?
Anyhoo, I think this is along the lines of what you intended:
#echo off
set /p textfileName=What would you like the file to be named?
#echo On the line below, write what text you would like the file to contain:
set /p textfileContents=
#echo %textfileContents% > %textfileName%.txt
#echo Your file has been created and is in the same directory as this batch file.
#echo It is named %textfilename%.txt
you have serious space issues in your variables " text filename and text contents"
avoid spaces in your variables or if you must use more than one word in your variable use symbols to separate the strings .. the code below should work fine
#echo off
: main_menu
echo.
echo.
set /p notf=name of text file :
set / p cof=contents of file :
echo %cof%>>"%notf%.txt"
echo %notf% text file was successfuly created...
echo.
echo.
echo press any key to return to main menu
pause>null
cls
goto main_menu
2 Questions:
1) Changing scripts based on input
Basically, say I have a file, like search.html that changes based on what you type in.
Aside from doing
set/p string=What would you like to search for?
echo ^<!DOCTYPE html^> >>file.html
echo ^<html^> >>file.html
echo ^<title^>^</title> >>file.html
echo ^<script language="JavaScript""^> >>file.html
echo string = '%site%'
...
Is there another way to do this?
2) Getting things returned from a file?
I have no example for this. I was simply wondering if you could start a file, use wait, and once it has closed get what was in it?
try this:
#echo OFF &setlocal
(
echo ^<!DOCTYPE html^>
echo ^<html^>
echo ^<title^>^</title^>
echo ^<script language="JavaScript""^>
echo string = '%site%'
)>file.html
2) Getting things returned from a file
#echo OFF &setlocal
FOR /f "delims=" %%a IN (file.html) DO (
ECHO(%%a
)
There are many ways to replace text in a file with something else. You can get input and then replace a MARKER (text like that) with the input text. VBS, Powershell, SED, AWK, and batch can do it.
Your second question is a bit short on detail - but FINDSTR etc can read lines from a file.
I turned off echo in bat file.
#echo off
then I do something like this
...
echo %INSTALL_PATH%
if exist %INSTALL_PATH%(
echo 222
...
)
and I get:
The system cannot find the path specified.
message between those two echos.
What can be the reason of this message and why message ignores echo off?
As Mike Nakis said, echo off only prevents the printing of commands, not results. To hide the result of a command add >nul to the end of the line, and to hide errors add 2>nul. For example:
Del /Q *.tmp >nul 2>nul
Like Krister Andersson said, the reason you get an error is your variable is expanding with spaces:
set INSTALL_PATH=C:\My App\Installer
if exist %INSTALL_PATH% (
Becomes:
if exist C:\My App\Installer (
Which means:
If "C:\My" exists, run "App\Installer" with "(" as the command line argument.
You see the error because you have no folder named "App". Put quotes around the path to prevent this splitting.
Save this as *.bat file and see differences
:: print echo command and its output
echo 1
:: does not print echo command just its output
#echo 2
:: print dir command but not its output
dir > null
:: does not print dir command nor its output
#dir c:\ > null
:: does not print echo (and all other commands) but print its output
#echo off
echo 3
#echo on
REM this comment will appear in console if 'echo off' was not set
#set /p pressedKey=Press any key to exit
"echo off" is not ignored. "echo off" means that you do not want the commands echoed, it does not say anything about the errors produced by the commands.
The lines you showed us look okay, so the problem is probably not there. So, please show us more lines. Also, please show us the exact value of INSTALL_PATH.
#echo off
// quote the path or else it won't work if there are spaces in the path
SET INSTALL_PATH="c:\\etc etc\\test";
if exist %INSTALL_PATH% (
//
echo 222;
)
For me this issue was caused by the file encoding format being wrong.
I used another editor and it was saved as UTF-8-BOM so the very first line I had was #echo off but there was a hidden character in the front of it.
So I changed the encoding to plain old ANSI text, and then the issue went away.