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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

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

#1 Post by Meerkat » 30 Jan 2017 04:44

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

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

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

#2 Post by pieh-ejdsch » 30 Jan 2017 06:41

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

Post Reply