FolderClean() function - how to delete all files in root directory of Files folder ?

To add comments, please log in or register
Marcin Rutkowski
317
Marcin Rutkowski  

In my software I create a lot of files (logging purposes) and before it was not very big deal I deleted them manually but now I create even more and I 'd like to delete them  on the beginning of the OnInit()   :)

I found the function  FolderClean and a lot of demo codes to delete files in existing folders but my tries to  delete all files in root (file folder where unsuccessful) ...It is not super urgent and I can still continue to delete them manually but if somebody knows a way how to do it and have 2 min spare for reply it  would be greatly appreciated.

I read the documentation and look in google for a while but with no luck, I was hoping that this will work like FolderClean(""); or FolderClean("\\"); but no luck  :)


Regards and Thank YOU

Alain Verleyen
38509
Alain Verleyen  
Show your code if you need coding help. And describe precisely what is "unsuccessful".
Marcin Rutkowski
317
Marcin Rutkowski  
Alain Verleyen:
Show your code if you need coding help. And describe precisely what is "unsuccessful".
int OnInit()
  {
   EventSetMillisecondTimer(200);
   ObjectsDeleteAll(0);

// ...
// test
   Print((string)FolderClean(""));
   Print((string)FolderClean("\\"));

// ...

   Print("FINISHED !!!");

   return(INIT_SUCCEEDED);
  }

In expert tab is   false false    ....I know this is probably simple mistake but now I have no ideas what else I can do  :)

Thank You for reply

PS. gol is to delete everything  regardles of the extension  but i use only .txt if this meananything

Alain Verleyen
38509
Alain Verleyen  
Marcin Rutkowski:

In expert tab is   false false    ....I know this is probably simple mistake but now I have no ideas what else I can do  :)

Thank You for rebpy

You have to provide the full code.
Marcin Rutkowski
317
Marcin Rutkowski  
Alain Verleyen:
You have to provide the full code.

//    ╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
//    ║                                     F U N C T I O N S     B O D I E S                                        ║
//    ╠═══════════════════════════════╦══════════════════════════════════════════════════════════════════════════════╝
//    ║  PUBLIC                       ║
//    ╚═══════════════════════════════╝


bool funFilePrint;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetMillisecondTimer(200);
   EventSetTimer(1);
   ObjectsDeleteAll(0);
   MqlDateTime time;

   TimeToStruct(TimeLocal(),time);

   string randomString=(string)time.hour+(string)time.min+(string)time.sec;

   string FileName="FileName"+randomString+".txt";

   Print(FileName);
   funFilePrint("text",FileName);

// ...

 ;// if we uncoment below, then we will have no files :)

//Print("is file deleted  "+(string)FileDelete(FileName));      //  <<<<<<<<<<<    but this works ???

   ;               //

   Print("are files deleted  "+(string)FolderClean(""));
   Print("are files deleted  "+(string)FolderClean("\\"));
   Print("are files deleted  "+(string)FolderClean("",0));
   Print("are files deleted  "+(string)FolderClean("\\",1));

// but files are still in the folder :)
// after each compilation :)
// ...

   Print("FINISHED !!!");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

bool funFilePrint(string locMessage="",string locFileNameW="metFilePrintLogFile.txt",bool parEnter=true,string locSeparator=";")
  {

   bool locFunctionTest=false;
   int handle=FileOpen(locFileNameW,FILE_CSV|FILE_READ|FILE_WRITE,locSeparator);// File opening
   FileSeek(handle,0,SEEK_END);

   if(parEnter==true)
      locMessage=locMessage+"\n";
   uint saved=FileWriteString(handle,locMessage);
   if(saved>0)
      locFunctionTest=true;
   FileClose(handle);
   return(locFunctionTest);
  }
//+------------------------------------------------------------------+

I prepared simple code  ...if we  compile multiple times, multiple files are created, but non of them are deleted

Im using this function for debugging , saving messages to  the file instead of Expert tab


but now the code now is just this literary :)

-no other ea are working

-  the files are previously created

- even I restart the mt5  (but always every file handle is closed)

Just not sure what to put as a   folder name ??


PS ..if we uncomment delete Function  ...files are automatically deleted, and this is ok

Alain Verleyen
38509
Alain Verleyen  
   Print("are files deleted  "+(string)FolderClean(".\\"));
Marcin Rutkowski
317
Marcin Rutkowski  
Alain Verleyen:
You have to provide the full code.

I start digging because i feel guilty, always feel this way if I asking for something obvious and I found the answer  ....

folder name has to be "//"         NOT "\\"  :)  ()                                 

why i didn't it   ..i do not have the answer for this question :), and I do  always  mix them

sorry for wasted time again :)


"//" :)

PS

error 5021        ERR_WRONG_DIRECTORYNAME

Marcin Rutkowski
317
Marcin Rutkowski  
Alain Verleyen:

Thank You 

..bit confusing    ...tested both options and    a and b works    ...now I'm more confused why   .\\ and    // works     are they for difrent purpuses ??

Regardles   Awesom  ...I asume You is proper :)

Alain Verleyen
38509
Alain Verleyen  
Marcin Rutkowski:

Thank You 

..bit confusing    ...tested both options and    a and b works    ...now I'm more confused why   .\\ and    // works     are they for difrent purpuses ??

Regardles   Awesom  ...I asume You is proper :)

Yes "//" works too, no idea why though

Actually as '/' is a normal character you don't have to double it, so :

FolderClean("/")

is working too :-D

I am suggesting you to stick to Windows old convention, so use "./" which in mql5 need to be written as ".//".

Get directory separator char on Windows? ('\', '/', etc.)
Get directory separator char on Windows? ('\', '/', etc.)
  • 2011.09.06
  • Mehrdad
  • stackoverflow.com
tl;dr: How do I ask Windows what the current directory separator character on the system is?
Marcin Rutkowski
317
Marcin Rutkowski  
Alain Verleyen:

Yes "//" works too, no idea why though

Actually as '/' is a normal character you don't have to double it, so :

is working too :-D

I am suggesting you to stick to Windows old convention, so use "./" which in mql5 need to be written as ".//".

Thank You :)

To add comments, please log in or register