Wer auch keine sinnlosen Kommentare wünscht, dem sei dieses Tool geschenkt.
Die Datei deren Kommentar entfernt werden soll muss in das Verzeichnis MQLx\Files\KillComment gestellt werden (Sandbox Restriktionen von MQ).
Sollte auch für den MT4 funktionieren.
Code:
//+------------------------------------------------------------------+
//| KillComment.mq5 |
//| Copyright © 2017 Ing. Otto Pauser |
//| https://www.mql5.com/de/users/kronenchakra |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017 Ing. Otto Pauser"
#property link "https://www.mql5.com/de/users/kronenchakra"
#property version "1.00"
#property description "Dieses Script entfernt alle Kommentare bis auf den Kopfkommentar am Anfang der Datei"
#property script_show_inputs
input string inp_fPath = "KillComment"; // Filepath
input string inp_fName = "FileName.mq5"; // Filename (IN/OUT)
input int inp_skip = 7; // Skip number of lines (header)
input bool inp_right = true; // Comment right // this will be deleted
input bool inp_left = true; // //--- Comment at beginning will be deleted
input bool inp_spacer = false; // //--- Delete spacer only
string fn = inp_fPath+"\\"+inp_fName; // filename
int fh; // filehandle
string lines[]; // lines array
int OnStart() // normally it's void, but to use MyAlert() it's int like OnInit()
{
string line; // single line
int count = 0, // lines counter
asize = 500, // arraysize of lines array
pos; // stringposition of "//"
ArrayResize(lines,asize); // set initial size of array, no initialization necessary
fh=FileOpen(fn,FILE_READ|FILE_SHARE_READ|FILE_TXT|FILE_ANSI);
if(fh==INVALID_HANDLE) // errorcheck
return(MyAlert(-1,"*ERROR* "+fn+" not found"));
while(!FileIsEnding(fh)) // scan the file
{
line=FileReadString(fh); // read the line
if(count>inp_skip-1) // number of lines skipped
{
pos=StringFind(line,"//"); // search the comments
if(pos>-1) // found at pos
if(pos==0) // comment at the beginning of line
{
if(inp_left) // option 'delete left' selected
line="";
}
else // comment beside code
{
if(inp_right) // option 'delets right' selected
line=StringSubstr(line,0,pos);
}
} // StringTrimRight() macht den string unbrauchbar
lines[count]=line; // store the line in array
count++; // increment counter
if(count>=ArraySize(lines)) // resize the array if necessary
ArrayResize(lines,ArraySize(lines)+asize);
}
FileClose(fh); // close the file
if(!FileDelete(fn)) // delete the file
return(MyAlert(-1,"*ERROR deleting file "+fn));
// create the file with same name
fh=FileOpen(fn,FILE_WRITE|FILE_SHARE_WRITE|FILE_TXT|FILE_ANSI);
if(fh==INVALID_HANDLE) // errorcheck
return(MyAlert(-1,"*ERROR* "+fn+" not found"));
FileWriteString(fh,lines[0]+"\n"); // write the first line
for(int i=1; i<count; i++) // write the lines back to file (skip double blank lines)
if((lines[i]!="") || (lines[i-1]!=""))
FileWriteString(fh,lines[i]+"\n");
FileClose(fh); // close the file
PlaySound("ok"); // make some noise
return(INIT_SUCCEEDED); // return value does not matter in this case
}
//+------------------------------------------------------------------+
//| This Alert() function is very useful also for Indicators and EAs |
//| You can use it inside return() to avoid brackets and more code |
//+------------------------------------------------------------------+
bool AlertPrintOnly = false; // controls the behavior of MyAlert()
int MyAlert(ENUM_INIT_RETCODE aRetVal, string ErrMsg="")
{
if(AlertPrintOnly)
{
Print(ErrMsg);
PlaySound("Alert");
}
else
Alert(ErrMsg); // Alert() itself plays the sound)
ResetLastError();
return(aRetVal);
}
/*
for ErrorCheck usually you need at least 6 lines of code
if(fh==INVALID_HANDLE)
{
Alert("*ERROR* "+fn+" not found");
ResetLastError(); // possibly unnecessary because we exit the program now
return(INIT_FAILED);
}
with this function you need only 2 lines of code
if(fh==INVALID_HANDLE) // errorcheck
return(MyAlert(-1,"*ERROR* "+fn+" not found"));
or particularly compact only one line, and the code remains easily readable
if(fh==INVALID_HANDLE) return(MyAlert(-1,"*ERROR* "+fn+" not found"));
*/
Es ist auch eine nette ErrorRoutine enthalten.
Dieses Script ist auch ein Beispiel zum Textdateien lesen und schreiben.
Auch das handling des Arrays 'lines[]' ist ein Beispiel um ein dynamisches Array effektiv, und nur bei Bedarf, zu vergrößern.
Hinweis: Die Ergebnisdatei überschreibt die Eingabedatei.
Grüße Otto