(SOLVED-again) Write each line of text to newtext.txt

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

(SOLVED-again) Write each line of text to newtext.txt

#1 Post by jmituzas » 30 Aug 2010 08:52

I need help with the following:

I have a tab delimited txt file that I need help exporting further.
This file may be between 2 - 50 lines of code.

What I need it to do is either create each line of text to its own .txt file. each numbered by its line number within the text file.

example:

line1 want to create file 1.txt
line2 want to create file 2.txt
line3 want to create file 3.txt
line4 want to create file 4.txt

Like I have stated the number of lines in this file will change so it can't be assigned for just lines 1-4.
Thanks in advance,
Joe
Last edited by jmituzas on 13 Oct 2010 13:23, edited 4 times in total.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Write each line of text to newtext.txt

#2 Post by orange_batch » 30 Aug 2010 09:58

Puts each line of "my text.txt" into it's corresponding "file #.txt". Includes blank lines.

Code: Select all

setlocal enabledelayedexpansion

for /f "delims=] tokens=1*" %%x in ('type "my text.txt" ^| find /v /n ""') do (
set /a counter+=1
echo:%%y>"file !counter!.txt"
)

Puts each line of "my text.txt" into it's corresponding "file #.txt". Does not include blank lines.

Code: Select all

setlocal enabledelayedexpansion

for /f "usebackq delims=" %%x in ("my text.txt") do (
set /a counter+=1
echo:%%x>"file !counter!.txt"
)

jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

Re: Write each line of text to newtext.txt

#3 Post by jmituzas » 30 Aug 2010 11:49

Thanks for quick reply.

One more thing I need to do with this script is to output the first line of text along with these files we just outputted.

example of my text.txt file

line1 want to output to file 1.txt

line1 + line2 to 2.txt
line1 + line3 to 3.txt
line1 + line4 to 4.txt

is this possible or do i have to use a temp file for line1 and prepend that to each file afterwards?

Thanks in advance,
Joe

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Write each line of text to newtext.txt

#4 Post by orange_batch » 30 Aug 2010 22:02

If line is line 1, stores it in a variable. If not line 1, puts line 1 variable plus current line of "my text.txt" into it's corresponding "file #.txt". Includes blank lines.

Code: Select all

setlocal enabledelayedexpansion

for /f "delims=] tokens=1*" %%x in ('type "my text.txt" ^| find /v /n ""') do (
set /a counter+=1
if !counter!==1 (set "line1=%%y") else (
echo:!line1!>"file !counter!.txt"
echo:%%y>>"file !counter!.txt"
)
)

If line is line 1, stores it in a variable. If not line 1, puts line 1 variable plus current line of "my text.txt" into it's corresponding "file #.txt". Does not include blank lines.

Code: Select all

setlocal enabledelayedexpansion

for /f "usebackq delims=" %%x in ("my text.txt") do (
set /a counter+=1
if !counter!==1 (set "line1=%%x") else (
echo:!line1!>"file !counter!.txt"
echo:%%x>>"file !counter!.txt"
)
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Write each line of text to newtext.txt

#5 Post by ghostmachine4 » 31 Aug 2010 00:31

@orange, note that for code that does not include blank lines, it will fail when"blank lines" actually contains more than 1 spaces or tabs on the line.


@OP, if you can download gawk for windows, here's an alternative. Just one line

Code: Select all

C:\test>gawk "NR==1{p=$0}NF{print p\"\n\"$0 > \"file\"++c\".txting\" }" file

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Write each line of text to newtext.txt

#6 Post by orange_batch » 31 Aug 2010 02:45

ghostmachine4 wrote:@orange, note that for code that does not include blank lines, it will fail when"blank lines" actually contains more than 1 spaces or tabs on the line.[/code]

But then the line isn't actually blank. 8) Code could be written to ignore those too, but not important enough.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Write each line of text to newtext.txt

#7 Post by ghostmachine4 » 31 Aug 2010 03:54

orange_batch wrote:
ghostmachine4 wrote:@orange, note that for code that does not include blank lines, it will fail when"blank lines" actually contains more than 1 spaces or tabs on the line.[/code]

But then the line isn't actually blank. 8) Code could be written to ignore those too, but not important enough.

in computing terms, blanks or whitespaces does not necessary mean just spaces. It includes tabs, newlines, backspaces etc.
OP's data doesn't necessary have whitespaces as single blank line, but its good to take care of that if encountered. Just a point to take note for the OP.

jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

Re: Write each line of text to newtext.txt

#8 Post by jmituzas » 31 Aug 2010 06:07

Thanks orange_batch once again!

Now I have the script to automate the complete Adobe InDesign Data-merge process.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Write each line of text to newtext.txt

#9 Post by orange_batch » 31 Aug 2010 08:16

Yeah I'm pimp like that. Represent.

Image

jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

Re: Write each line of text to newtext.txt

#10 Post by jmituzas » 06 Oct 2010 06:42

orange_batch wrote:If line is line 1, stores it in a variable. If not line 1, puts line 1 variable plus current line of "my text.txt" into it's corresponding "file #.txt". Does not include blank lines.

Code: Select all

setlocal enabledelayedexpansion

for /f "usebackq delims=" %%x in ("my text.txt") do (
set /a counter+=1
if !counter!==1 (set "line1=%%x") else (
echo:!line1!>"file !counter!.txt"
echo:%%x>>"file !counter!.txt"
)
)


Am having a bit of a problem with this one, will try to explain.

In excel if you create a worksheet with data to run a job that takes up 15 rows , once that job is complete and you use the same sheet just deleting all data but row 1 and this job only has 5 rows, then decide to save as tab-delimited text it will output 15 rows again. Now left with numerous blank lines that convert to files with only the first line within them.

I am not sure but I think that this is picking up the first line not being blank so outputs the files. What I need is if the other lines are blank not to output those files.
Please help me complete this script.
Thanks in advance - with best regards,
Joe

jmituzas
Posts: 13
Joined: 30 Aug 2010 08:43

Re: Write each line of text to newtext.txt (re-opening threa

#11 Post by jmituzas » 13 Oct 2010 13:21

figured out how to check right from excel: http://www.excelforum.com/excel-programming/748839-exporting-worksheets-as-tab-delimited-text-vbs.html

Code: Select all

Sub LipoSuction()
'JBeaucaire (8/3/2009)
Dim LR As Long, LC As Long
Dim ws As Worksheet

For Each ws In Worksheets
    LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row + 1
    LC = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column + 1

    'Clear everything below column A last cell and row 1 last cell
        ws.Range(ws.Cells(1, LC), ws.Cells(ws.Rows.Count, ws.Columns.Count)).Clear
        ws.Range(ws.Cells(LR, 1), ws.Cells(ws.Rows.Count, ws.Columns.Count)).Clear
Next ws

End Sub

Post Reply