#import "shell32.dll"
 int ShellExecuteW(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd);
#import

void OnStart() { string line_read[], filename;           ArrayResize(line_read, 50, 0);

   int ts=ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
      "Versions_v_Zip_0.bat", "", "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1);

   Sleep(500);       int row=0, handle=FileOpen("comparer_fichiers_versions.txt", FILE_TXT|FILE_READ);
   if(handle > 0) { while(True) { string temp=FileReadString(handle);
         if (StringSubstr(temp, 0, 8) == "***** .\\" && temp != line_read[row-1]) {
            line_read[row] = temp;        row ++; }
         if (FileIsEnding(handle)) break; }
      FileClose(handle); }
   else { Alert("*** File not found, the last error is ", GetLastError() + " ***");       return; }
   FileDelete("comparer_fichiers_versions.txt");

   for (row = 0; row < 50 && StringLen(line_read[row]); row ++) { temp = StringSubstr(line_read[row], 6, StringLen(line_read[row]));
      ts = ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
         "Versions_v_Zip_1.bat", "\"" + temp + "\" 1", "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1);

      Sleep(150);       string tmp2=temp;
      handle = FileOpen((filename=StringSubstr(temp, StringFind(temp, "\\", StringFind(temp, "\\") + 1) + 1, StringLen(temp))), FILE_TXT|FILE_READ);
      if (handle > 0) { while(True) { temp = FileReadString(handle);
            copy_file(filename, temp);       if (FileIsEnding(handle)) break; }
         FileClose(handle); }
      else Alert("*** File " + filename + " not found, the last error is ", GetLastError() + " ***");

      Sleep(150);       FileDelete(filename, 0);         filename = StringSubstr(filename, 0, StringFind(filename, ".")) + ".txt";

      temp = StringSubstr(tmp2, StringFind(tmp2, "\\") + 1, StringFind(tmp2, "\\", StringFind(tmp2, "\\") + 1) - StringFind(tmp2, "\\") - 1);
      ts = ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
         "Versions_v_Zip_1.bat", "\"" + filename + "\" 2 " + temp, "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1);

      Sleep(150);       FileDelete(filename, 0);

      ts = ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
         "Versions_v_Zip_1.bat", "\"" + tmp2 + "\" 3", "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1);

      Sleep(150);       handle = FileOpen((filename=StringSubstr(tmp2, StringFind(tmp2, "\\", StringFind(tmp2, "\\") + 1) + 1, StringLen(tmp2))), FILE_TXT|FILE_READ);
      if (handle > 0) { while(True) { temp = FileReadString(handle);
            copy_file(filename, temp);       if (FileIsEnding(handle)) break; }
         FileClose(handle); }
      else Alert("*** File " + filename + " not found, the last error is ", GetLastError() + " ***");

      Sleep(150);       FileDelete(filename, 0);         filename = StringSubstr(filename, 0, StringFind(filename, ".")) + ".txt";

      temp = StringSubstr(tmp2, StringFind(tmp2, "\\") + 1, StringFind(tmp2, "\\", StringFind(tmp2, "\\") + 1) - StringFind(tmp2, "\\") - 1);
      ts = ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
         "Versions_v_Zip_1.bat", "\"" + filename + "\" 4 " + temp, "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1);

      Sleep(150);       FileDelete(filename, 0); }

   ts = ShellExecuteW(0,"open","C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4\\"+
      "Versions_v_Zip.bat", "", "C:\\Users\\Administrator\\AppData\\Roaming\\MetaQuotes\\Terminal\\E88DA660F9A20E5DD9C622B8430BA4F1\\MQL4", 1); }

int copy_file(string filename,string temp) { int handle, it=0;
   bool tst=false;         filename = StringSubstr(filename, 0, StringFind(filename, ".")) + ".txt";
   while (! tst && it < 7) { if (! FileIsExist(filename)) handle = FileOpen (filename, FILE_TXT|FILE_WRITE);
      else  handle = FileOpen (filename, FILE_TXT|FILE_READ|FILE_WRITE);
      if (handle != INVALID_HANDLE) { if (FileSize(handle)) FileSeek(handle, FileSize(handle), 0);
         tst = FileWrite(handle, temp);
         if (! tst) Print ("Problème : FileWrite(handle, ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***"); }
      else Print ("Problème : handle == INVALID_HANDLE. ("+it+" - "+IntegerToString(GetLastError())+") / "+Symbol()+" ***");
   FileClose(handle);      it ++; }
   return(handle); }
