Writing a word on each line
Moderators: JRL, Dorian (MJT support)
-
- Pro Scripter
- Posts: 68
- Joined: Wed May 04, 2005 10:24 pm
Writing a word on each line
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!
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!
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
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
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
-
- Pro Scripter
- Posts: 68
- Joined: Wed May 04, 2005 10:24 pm
-
- Pro Scripter
- Posts: 68
- Joined: Wed May 04, 2005 10:24 pm
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?
-
- Pro Scripter
- Posts: 68
- Joined: Wed May 04, 2005 10:24 pm
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
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
-
- Pro Scripter
- Posts: 68
- Joined: Wed May 04, 2005 10:24 pm
But why do you have to go line-line??
I would do it like:
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:
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:
Good luck!
Olga.
I would do it like:
Code: Select all
ReadFile>YourFilePath,FileText
RPL>FileText,CRLF,%CRLF%%YourString%,FileText
DeleteFile>YourFilePath
WriteLn>YourFilePath,wresult,%YourString%%FileText%
Code: Select all
Sub RemoveTrailingSpaces(newStr)
newStr = Trim( newStr )
newStr = RegExpReplace( "^(\r|\n)+", newStr, "" )
GlobalReturnStr = RegExpReplace( "(\r|\n)+$", newStr, "" )
End Sub
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
Olga.
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
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>.OlgaFB wrote:But why do you have to go line-line??
I would do it like:
Good luck!Code: Select all
ReadFile>YourFilePath,FileText RPL>FileText,CRLF,%CRLF%%YourString%,FileText DeleteFile>YourFilePath WriteLn>YourFilePath,wresult,%YourString%%FileText%
Olga.