Batch: Slowing processing speed - performance

I am creating a little project to impress my friends, and was curious on how I could slow down the text outflow on the program, using just one line.
I am having a problem (And trust me, I did my research), and was curious as to how to slow down the batch processing speed in general, without doing echo hi TIMEOUT 5>nul echo hi again, etc. But instead, only a single line at the beginning, which slows the output of the text for the whole batch script. Thanks!

The only real way to do this in the batch language would be to use the call command to call a function and to work. This can be placed at the bottom of your batch document.
Furthermore, Since your not real clear on your goal here & from reading the comments, I'm going to assume that from echo hi & TIMEOUT 5>nul echo hi again you want to display each line after 5 seconds BUT from just one line or command. This can be done easily with call & an FOR loop. In this case we can use %* to gather all "Words Line One", "Words Line Two" that you wish.
Combining this with simple syntax-replacement to remove the quotes and we good.
DelayedLine.bat
#echo off
#setlocal EnableDelayedExpansion
Rem | To use the command, Call :SlowOutput "Desired Line" "Desired Line" "Desired Line"
Call :SlowOutput "Line Number One" "Line Number Two" "Line Number Three"
pause>nul
Goto :EOF
:SlowOutput
for %%A in (%*) do (
Set "String=%%A"
Set String=!String:"=%!
echo !String!
Rem | Change 5 To How Many (Seconds) You Wish
Timeout 5 /NoBreak>Nul
)
goto :EOF
If your goal was to have a typewrighter effect, we can use a script by hackoo on this thread here and modify it to work with the call in an organized matter.
This script will display letter by letter and for each call "Word" "Word" new quote a new line. All working by one line via call
LetterDelay.bat
#echo off
Rem | To use the command, Call :SlowText "Desired Text"
Rem | To combine two "echo's" on one line we can use the "&"
Call :SlowText "Hello!" & Call :SlowText "How is your day today?"
pause>nul
Goto :EOF
:SlowText
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 100
echo For x = 1 to intTextLen
echo strTempText = Mid(strText,x,1^)
echo WScript.StdOut.Write strTempText
echo WScript.Sleep intPause
echo Next
)>%tmp%\%~n0.vbs
#cScript.EXE /noLogo "%tmp%\%~n0.vbs" "%~1"
Rem | echo. is optional. It's used as a constant for a newline \n
echo.
goto :EOF
For help on any of the commands do the following:
call /?
set /?
for /?
if /?
find /?
So on.

Related

Creating a shop. Won't subtract money correctly

I expect to be able to buy items in the shop and have it do the correct subtraction. In the code below, you start out with 10 gold pieces, (gp), but whenever option 2 or 4, to spend 5gp or 1gp, is entered, it takes away all 10gp. I know that it's because it doesn't go past the first if %input%== 1 but I don't know how to fix it, I have tried almost everything, including if/else statements, but I may not have been doing them properly.
:shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo.
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo.
set /p input=Enter:
if %input%== 5 goto main
if %input%== 1
if %gp% LSS 10 goto nofunds
set /a gp= %gp% - 10
goto shopcont
if %input%== 2
if %gp% LSS 5 goto nofunds
set /a gp= %gp% - 5
goto shopcont
if %input%== 3
if %gp% LSS 20 goto nofunds
set /a gp= %gp% - 20
goto shopcont
if %input%== 4
if %gp% LSS 1 goto nofunds
set /a gp= %gp% - 1
goto shopcont
goto shop
:nofunds
cls
echo You don't have enough gold to purchase that item.
pause >nul
goto shop
:shopcont
cls
echo Would you like to purchase anything else?
goto purchase
I am still new at this so examples and explanations would be wonderful!
Please do not tell me to use choice.exe instead of Set /P, unless it will fix the actual issue.
In the below example, I have used Set /P under :purchase to satisfy your ill advised stipulation to not use choice.exe, (which I used under :shopcont instead).
:shop
ClS
Echo You see a middle aged man behind the shop counter, as well as a
Echo younger man sweeping the floor.
Echo(
Echo "Welcome young travellers, is there anything I can help you with?"
:purchase
Set "input="
Set "invalid=true"
Echo(
Echo ------------------------------------------------------------------
Echo(%name%
Echo Gold: %gp%
Echo ------------------------------------------------------------------
Echo(
Echo 1. Battleaxe 10gp [Stats: 1d8(S) Versatile(1d10)]
Echo 2. Mace 5gp [Stats: 1d6(B)]
Echo 3. L.Crossbow 20gp [Stats: 1d8(P) Range 80/320]
Echo 4. 5 Bolts 1gp [Equip with Crossbow]
Echo 5. Go Back
Echo(
Set /P "input=Enter: "
For /L %%A In (1,1,5) Do If "%%~A" == "%input:"=%" Set "invalid="
If Defined invalid ClS & GoTo purchase
If %input% Equ 5 GoTo main
If %input% Equ 4 If %gp% GEq 1 Set /A gp -=1 & GoTo shopcont
If %input% Equ 3 If %gp% GEq 20 Set /A gp -=20 & GoTo shopcont
If %input% Equ 2 If %gp% GEq 5 Set /A gp -=5 & GoTo shopcont
If %input% Equ 1 If %gp% GEq 10 Set /A gp -=10 & GoTo shopcont
Echo You do not have enough gold to purchase that item.
:shopcont
"%__AppDir__%choice.exe" /M "Would you like to purchase anything else"
If "%ErrorLevel%"=="1" ClS & GoTo purchase
Please note that I have tried to replicate that which you posted in your question, this assumes that %gp% and %name% are already defined prior to this code section and that the label :main exists elsewhere in your unposted code.
You asked for examples and explanations, but those are readily available under each command's usage information and via web searches, so I will not be pointlessly including such things.
The usage of command set /P is not recommended for a simple choice menu. A simple typing mistake by user of batch file can easily result in a syntax error on further processing of the batch file detected by Windows command processor resulting in an unexpected exit of batch file execution. A user playing this batch file game by double clicking on it will not be happy on typing for example by mistake " instead of 2 and suddenly the console window is closed because cmd.exe exited batch file processing because of a serious syntax error caused by " and not good coded batch file.
See also:
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Safe number comparison in Windows batch file
However, the main problem is the used syntax on all IF commands. The syntax of command IF can be seen by opening a command prompt, running if /? and reading the output help. if %input%== 1 without a command or a command block starting with ( and ending with matching ) to execute on condition is true on same line results in a syntax error on batch file execution. This can be seen on debugging the batch file.
The indentations have no meaning for cmd.exe regarding to process flow. Windows command processor is not Python. Windows command processor executes one command line respectively command block after the other independent on how many leading spaces or tabs are used to indent the command lines.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
An arithmetic expression is the string after set /A evaluated by cmd.exe on execution of the batch file. The help output on running set /? explains that within an arithmetic expression it is possible to reference the value of an environment variable by writing just its name without % or ! around variable name. That has two advantages:
If the environment variable does not exist at all, Windows command processor uses value 0 for not existing environment variable. Using %NotExistingVariable% in an arithmetic expression results in a syntax error because of this string is replaced by nothing which usually results in a missing operand error on evaluation of the arithmetic expression.
Environment variables can be modified with arithmetic expressions multiple times in a command block without usage of delayed expansion.
For that reason set /a gp= %gp% - 10 is not a recommended syntax to decrement the environment variable gp by 10. Better is using set /A gp=gp - 10 and best set /A gp-=10.
The DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ explains that echo. can fail to print an empty line into console window and that echo/ or echo( is better for this task.
A minimal, complete, and verifiable example for this task is following batch file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "gp=50"
set "name=B1u3Soul"
:Shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:Purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo/
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo/
%SystemRoot%\System32\choice.exe /C 12345 /N /M "Enter: "
if errorlevel 5 goto Main
if errorlevel 4 set "GoldAmount=1" & goto GoldInPurse
if errorlevel 3 set "GoldAmount=20" & goto GoldInPurse
if errorlevel 2 set "GoldAmount=5" & goto GoldInPurse
set "GoldAmount=10"
:GoldInPurse
if %gp% LSS %GoldAmount% goto NoFunds
set /A gp-=GoldAmount
echo/
%SystemRoot%\System32\choice.exe /C YN /N /M "Would you like to purchase anything else [Y/N]? "
cls
if errorlevel 2 goto Main
goto Purchase
:NoFunds
echo/
echo You don't have enough gold to purchase that item.
pause >nul
goto Shop
:Main
endlocal
See also single line with multiple commands using Windows batch file for an explanation of operator & as used in this batch file.
It would be of course possible to use just choice instead of %SystemRoot%\System32\choice.exe. But the usage of full qualified file name (drive + path + file name + file extension) makes the batch file independent on environment defined outside the batch file. For this batch file it does not matter how PATH and PATHEXT is defined on starting the batch file. It depends only on environment variable SystemRoot defined by Windows and which is not modified by applications or users in general.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Microsoft article about Using command redirection operators

Prevent batch script from terminating upon invalid character input

I always have this issue when writing batch scripts. Whenever I have the script prompt the user to set a variable, if a semicolon (that's just one example) is inputted, the script will close. Is there a way to prevent it from doing so?
Example:
#echo off
:1
cls
echo Please enter your ID:
set /P id=
if /i %id%==119 goto Tom
if /i %id%==204 goto Brittany
if /i %id%==12 goto Shannon
if /i %id%==64 goto Jack
goto 1
:Tom
cls
echo Tom, you have to mow the lawn.
pause>nul
exit
:Brittany
cls
echo Brittany, you have to fold the laundry.
pause>nul
exit
:Shannon
cls
echo Shannon, you have to feed the dog.
pause>nul
exit
:Jack
cls
echo Jack, you have to replace the radio's capacitors.
pause>nul
exit
What I would see running that script:
C:\>myscript.bat
Please enter your ID:
asjfash;dfjlas;ldf
asjfash;dfjlas;ldf==i was unexpected at this time.
and the script closes.
Thanks!
Read Syntax : Escape Characters, Delimiters and Quotes:
Delimiters
Delimiters separate one parameter from the next - they split the
command line up into words.
Parameters are most often separated by spaces, but any of the
following are also valid delimiters:
Comma (,)
Semicolon (;)
Equals (=)
Space ()
Tab (   )
If a user inputs some string containing any of above listed separators (like set "id=1 19" then %id% contains a space), then
if /i %id%==119 goto Tom
results to
if /i 1 19==119 goto Tom
↑ this space causes error 19==119 was unexpected at this time
Definitely, you need to escape delimiters and all other cmd-poisonous characters as follows:
#echo off
:1
cls
echo Please enter your ID:
set /P id=
if /i "%id%"=="119" goto Tom
if /i "%id%"=="204" goto Brittany
if /i "%id%"=="12" goto Shannon
if /i "%id%"=="64" goto Jack
goto 1
rem script continues here
FYI, Redirection article lists other cmd-poisonous characters which need to be escaped as their unescaped occurrence in a batch script has following meaning:
&  - Single Ampersand: used as a command separator
&& - Double Ampersand: conditional command separator (like if errorlevel 0)
|| - Double Pipe (Vertical Line): conditional command separator (like if errorlevel 1)
|  - Single Pipe: redirects the std.output of one command into the std.input of another
>  - Single Greater Than: redirects output to either a file or file like device
>> - Double Greater than: output will be added to the very end of the file
<  - Less Than: redirect the contents of a file to the std.input of a command

Allowing multiple choice selections for users using batch file [duplicate]

This question already has answers here:
Multiple choices menu on batch file?
(13 answers)
Closed 8 years ago.
I want to create a windows batch scripting whereby it allows the user to enter multiple choices at one go, and then after that the program will runs.
With reference to this website (Multiple choices menu on batch file?), I get to know it somehow works to allow multiple selection. However, this is in bash scripting. For example...
#echo off
setlocal enabledelayedexpansion
echo Which would you like to use?
echo 1. Hello.txt
echo 2. Byebye.txt
echo 3. ThisIsText.txt
echo 4. MyBatchScript.txt
echo 5. All
set /p op=Type the numbers of the names you want to use (separated by commas with no spaces. E.g: 1,3,2):
Until here, it works by prompting users to select one or more choices.
for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
However, until here, I realised that the choices would be stored into a variable "op" and this would then be in i. And basically, j, k, l and m are not used. I'm not sure if I interpreted it wrongly. Hopefully I did not interpret the coding wrongly.
So for what I want is that...
When the user selects only 1 options,
It will insert the "Hello.txt" into a command (e.g.)
echo This is complicated > Hello.txt
But if the user selects more than 1 options (for example, user typed 1,2),
then it will insert
echo This is complicated > Hello.txt
echo This is complicated > Byebye.txt
And if the user selects option '5', it cannot be entered along with other numbers (since it is ALL). Then it will echo the This is complicated > Byebye.txt , Hello.txt , etc
Is there anyway to do it using batch scripting?
Edit: Can anyone explain this to me? I tried finding different websites but I still don't get it. Sorry, I am new to writing batch scripts. So the understanding of it is still not deep. Disclaimer: This is the coding I got from the website I mentioned above.
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next
:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%
I do not get how this helps to run multiple command. If I input 1,2,3 into the field, how does it gets me to part where I can run it all together?
You may make good use of the fact that the standard separators for items in flat FOR command (no /F option) are spaces, commas, semicolons and equal-signs:
#echo off
setlocal enabledelayedexpansion
echo Which would you like to use?
echo 1. Hello.txt
echo 2. Byebye.txt
echo 3. ThisIsText.txt
echo 4. MyBatchScript.txt
echo 5. All
:getOptions
set /p "op=Type the numbers of the names you want to use (separated by commas OR spaces): "
if "%op%" equ "" goto getOptions
if %op% equ 5 set op=1,2,3,4
for %%a in (%op%) do (
echo Process option %%a
call :option-%%a
)
goto :EOF
:option-1
echo 1. Hello.txt
exit /B
:option-2
echo 2. Byebye.txt
exit /B
:option-3
echo 3. ThisIsText.txt
exit /B
:option-4
echo 4. MyBatchScript.txt
exit /B
You wrote, you tried ('%op'), ("%op"), (%op) and variations.
It should be: ("%op%")
Only forvariables and Commandline-Parameters use <Percent><char> syntax.
op is a "normal" variable and it is used <Percent><name><Percent>: %op%

How do I make a log of all ECHO commands in a BATCH file?

I am trying to use a BATCH file as a choose your own adventure story in which the user can create a character and be called by name and such by the characters in the story. It hit me an hour back that it would be cool if as or after the BATCH file containing the story was run it made a log of all of the ECHO commands so that later the player could read what they did on any given play threw.
I would like the log file to read like this:
%date% %time%
(all text displayed by echo commands for the length the file runs)
Unfortunately all I can figure out how to do is to make a loge file with just the date and time. Putting ">> StoryLog.txt" works to make the .txt file and I get the date and time in there but it just displays the text ">> StoryLog.txt" after what I want the batch file to display in echoed txt as in "You go north down the path >> StoryLog.txt" is shown on the screen. This naturally just wont work. What do I do?
for this purpose I use the following:
set LogFile=somepath\logfile.txt
set logg=^> _^&type _^&type _^>^>%LogFile%
echo this goes to screen AND file! %logg%
This is a bit tricky.
So let's disassemble that line to four parts:
set logg= ^> _ ^&type _ ^&type _^>^>%LogFile%
The Idea is to print the line to a temporary file (named _) (second part)
then type the contents of that file to screen (third part)
then type it to the logfile (fourth part).
Put that all to a variable (first part), so you don't have to type that monsterstring to every line. (this is the reason why the > and & are escaped with ^)
So every time you use
echo whatever %logg%
it will appear on the screen AND write to %logfile%
Note that this will also work:
%logg% echo whatever
Edit djangofan:
Also, you can do it with functions:
#ECHO off
:: do not enable delayed expansion since it will break this method
SETLOCAL ENABLEEXTENSIONS
SET LogFile=logfile.out
SET Logg=^> tmp.out^&^& type tmp.out^&^&type tmp.out^>^>%LogFile%
CALL :logit "This is my message!"
CALL :logit "Hear my thunder?"
GOTO :end
:logit
ECHO %~1 %Logg%
DEL /Q tmp.out
EXIT /B 0
:end
pause
Edit Stephan:
If you use CALL, the %logg% would be overkill. In that case I would just use:
:logit
echo %~1
echo %date%,%time% - %~1 >>logfile
exit /b 0
This might be the best solution to the original question, because the Date/Time will be written into logfile, but not on the screen.
Btw: you don't have to delete the tempfile every time you use it, just delete it one time, just before the batch ends.
As my other answer got quite long due to some edits, here is a new suggestion
(not much changed, but it makes the code quite easy to read):
#ECHO off
SET LogFile=logfile.out
set "say=call :logit "
%say% "This is my message!"
%say% "Hear my thunder?"
GOTO :end
:logit
ECHO %~1
echo %date% %time% - %~1 >>%logfile%
EXIT /B 0
:end
You cannot do that in batch script literally, but if you define functions, you can output data within the function block that also writes to a log file before the function returns, like so:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
echo. 2>outfile.log
CALL :functionPerson "Jon Doe" jump
GOTO :END
:functionPerson fullname action
ECHO fullname is ^"%~1^"
ECHO action is %2
ECHO %1 performs action %2>> outfile.log
EXIT /B 0
:END
pause
Each time you echo for the player to know what happens, you could also echo into your log file, adding date and time at the beginning of your line :) Simple as that for me.
Don't know how your script looks though.

Windows batch: echo without new line

What is the Windows batch equivalent of the Linux shell command echo -n which suppresses the newline at the end of the output?
The idea is to write on the same line inside a loop.
Using set and the /p parameter you can echo without newline:
C:\> echo Hello World
Hello World
C:\> echo|set /p="Hello World"
Hello World
C:\>
Source
Using: echo | set /p= or <NUL set /p= will both work to suppress the newline.
However, this can be very dangerous when writing more advanced scripts when checking the ERRORLEVEL becomes important as setting set /p= without specifying a variable name will set the ERRORLEVEL to 1.
A better approach would be to just use a dummy variable name like so:
echo | set /p dummyName=Hello World
This will produce exactly what you want without any sneaky stuff going on in the background as I had to find out the hard way, but this only works with the piped version; <NUL set /p dummyName=Hello will still raise the ERRORLEVEL to 1.
The simple SET /P method has limitations that vary slightly between Windows versions.
Leading quotes may be stripped
Leading white space may be stripped
Leading = causes a syntax error.
See http://www.dostips.com/forum/viewtopic.php?f=3&t=4209 for more information.
jeb posted a clever solution that solves most of the problems at Output text without linefeed, even with leading space or = I've refined the method so that it can safely print absolutely any valid batch string without the new line, on any version of Windows from XP onward. Note that the :writeInitialize method contains a string literal that may not post well to the site. A remark is included that describes what the character sequence should be.
The :write and :writeVar methods are optimized such that only strings containing troublesome leading characters are written using my modified version of jeb's COPY method. Non-troublesome strings are written using the simpler and faster SET /P method.
#echo off
setlocal disableDelayedExpansion
call :writeInitialize
call :write "=hello"
call :write " world!%$write.sub%OK!"
echo(
setlocal enableDelayedExpansion
set lf=^
set "str= hello!lf!world^!!!$write.sub!hello!lf!world"
echo(
echo str=!str!
echo(
call :write "str="
call :writeVar str
echo(
exit /b
:write Str
::
:: Write the literal string Str to stdout without a terminating
:: carriage return or line feed. Enclosing quotes are stripped.
::
:: This routine works by calling :writeVar
::
setlocal disableDelayedExpansion
set "str=%~1"
call :writeVar str
exit /b
:writeVar StrVar
::
:: Writes the value of variable StrVar to stdout without a terminating
:: carriage return or line feed.
::
:: The routine relies on variables defined by :writeInitialize. If the
:: variables are not yet defined, then it calls :writeInitialize to
:: temporarily define them. Performance can be improved by explicitly
:: calling :writeInitialize once before the first call to :writeVar
::
if not defined %~1 exit /b
setlocal enableDelayedExpansion
if not defined $write.sub call :writeInitialize
set $write.special=1
if "!%~1:~0,1!" equ "^!" set "$write.special="
for /f delims^=^ eol^= %%A in ("!%~1:~0,1!") do (
if "%%A" neq "=" if "!$write.problemChars:%%A=!" equ "!$write.problemChars!" set "$write.special="
)
if not defined $write.special (
<nul set /p "=!%~1!"
exit /b
)
>"%$write.temp%_1.txt" (echo !str!!$write.sub!)
copy "%$write.temp%_1.txt" /a "%$write.temp%_2.txt" /b >nul
type "%$write.temp%_2.txt"
del "%$write.temp%_1.txt" "%$write.temp%_2.txt"
set "str2=!str:*%$write.sub%=%$write.sub%!"
if "!str2!" neq "!str!" <nul set /p "=!str2!"
exit /b
:writeInitialize
::
:: Defines 3 variables needed by the :write and :writeVar routines
::
:: $write.temp - specifies a base path for temporary files
::
:: $write.sub - contains the SUB character, also known as <CTRL-Z> or 0x1A
::
:: $write.problemChars - list of characters that cause problems for SET /P
:: <carriageReturn> <formFeed> <space> <tab> <0xFF> <equal> <quote>
:: Note that <lineFeed> and <equal> also causes problems, but are handled elsewhere
::
set "$write.temp=%temp%\writeTemp%random%"
copy nul "%$write.temp%.txt" /a >nul
for /f "usebackq" %%A in ("%$write.temp%.txt") do set "$write.sub=%%A"
del "%$write.temp%.txt"
for /f %%A in ('copy /z "%~f0" nul') do for /f %%B in ('cls') do (
set "$write.problemChars=%%A%%B  ""
REM the characters after %%B above should be <space> <tab> <0xFF>
)
exit /b
As an addendum to #xmechanix's answer, I noticed through writing the contents to a file:
echo | set /p dummyName=Hello World > somefile.txt
That this will add an extra space at the end of the printed string, which can be inconvenient, specially since we're trying to avoid adding a new line (another whitespace character) to the end of the string.
Fortunately, quoting the string to be printed, i.e. using:
echo | set /p dummyName="Hello World" > somefile.txt
Will print the string without any newline or space character at the end.
A solution for the stripped white space in SET /P:
the trick is that backspace char which you can summon in the text editor EDIT for DOS. To create it in EDIT press ctrlP+ctrlH.
I would paste it here but this webpage can't display it. It's visible on Notepad though (it's werid, like a small black rectangle with a white circle in the center)
So you write this:
<nul set /p=.9 Hello everyone
The dot can be any char, it's only there to tell SET /P that the text starts there, before the spaces, and not at the "Hello".
The "9" is a representation of the backspace char that I can't display here. You have to put it instead of the 9, and it will delete the "." , after which you'll get this:
Hello Everyone
instead of:
Hello Everyone
I hope it helps
Here is another method, it uses Powershell Write-Host which has a -NoNewLine parameter, combine that with start /b and it offers the same functionality from batch.
NoNewLines.cmd
#ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - ';Write-Host -NoNewLine 'Result 2 - ';Write-Host -NoNewLine 'Result 3 - '"
PAUSE
Output
Result 1 - Result 2 - Result 3 - Press any key to continue . . .
This one below is slightly different, doesn't work exactly like the OP wants, but is interesting because each result overwrites the previous result emulating a counter.
#ECHO OFF
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 1 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 2 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 3 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 4 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 5 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 6 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 7 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 8 - '"
start /b /wait powershell.exe -command "Write-Host -NoNewLine 'Result 9 - '"
PAUSE
You can remove the newline using "tr" from gnuwin32 (coreutils package)
#echo off
set L=First line
echo %L% | tr -d "\r\n"
echo Second line
pause
By the way, if you are doing lots of scripting, gnuwin32 is a goldmine.
I made a function out of #arnep 's idea:
echo|set /p="Hello World"
here it is:
:SL (sameline)
echo|set /p=%1
exit /b
Use it with call :SL "Hello There"
I know this is nothing special but it took me so long to think of it I figured I'd post it here.
DIY cw.exe (console write) utility
If you don't find it out-of-the-box, off-the-shelf, you can DIY. With this cw utility you can use every kind of characters. At least, I'd like to think so. Please stress-test it and let me know.
Tools
All you need is .NET installed, which is very common nowadays.
Materials
Some characters typed/copy-pasted.
Steps
Create .bat file with the following content.
/* >nul 2>&1
#echo off
setlocal
set exe=cw
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*csc.exe"') do set "csc=%%v"
"%csc%" -nologo -out:"%exe%.exe" "%~f0"
endlocal
exit /b %errorlevel%
*/
using System;
namespace cw {
class Program {
static void Main() {
var exe = Environment.GetCommandLineArgs()[0];
var rawCmd = Environment.CommandLine;
var line = rawCmd.Remove(rawCmd.IndexOf(exe),exe.Length).TrimStart('"');
line = line.Length < 2 ? "\r" : line.Substring(2) ;
Console.Write(line);
}
}
}
Run it.
Now you have a nice 4KB utility so you can delete the .bat.
Alternatively, you can insert this code as a subroutine in any batch, send the resulting .exe to %temp%, use it in your batch and delete it when you're done.
How to use
If you want write something without new line:
cw Whatever you want, even with "", but remember to escape ^|, ^^, ^&, etc. unless double-quoted, like in "| ^ &".
If you want a carriage return (going to the beginning of the line), run just
cw
So try this from command line:
for /l %a in (1,1,1000) do #(cw ^|&cw&cw /&cw&cw -&cw&cw \&cw)
From here
<nul set /p =Testing testing
and also to echo beginning with spaces use
echo.Message goes here
Maybe this is what your looking for, it's a old school script... :P
set nl=^& echo.
echo %nl%The%nl%new%nl%line%nl%is%nl%not%nl%apparent%nl%throughout%nl%text%nl%
echo only in prompt.
pause
or maybe your trying to replace a current line instead of writing to a new line?
you can experiment with this by removing the "%bs%" after the "." sign and also by spacing out the other "%bs%" after the "Example message".
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "bs=%%a"
<nul set /p=.%bs% Example message %bs%
pause
I find this really interesting because it uses a variable for a purpose other than what it is intended to do. as you can see the "%bs%" represents a backspace. The second "%bs%" uses the backspace to add spaces after the "Example message" to separate the "Pause command's output" without actually adding a visible character after the "Example message". However, this is also possible with a regular percentage sign.
Sample 1: This works and produces Exit code = 0. That is Good.
Note the "." , directly after echo.
C:\Users\phife.dog\gitrepos\1\repo_abc\scripts #
#echo.| set /p JUNK_VAR=This is a message displayed like Linux echo -n would display it ... & echo %ERRORLEVEL%
This is a message displayed like Linux echo -n would display it ... 0
Sample 2: This works but produces Exit code = 1. That is Bad.
Please note the lack of ".", after echo. That appears to be the difference.
C:\Users\phife.dog\gitrepos\1\repo_abc\scripts #
#echo | set /p JUNK_VAR=This is a message displayed like Linux echo -n would display it ... & echo %ERRORLEVEL%
This is a message displayed like Linux echo -n would display it ... 1
Inspired by the answers to this question, I made a simple counter batch script that keeps printing the progress value (0-100%) on the same line (overwritting the previous one). Maybe this will also be valuable to others looking for a similar solution.
Remark: The * are non-printable characters, these should be entered using [Alt + Numpad 0 + Numpad 8] key combination, which is the backspace character.
#ECHO OFF
FOR /L %%A in (0, 10, 100) DO (
ECHO|SET /P="****%%A%%"
CALL:Wait 1
)
GOTO:EOF
:Wait
SET /A "delay=%~1+1"
CALL PING 127.0.0.1 -n %delay% > NUL
GOTO:EOF
You can suppress the new line by using the set /p command. The set /p command does not recognize a space, for that you can use a dot and a backspace character to make it recognize it. You can also use a variable as a memory and store what you want to print in it, so that you can print the variable instead of the sentence. For example:
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('"prompt $H & for %%b in (1) do rem"') do (set "bs=%%a")
cls
set "var=Hello World! :)"
set "x=0"
:loop
set "display=!var:~%x%,1!"
<nul set /p "print=.%bs%%display%"
ping -n 1 localhost >nul
set /a "x=%x% + 1"
if "!var:~%x%,1!" == "" goto end
goto loop
:end
echo.
pause
exit
In this way you can print anything without a new line. I have made the program to print the characters one by one, but you can use words too instead of characters by changing the loop.
In the above example I used "enabledelayedexpansion" so the set /p command does not recognize "!" character and prints a dot instead of that. I hope that you don't have the use of the exclamation mark "!" ;)
Use EchoX.EXE from the terrific "Shell Scripting Toolkit" by Bill Stewart
How to suppress the linefeed in a Windows Cmd script:
#Echo Off
Rem Print three Echos in one line of output
EchoX -n "Part 1 - "
EchoX -n "Part 2 - "
EchoX "Part 3"
Rem
gives:
Part 1 - Part 2 - Part 3
{empty line}
d:\Prompt>
The help for this usage is:
Usage: echox [-n] message
-n Do not skip to the next line.
message The text to be displayed.
The utility is smaller than 48K, and should live in your Path. More things it can do:- print text without moving to the next line- print text justified to the left, center, or right, within a certain width- print text with Tabs, Linefeeds, and Returns- print text in foreground and background colors
The Toolkit includes twelve more great scripting tricks.
The download page also hosts three other useful tool packages.
I found this simple one-line batch file called "EchoPart.bat" to be quite useful.
#echo | set /p=%*
I could then write something like the line below even on an interactive CMD line, or as part of a shortcut. It opens up a few new possibilities.
echopart "Hello, " & echopart "and then " & echo Goodbye
And if you're using it in batch files, the texts can be got from parameter variables instead of immutable strings. For instance:
#echopart Hello %* & #echo , how are you?
So that executing this line in "SayHello.bat" allows:
or even...
Have a play, and have fun!
I believe there's no such option. Alternatively you can try this
set text=Hello
set text=%text% world
echo %text%
Echo with preceding space and without newline
As stated by Pedro earlier, echo without new line and with preceding space works (provided "9" is a true [BackSpace]).
<nul set /p=.9 Hello everyone
I had some issues getting it to work in Windows 10 with the new console but managed the following way.
In CMD type:
echo .◘>bs.txt
I got "◘" by pressing [Alt] + [8]
(the actual symbol may vary depending upon codepage).
Then it's easy to copy the result from "bs.txt" using Notepad.exe to where it's needed.
#echo off
<nul set /p "_s=.◘ Hello everyone"
echo: here
With jscript:
#if (#X)==(#Y) #end /*
#cscript //E:JScript //nologo "%~nx0" %*
#exit /b %errorlevel%
*/if(WScript.Arguments.Count()>0) WScript.StdOut.Write(WScript.Arguments.Item(0));
if it is called write.bat you can test it like:
call write.bat string & echo _Another_String_
If you want to use powershell but with cmd defined variables you can use:
set str=_My_StrinG_
powershell "Write-Host -NoNewline ""%str%"""" & echo #Another#STRING#
Late answer here, but for anyone who needs to write special characters to a single line who find dbenham's answer to be about 80 lines too long and whose scripts may break (perhaps due to user-input) under the limitations of simply using set /p, it's probably easiest to just to pair your .bat or .cmd with a compiled C++ or C-language executable and then just cout or printf the characters. This will also allow you to easily write multiple times to one line if you're showing a sort of progress bar or something using characters, as OP apparently was.

Resources