(SOLVED-again) Write each line of text to newtext.txt
Moderator: DosItHelp
(SOLVED-again) Write each line of text to newtext.txt
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
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.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Write each line of text to newtext.txt
Puts each line of "my text.txt" into it's corresponding "file #.txt". Includes blank lines.
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 "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"
)
Re: Write each line of text to newtext.txt
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
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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Write each line of text to newtext.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". Includes blank lines.
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 "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"
)
)
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Write each line of text to newtext.txt
@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
@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
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Write each line of text to newtext.txt
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. Code could be written to ignore those too, but not important enough.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Write each line of text to newtext.txt
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. 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.
Re: Write each line of text to newtext.txt
Thanks orange_batch once again!
Now I have the script to automate the complete Adobe InDesign Data-merge process.
Now I have the script to automate the complete Adobe InDesign Data-merge process.
-
- Expert
- Posts: 442
- Joined: 01 Aug 2010 17:13
- Location: Canadian Pacific
- Contact:
Re: Write each line of text to newtext.txt
Yeah I'm pimp like that. Represent.
Re: Write each line of text to newtext.txt
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
Re: Write each line of text to newtext.txt (re-opening threa
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