0

I have a Sample.txt file where the content is:

Hi How Are You?

Using batch file, I have to split the words vertically after every space. Output should be like this:

Hi
How
Are
You?
aschipfl
  • 33,626
  • 12
  • 54
  • 99
user3818587
  • 303
  • 4
  • 8

2 Answers2

0
for /f "delims=" %%a in (sample.txt) do (
  for %%b in (%%a) do (
    echo %%b
  )
)

First (outer) for processes each line in the file, second (inner) for processes each word in that line.

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

You could read the file into a variable, using:

< "Sample.txt" set /P LINE=""

Or, if the line is longer than 1021 bytes (!), using this:

for /F usebackq^ delims^=^ eol^= %%L in ("Sample.txt") do (
    set "LINE=%%L"
)

Then you could replace every SPACE by a line-break, like this:

setlocal EnableDelayedExpansion
echo(!LINE: =^

!
endlocal

Or, when first storing the line-break in a variable, like this:

(set LF=^

)
setlocal EnableDelayedExpansion
echo(!LINE: =^%LF%%LF%!
endlocal

The empty line in each of the above code variants is mandatory to get a line-break in the output. Reference this answer by user jeb for how this new-line hack works.

I am using delayed expansion here in order to avoid trouble with special characters.

aschipfl
  • 33,626
  • 12
  • 54
  • 99