Page 1 of 1

About "Read words separated by space in a batch script"

Posted: 30 Jan 2017 04:44
by Meerkat
Hello! This is about DBenham's answer in SO here: http://stackoverflow.com/a/19121445.

The code is awesome because it does not treat comma as delimiter. However, even though there is an exclamation in the file, the character is not displayed.

Example.txt

Code: Select all

Rock and Roll!
What am I doing in my laip?

Code.bat

Code: Select all

@echo off
setlocal disableDelayedExpansion

:: Define LF to contain a newline character
set LF=^


:: Above 2 blank lines are critical - DO NOT REMOVE

for /f "eol= tokens=*" %%A in (Example.txt) do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  for %%L in ("!LF!") do for /f "eol= " %%W in ("!ln: =%%~L!") do echo %%W
  endlocal
)

Output:

Code: Select all

Rock
and
Roll
What
am
I
doing
in
my
laip?
What can be a remedy for it?

Meerkat

Re: About "Read words separated by space in a batch script"

Posted: 30 Jan 2017 06:41
by pieh-ejdsch
You must not generate the line jump seperat, he can be in a loop also into a variable created.
Around an exclamation point which was read in a loop to output, the delayed variable representation must be deactivated.

Code: Select all

@echo off
setlocal disableDelayedExpansion
for %%L in (^"^
%== !LF -- this is an EMPTY Line ! ==%
)do for /f "eol= delims= tokens=*" %%A in (Example.txt)do (
  set "ln=%%A"
  setlocal enableDelayedExpansion
  for /f delims^=^ eol^= %%W in ("!ln: =%%~L!")do (if !!* == * endlocal
    echo(%%W
  )
)
pause
Example.txt

Code: Select all

Rock and Roll! & 100% "   TAB   . 
<code >^ ^ ^^ </code>
with comma , : ; ~ * / { [ ] } What ( is ) am I doing in my laip?
+ # '  | @
Phil