diva
diva

Reputation: 21

split txt file line by line and create from each line a txt file

I need a batch script which splits a file with 4 lines and creates from those 4 lines 4 txt files.

Looks like this: test.txt

line 1
line 2
line 3
line 4

--> every line should be outputted in an other txt file.

Thanks guys, Anita

Upvotes: 1

Views: 830

Answers (3)

Aacini
Aacini

Reputation: 67216

EDIT: I simplified the code a little...

@echo off

for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test.txt') do (
   > file%%a.txt echo(%%b
)

If a line in the file start with colon, this solution will eliminate the colon. This problem may be fixed, if needed.

Upvotes: 1

MC ND
MC ND

Reputation: 70923

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "inputFile=test.txt"
    for %%z in ("%inputFile%") do if %%~zz gtr 0 (
        set "lineNumber=1000000000"
        for /f "delims=" %%a in ('
            findstr /n "^" "%inputFile%"
        ') do (
            set "line=%%a" & set /a "lineNumber+=1" 
            setlocal enabledelayedexpansion 
            >"%%~fz.!lineNumber:~-9!" (echo(!line:*:=!)
            endlocal
        )
    )

Upvotes: 2

npocmaka
npocmaka

Reputation: 57252

@echo off
set "filen=c:\file.txt"

for /f "usebackq tokens=* delims=" %%a in ("%filen%") do (
   (echo(%%a>%%a)
)

Upvotes: 0

Related Questions