Add a line of content into .txt file.

Add a line of content into .txt file.

nguyenthinhvt95
Advocate Advocate
848 Views
2 Replies
Message 1 of 3

Add a line of content into .txt file.

nguyenthinhvt95
Advocate
Advocate

I have a problem need your help:

I create a txt file to count how many file export per month to report to boss.

the structure of content line in .txt file (show in the picture):

"Number count" , "Path project" , "extra infomation".

 

At the moment I use foreach funtions to reach all line of .txt file and then use token by "," to get the final number.

It working well but it take really a long time to reach all line of .txt.

Is there any function that allows adding a new line of content to a TXT file, with the requirement that the added line must be on a new line compared to the existing content in the TXT?"

 

 

// DIALOGS MESSAGE OFF
// DIALOGS ERROR OFF
INT old_time = time()

STRING Link_txt = "A:\CAM\Macro\6.CSharp\5_All_in_one\Control\OpenPowermillFileData.txt"

STRING LIST $TXT_Data = {}
STRING LIST $TK_temp = {}

FILE OPEN $Link_txt FOR READ AS input
FILE READ $TXT_Data FROM input
FILE CLOSE input

 

int $OpenCode = 0
int Check_Variable =0

//Tao 1 noi dung moi tu file ban dau. Xoa khoang trang va nap vao bien text
string text = "Total line: " + $size(TXT_Data)
int dem = 1


CREATE PATTERN ;
ACTIVATE Pattern #

 


Foreach item In TXT_Data {
//Xóa khoảng trắng
if item != "" and $dem >= 2 {
$TK_temp = Tokens (item , ',')
// lay gia tri lon nhat
$Check_Variable = INT($TK_temp[0])
if OpenCode <= $Check_Variable {
$OpenCode = $Check_Variable
}
$Text = $Text + crlf + $item
}


$dem = $dem + 1
string PatternNameOLD= $entity('pattern', '').name
string PatternNameNEW= "Read line: " + $dem

RENAME Pattern $PatternNameOLD $PatternNameNEW
}

DELETE Pattern $entity('pattern', '').name
//===================Check the last number and + 1======================================
$OpenCode = $OpenCode + 1

//===================Gom chuoi cu va moi thanh 1 file vao dien vao file txt===================


//====================Date time :HH/MM/SS dd/mm/yy====================================================
INT tm=time()
STRING ARRAY $timestamp[] = tokens(utc_time($tm).timestamp, "-")
string hour = local_time(tm).hour
string date_full = $local_time(tm).hour + ":" + $local_time(tm).minute + "," + $timestamp[2] + "/" + $timestamp[1] + "/" + $timestamp[0] + "," + USER_ID()
//====================Date time====================================================

 

String value = $OpenCode + "," + $project_pathname(0) + "," + date_full
$Text = $Text + crlf + $value
//=====Write file again with updated=======
FILE OPEN $Link_txt FOR WRITE AS out
FILE WRITE $text to out
FILE CLOSE out

EDIT PAR 'project.Notes.' ""
EDIT PAR 'project.Notes.' $OpenCode

 

//=========================check total line and keep line the line ==============================

int SizeOfFile = size(TXT_Data)

 

//=========================input the number of line want to keep ==============================
int LineKeepNumber = 300
//=========================input the number of line want to keep ==============================

 

int Startline = SizeOfFile - LineKeepNumber
if SizeOfFile > LineKeepNumber {

FILE OPEN $Link_txt FOR READ AS input
FILE READ $TXT_Data FROM input
FILE CLOSE input

$text = "Total line: " + $size(TXT_Data)
int dem_line = 1

Foreach item In TXT_Data {
if $dem_line > Startline {
$Text = $Text + crlf + $item
}
$dem_line = $dem_line + 1
}
//=====Write file again with updated=======
FILE OPEN $Link_txt FOR WRITE AS out
FILE WRITE $text to out
FILE CLOSE out


}

INT cumulative_time = time() - old_time
STRING msg = "Open code process took " + (cumulative_time) + "secs"
print $msg

DIALOGS MESSAGE ON
DIALOGS ERROR ON

 

 

 

 

 

0 Likes
Accepted solutions (1)
849 Views
2 Replies
Replies (2)
Message 2 of 3

icse
Advisor
Advisor

if oyu whant to append text to a file you use this:

 

string $filepath = "C:\test.txt"
string $txttoappend = "This should be added to the file as new line"

FILE OPEN $filepath FOR APPEND AS output
FILE WRITE $txttoappend TO output
FILE CLOSE output

 

if reading and handle entries in that file is to slow in the powermill macro language consider using a simple C# application.

 

Message 3 of 3

nguyenthinhvt95
Advocate
Advocate
Accepted solution

Hi @icse 

You are right, I use method cmd to create a new .txt file and then I append this to the main txt file. This really fast.

Thank you for a suggestion.

0 Likes