-1

I have .txt file contain 297 line, i want every line of that copied into new .txt file, so it will contain 297 file, for file name each file like this line1.txt line2.txt line3.txt, it possible using batch?

I have try using findrepl.bat and instruction form here batch to copy FIRST line of multiple text files but thats script for first line only.

Community
  • 1
  • 1
DikBagus
  • 1
  • 2
  • 4
    Please help yourself. This website is not a free code-factory. Have you made any effort whatsoever to solve this problem? – Blorgbeard Feb 25 '14 at 02:04
  • 1
    What have you tried so far? Also, keep in mind that Windows has restrictions on filename length and certain characters (e.g., "\"). Perhaps there is a better approach to whatever you're trying to accomplish. – mbroshi Feb 25 '14 at 02:06

3 Answers3

1

FOR /F is what you want, with the use of SET /A to numerically increment a variable. Because CMD/batch is really only a punch card reader pretending to be a shell, the contents of source_file.txt can cause the script to break. In particular, if it has any special shell characters (like < > ! " or &), the shell will interpret those as special characters and not just echo them to the output file.

SETLOCAL ENABLEDELAYEDEXPANSION
SET LINENO=1
FOR /F "delims=" %%l IN (source_file.txt) DO (
    ECHO %%l>file!LINENO!.txt
    SET /A LINENO=LINENO+1
)
mojo
  • 4,050
  • 17
  • 24
0

This should split a file and create separate files for every line.

The filenames will be the same as each line.

@echo off
for /f "delims=" %%a in (file.txt) do >>"%%a.txt" echo %%a
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0
setlocal enableextensions disabledelayedexpansion
for /f "usebackq tokens=1,* delims=:" %%a in (
    'findstr /n "^" "file.txt"'
) do echo(%%b>line%%a.txt
endlocal
MC ND
  • 69,615
  • 8
  • 84
  • 126