One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com
Using 89% of the same design the blog had in 2001.
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike,
always wear white.
As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green.
but if I want something quick that can run anywhere [someone's on Windows], I use a batch file, and over the years, kinda like VIm, I've slowly become if not proficient then at least competent.
Here you go, create a .bat file with the following in it :
@echo off
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
echo There should be a newline%NL%inserted here.
echo.
pause
Wha? set NL=^^^%NLM%%NLM%^%NLM%%NLM%??? What in the world? Huh?
The caret is an escape character for the next character, or at the line end it is used as multiline character, but this is nearly the same.
At the line end it simply escapes the next character, in this case theย <Linefeed>, but there is a hidden feature, so if the escaped character is aย <LF>ย it is ignored and the next character is read and escaped, but this charater will be always escaped, even if it is also aย <LF>.
...
So you need to add an escaped linefeed into the line and that is the NL-Variable. The NL-Variable consists of only three characters. NL=^<LF><LF>ย And if this is expanded, it creates only one escapedย <LF>ย as the firstย <LF>ย after the caret will be ignored.
I mean, um, that requires Inception level cognition to understand. ๐ (Okay, actually it's pretty straightforward, but talk about a lack of discoverability.)
What's even more interesting is that dev's answer to the original question, which is this:
Like the answer of Ken, but with the use of the delayed expansion.
setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)
echo Line1!\n!Line2
echo Works also with quotes "!\n!line2"
Aside
Okay, before I point out what I want to point out, note that %=_____=% is an inline comment format for batch.
From ss64.com, a common domain when I'm in batchland:
Batch variableย namesย can contain spaces and punctuation, we can take advantage of this fact by typing our comment as a non-existent variable. Because it doesnโt exist, this variable will expand to nothing and so will have no effect when the script is run.
n.b. This only works in batch files, not directly at the command prompt.
To be sure that we donโt accidently choose a name which is in fact a real variable, start and end the comment with "="
@Echo off
Echo This is an example of an %= Inline Comment =% in the middle of a line.
(Variable names starting with "=" are reserved for undocumented dynamicย variables. Those dynamic variables never end with "=", so by using an "=" at both the start and end of our comment, there isย noย possibility of a name clash.)
And we're back
Okay, now what I'd like to point out here is the setlocal EnableDelayedExpansion, something I think I had to use recently in a script I wrote. It's a bizarre new world.
Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time[;] this option is turned on with the SETLOCAL
EnableDelayedExpansionย command.
Variable expansion means replacing a variable (e.g.ย %windir%)ย with its valueย C:\WINDOWS
By default expansion will happen just once, before each line is executed. In this case, a "line" includes bracketed expressions even if they are formatted to run over several lines.
Whenย !delayed!ย expansion is turned on, the variables will be expanded each time the line is executed, or for each loop in a FOR looping command.
And here's a decent example from that page showing how it works in practice.
@echo off SETLOCALย EnableDelayedExpansion Set "_var=first" Set "_var=second"ย & Echo %_var% !_var!
The output is
first second
Note that the above example has to be in a batch file to work properly.
Anyhow, it's an insane rabbit hole that I didn't schedule for this sprint and fell into anyhow. I'm going to cut my losses now that I've warned others who may have been as naรฏve about batch scripts as I was, oh so long (less than three weeks ๐) ago.
Oh good heavens. I guess this makes sense. The first example can be taken by itself as a brain teaser, I think.
@echo off
Setlocal
Set _html=Hello^>World
Echo %_html%
In the above, the Echo command will create a text file called 'world' - not quite what we wanted!
Got that figured out? If not, sit and think about it for a second before reading more.
Got it? Good. Clever, huh?
This is because the variable is expanded at parse time, so the last line is executing Echo Hello > World and the > character is interpreted as a redirection operator.
If we now try the same thing with EnableDelayedExpansion:
Setlocal EnableDelayedExpansion
Set _html=Hello^>World
Echo !_html!
With delayed expansion, the variable (including the > ) is only expanded at execution time so the > character is never interpreted as a redirection operator.
This makes it possible to work with HTML and XML formatted strings in a variable.
The longer I work in Windows, the more I find myself using cmd.exe. I use PowerShell plenty too, but if I want something quick that can run anywhere [someone's on Windows], I use a batch file, and over the years, kinda like VIm, I've slowly become if not proficient then at least competent.
Nearly (and maybe even over at this point) thirty years ago I had a guy wisely tell me, when I was considering buying a new Mac or Windows PC, "It's all zeros and ones." Same for script languages, mostly. It might be a pain to learn batch scripting on Windows sometimes, but there's very little you can't do if you set your mind to it.
REM https://stackoverflow.com/questions/18462169/how-to-loop-through-array-in-batch
echo off
set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut
set "x=0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
Clever.
While I'm at it, here's a PowerShell script I've been using to approximate grep there. I've dabbled in this problem before, but it's usually a good idea to reduce it to script instead of leaving only human-readable lessons learned:
That captures the Out-File wackiness from the previous post but also wraps Select-Stringing a file, making it easier to remember, not that it's difficult. I should fix the casing.
The postings on this site are [usually] my own and do not necessarily reflect the views of any employer, past or present, or other entity. About Our Author