Writing a word on each line

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
macrunning
Pro Scripter
Posts: 68
Joined: Wed May 04, 2005 10:24 pm

Writing a word on each line

Post by macrunning » Tue Apr 04, 2006 12:40 am

Hello,
I need some help. I need to write a word or number at the beginning of each line of a .txt file. The number of lines will vary from day to day. I need to open the file and somehow get the word TEAM or 123 at the beginning of each line. Then I need it to stop once it gets done with the last line in the file.
Any help on this would be greatly appreciated...
Thanks!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Apr 04, 2006 1:56 am

Something like this maybe:

Let>CF_OVERWRITE=1
Let>k=1
DeleteFile>c:\test\test2.txt
Label>readfile
Let>prefix=123
ReadLn>c:\test\test1.txt,k,line
If>line=##EOF##,doneread
Concat>%prefix%,%line%
Writeln>c:\test\test2.txt,result,%prefix%
Let>k=k+1
Goto>readfile
Label>doneread
CopyFile>c:\test\test2.txt,c:\ftps\test1.txt
Label>end

macrunning
Pro Scripter
Posts: 68
Joined: Wed May 04, 2005 10:24 pm

Post by macrunning » Tue Apr 04, 2006 2:04 am

Umm, thanks!
What is ##EOF## suppose to be?

macrunning
Pro Scripter
Posts: 68
Joined: Wed May 04, 2005 10:24 pm

Post by macrunning » Tue Apr 04, 2006 2:23 am

Hmmm, the more I look at it the more I think its not quite what I was thinking. I don't need 2 different files. I have one .txt file that gets opened in notepad, the script runs thru it and cleans it of junk I don't need for my database. But now I need to put a (prefix) at the beginning of each line in the .txt file. This will become a name field in my database. I was thinking it would just be easy to do it while the script was already in notepad cleaning the file but I can see I dont neccessarily have to do it that way. I'd like to just run thru each line a think while notepad is open. Any ideas?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Apr 04, 2006 2:31 am

If what you want is to modify your cleaning script to do this, then please post that script.

For future reference that =##EOF## is what ReadLn> returns when the end of the file has been reached.

macrunning
Pro Scripter
Posts: 68
Joined: Wed May 04, 2005 10:24 pm

Post by macrunning » Tue Apr 04, 2006 2:32 am

Well I thought I would try this script out, but made a small modifcation to it and it doesn't seem to run at all. The file I am trying to place the prefix on each line is on the desktop. Here is what I am trying:

Let>CF_OVERWRITE=1
Let>k=1
DeleteFile>C:\Documents and Settings\Jason\Desktop\temp2.txt
Label>readfile
Let>prefix=Team,
ReadLn>C:\Documents and Settings\Jason\Desktop\myfile.txt,k,line
If>line=##EOF##,doneread
Concat>%prefix%,%line%
Writeln>C:\Documents and Settings\Jason\Desktop\myfile.txt,result,%prefix%
Let>k=k+1
Goto>readfile
Label>doneread
//CopyFile>c:\test\test2.txt,c:\ftps\test1.txt
Label>end

macrunning
Pro Scripter
Posts: 68
Joined: Wed May 04, 2005 10:24 pm

Post by macrunning » Tue Apr 04, 2006 2:56 am

Cool, I got it to work. Thanks for your help.

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Mon Aug 07, 2006 11:45 am

But why do you have to go line-line??

I would do it like:

Code: Select all

ReadFile>YourFilePath,FileText
RPL>FileText,CRLF,%CRLF%%YourString%,FileText

DeleteFile>YourFilePath
WriteLn>YourFilePath,wresult,%YourString%%FileText%
Probably it's good to check in the beginning that there are no empty strings at the end, or to remove them. Trim doesn't work for newlines, so I wrote for such cases my trimming function:

Code: Select all

Sub RemoveTrailingSpaces(newStr)
   newStr = Trim( newStr )
   newStr = RegExpReplace( "^(\r|\n)+", newStr, "" )
   GlobalReturnStr = RegExpReplace( "(\r|\n)+$", newStr, "" )
End Sub
The code for the RegExpr etc. look at
http://www.mjtnet.com/forum/viewtopic.php?t=3113
and
http://www.mjtnet.com/forum/viewtopic.php?t=3112

call it from MSch:

Code: Select all

VBRun>RemoveTrailingSpaces,FileText
VBEval>GetResultString(),FileText
Good luck!
Olga.

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Mon Aug 07, 2006 11:47 am

And in the Notepad,

Press CTRL
Send>a
Release CTRL
WaitClipboard
GetClipboard>FileText

etc.

OlgaFB
Pro Scripter
Posts: 58
Joined: Mon Nov 01, 2004 3:04 pm
Contact:

Post by OlgaFB » Mon Aug 07, 2006 12:00 pm

Now it's interesting, who of us is a Junior Coder. :))

By the way, interesting - question to Marcus - what happens when you call a label by a name of a command (readfile)? - nothing? Proceed as always?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Aug 11, 2006 2:36 am

OlgaFB wrote:But why do you have to go line-line??

I would do it like:

Code: Select all

ReadFile>YourFilePath,FileText
RPL>FileText,CRLF,%CRLF%%YourString%,FileText

DeleteFile>YourFilePath
WriteLn>YourFilePath,wresult,%YourString%%FileText%
Good luck!
Olga.
That's a good idea Olga. You certainly could use ReadFile> but my experience is that ReadFile> will choke "out of memory" on very large files. I would guess the limit depends to some extent on the size of PC memory. Unless I know for sure that the file is relatively small (in this case the question doesn't provide that info) I would prefer to be conservative and suggest the more robust solution of using ReadLn>.

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts