Echo appending to a text file - batch-file

So, when I'm echoing text from a batch file to a text file, I can escape the special characters without issue. However, when I echo a certain character combination, I can't seem to find a way around the fact that it is just echoing to the screen and not to the file.
Here's what works...
echo firsttest^=uncpath>>test.txt
echo [secondtest]>>test.txt
Here's what doesn't work...
echo thirdtest^=1>>test.txt
echo fourthtest^=2>>test.txt
I've tried escaping one and both of the arrow characters, but still no-go.
Any advice? I'm sure it has to do with the standard redirection of console output using the "1>" and the "2>", but just don't know how to get around that.
Thanx.

Your analysis is correct. There are two main concepts to work around it:
(echo thirdtest=1)>>test.txt
(echo fourthtest=2)>>test.txt
and
>>test.txt echo thirdtest=1
>>test.txt echo fourthtest=2
(Note: = isn't one of the special characters that need escaping)
Where in the line you put your redirection doesn't matter, as running with echo on shows. (if you enjoy to get confused, enter echo hello>file.txt world, followed by type file.txt)
Another note: if you echo several lines, it's faster to:
(
echo first line
echo second line
echo third line
)>>test.txt
(reason: needs only one disk access (read/modify/write) instead of doing the same for each single line) You won't notice it with just three lines, but think of writing hundreds of lines (for example in a loop). Time savings will be huge.

Related

DOS v6 era batch echo and ver on same line

What I have:
I have a simple batch file with ASCII characters to make a semi graphical menu for MS DOS, drawing a box and placing text with "#echo off" at the start of my file and each line in editor beginning with "echo". I have not implemented color codes, nothing fancy yet.
Is there a way to display the output of the 'ver' command on the same line as something that is 'echo'd?
Basically, I would like to attempt and achieve and output on a line similar to:
|| <---MS-DOS ver. 6.22---> ||
Where the pipes are ASCII characters (ALT-186) and 'escape' the echo to run 'ver' then return to printing the end character (alt-186)
I remember doing something like it YEARS ago when I was fumbling around on my computer, but unlike riding a bike, I have forgotten many tricks I had at the time.
I have Googled for quite some time, the last few days between work and sleep on my free time, but everything is geared toward the Windows XP or newer command line instead of DOS. I have read many articles on batch scripting and while helpful in relearning other tricks, they are all still geared toward newer CLI. I have read up on escaping and for some reason I am just not getting it. Maybe I have spent too much time trying to figure this out on my own and have burned out? Any help or link to the proper articles will be appreciated. Sample code even better as that is how I learned way back then.
For the case of MSDOS-Version exists a simple solution.
echo #prompt $b$b $l--$V--$g $b$b > temp.bat
%comspec% /c temp.bat
This works, because $V will be translated into the MSDOS-Version, for output of other programs it's more complicated.
Attention: It only works inside a batch program, because variable expansion isn't supported on the command line.
On the command line you could use:
echo #prompt $b$b $V $b$b > temp.bat
C:\command.com /c temp.bat
Output: || <--MS-DOS Version 6.22--> ||
In MS-DOS 6.22 it's tricky to output text without line feed.
In the most cases you should build the complete line before you try to output it.
Or you can try predefined files without a linefeed.
Today I do the echo without line feed this way, ...
echo.|set /P =text without CR/LF
... but I can't say if this would have worked in the early nineties.

Use content of a file in a string

I have a file (let's call it version.txt) that contains a version number and some text:
v5.02
Some text explaining
where and how this
number is used
Based on this answer, I use
set /p version=<version.txt
to store the first line of the file in the version variable. Now I'm trying to write a batch script that operates on folders that contain this version number in their name. However, I get unexpected results because something seems to go wrong when I insert the variable in a path. For example, this script
#set /p version=<version.txt
#echo C:\some\folder\%version%\some\file.exe
prints
C:\some\folder\v5.02
instead of
C:\some\folder\v5.02\some\file.exe
What's going on? I have a feeling there are hidden characters of some sort at the end of the text in the variable, because setting the variable by hand to a constant in the script works.
Edit: I'm using Windows 10 with Notepad++ as my editor, if it helps.
I can only replicate your issue, when version.txt uses Unix line endings (LF) instead of Windows (CRLF). for /f is immune to this issue:
for /f "delims=" %%a in (version.txt) do set "verion=%%a" & goto :skip
:skip
echo C:\some\folder\%version%\some\file.exe
goto :skip breaks the loop after reading the first line.
Since everything I tried didn't seem to work, the solution I found in the end is to call the batch script from a Python script. The Python script reads the first line of the version file and passes it as an argument to the batch script. Out of context, it is a bit of an inelegant solution, but in my case the batch script was already called by a Python script, so it's not that terrible.
Here is a minimal example:
version.txt
v5.02
Some text explaining
where and how this
number is used
script.bat
#echo C:\some\folder\release\%1\some\file.exe
script.py
import os
with open("version.txt") as f:
version = f.readline().rstrip()
os.system("cmd /c script.bat %s" % version)
Edit: Following Stephan's comment, I tried to change the line ending in the text file from LF to CRLF and it indeed solves the problem. However, since I don't really have control over everything that writes in that file, the solution above remains the most feasible in my case.
Edit 2: Stephan's answer (with the for loop) is actually a better solution than this one since it avoids having to transfer part of the work to the calling Python script.

How to write a block of text (multiple lines text) to a file in Windows via batch file without using multiple echo calls?

I have been trying to write multiple lines of text to a text file in Windows but don't know how to do this. Also, I've searched on the internet a lot about this but all solutions use echo command multiple times.
Is there any way to do this without using echo command multiple times like "cat" in Linux?
more >> filename.txt
Should do what you need
To echo text with a single line should be possible although not easy/elegant. You'll need a few more lines to store the new line character before using it
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem just set once, and after that you can use %%NL%% to echo a new line everywhere you want
echo This is a sentence%NL%that's broke down into 2 lines.%NL%And this is another line.
But why would you want that? That'll make the line super long and nobody likes horizontal scrolling. It's also possible to do that in one line with multiple echo calls
C:\Users>echo This is a very long line of text & echo that requires you to scroll horizontally. & echo And people HATE horizontal scrolls
This is a very long line of text
that requires you to scroll horizontally.
And people HATE horizontal scrolls
So the neatest way is to use powershell
C:\Users>powershell -Command "Write-Host This is`na multiline`ntext"
This is
a multiline
text
`n is the newline because ` is the escape character in powershell
Alternatively just use powershell's echo
C:\Users>powershell -Command "echo 'Each parameter to echo' '(A.K.A Write-Output)' 'will be printed on a separate line' 'like this'"
Each parameter to echo
(A.K.A Write-Output)
will be printed on a separate line
like this
If using multiple lines is allowed, but with just a single echo then escape the new line with ^ and leave the next line blank (i.e. 2 lines of code for each output line)
(echo Line 1^
Line 2^
Line 3) >file.txt

What does ECHO .> mean in batch file?

I have a batch file with the following code within:
ECHO .> C:\file.txt
I read about ECHO and understand what it is used for, but what I do not know is what are the characters used for after the word echo (.>) and what is the use of the path of file after that.
It's used to truncate, or create if necessary, the file. echo . outputs a single line which is redirected to the file, effectively truncating it.
To obtain an absolutely empty file I often use cd . > filename. I don't know where I picked that one up but it's been around for a long time on UNIX systems.
> redirects the output of the command before.
echo .>c:\file.txtprints a dot to the file c:\file.txt, overwriting its contents (so it will contain a dot only afterwards)
I think, you got it wrong. Usually echo.>file.txt is used to create a empty file (or delete the contents, if the file exists). (Note the missing space)

Storing multi-word strings to a file

I've recently been trying to make a program to simply store text to a file for later viewing, storing it as a .rar file for security against those who don't understand how to extract the text from the .rar (i.e. the less "techy" people)...
I have, however, encountered an error in the program that results in the <word> not expected at this time followed by the .exe closing when I input add/<word> <word>... (i.e. any multi-word string with spaces in between the words [add/<word>, however, does function properly]).
Is there a special rule that must be followed for storing multi-word strings to a .rar or a file in general (I do, however, know that there is a rule for creating/renaming folders/directories)?
The Program Segment:
:command
cls
set /p journal=<journal.rar
echo %journal%
echo.
set /p command=What would you like to do?
cls
if %command%==exit exit
if %command%==help goto help
if %command%==delete echo START; > journal.rar
if %command:~0,4%==add/ echo "%journal%""%command:~4%;" > journal.rar
if %command:~0,5%==edit/ echo %journal:%command:~5%=%;" > journal.rar
goto command
Excuse me. Your question is not clear. There are several confusing points in it, like "followed by the .exe closing" (which .exe is closing?), and the fact that your question is NOT related to .rar files in any way, nor to "storing multi-word strings". However, I can see the following points in it:
When a variable value is expanded with percent signs this way: %command% you must be aware that the variable is first expanded and then the resulting line is parsed. This mean that the value of the variable may introduce errors in the line. For example, in this line: if %command%==exit exit, if the value of command variable is add/one two three, then the line that is parsed is this: if add/one two three==exit exit that, of course, issue an error! (type if /? for further details).
The way to avoid this problem is enclosing both the variable and the comparison value in quotes; this way, if the value have several words with spaces, the whole value is grouped in the IF command for comparison purposes: if "%command%" == "exit" exit. This must be done in every IF command that use the value of the variable.
In the following line:
if %command:~0,5%==edit/ echo %journal:%command:~5%=%;" > journal.rar
you must be aware that the line is parsed from left to right; this means that you can not nest a %variable% expansion inside another one. The way to solve this problem is first complete a %normal% variable expansion, and then a !delayed! variable expansion that will take the previous expanded value. To do that, insert this line at beginning of your program:
setlocal EnableDelayedExpansion
and change previous line by this one:
if "%command:~0,5%" == "edit/" echo !journal:%command:~5%=!;" > journal.rar
For further details, type set /? and carefully read the sections about "delayed expansion".
Here is a sample that can accept multiple words:
set "command="
set /p "command=What would you like to do? "
cls
if /i "%command%"=="have lunch" goto :food

Resources