Lachlan Jansen
Lachlan Jansen

Reputation: 55

insert new line after every character [batch-file]

I am trying to create a batch-file script to convert a normal .txt document to one that is completely vertical. For example:

From:

This is a test document,
it is displaying characters on the same line.

To:

T
h
i
s

i
s

a

t
e
s
t

d
o
c
u
m
e
n
t
,

i
t

i
s

d
i
s
p
l
a
y
i
n
g

c
h
a
r
c
t
e
r
s

o
n

t
h
e

s
a
m
e

l
i
n
e
.

Upvotes: 1

Views: 716

Answers (2)

aschipfl
aschipfl

Reputation: 34989

The following script is based on MC ND's answer and avoids additional line-breaks in place of line-breaks that also occur in the original file:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "SPACE= "
set "FLAG=#"
for /F delims^=^ eol^= %%C in ('
    cmd /U /C type "%~dpn0.txt" ^| find /N /V ""
') do (
    set "CHAR=%%C"
    setlocal EnableDelayedExpansion
    set "CHAR=!CHAR:*]=!"
    if defined CHAR (
        echo(!CHAR!
        endlocal
        set "FLAG=#"
    ) else (
        if defined FLAG (
            echo(%SPACE%
            endlocal
            set "FLAG="
        ) else endlocal
    )
)

endlocal
exit /B

Upvotes: 1

MC ND
MC ND

Reputation: 70971

Maybe this will not completly fit your needs, but as a starting point you can try with

cmd /u /c"type normal.txt" | find /v "" > vertical.txt 

This will start a cmd instance with unicode output. Inside this instance the normal.txt file is send to standard output. All the output from the cmd instance is piped into a find command that will see the null characters (the unicode output is a two bytes per character sequence, one of them a null or 0x00 ascii) as line terminators and will output a new line after each character. All the data is redirected to the output file.

Upvotes: 5

Related Questions