list all files of a directory

 

hi!

i am trying to get through all files of a directory. i found this code, and wrote an EA with it, but it doesnt work.

This code should print all names of the files, but it doesnt. do you know why?

thanks for help

//+------------------------------------------------------------------+
//|                                                CheckFindFile.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
 
//#property show_inputs
 
#import "kernel32.dll"
int  FindFirstFileA(string path, int& answer[]);
bool FindNextFileA(int handle, int& answer[]);
bool FindClose(int handle);
//#import
 
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int init()
  {
//----
   int win32_DATA[79];
   
   string param = "C:\\analysis\\*.mq4";
   Print (param);
      
   
   int handle = FindFirstFileA( param ,win32_DATA);
   Print(bufferToString(win32_DATA));
   ArrayInitialize(win32_DATA,0);
 
   while (FindNextFileA(handle,win32_DATA))
      {
      Print(bufferToString(win32_DATA));
      ArrayInitialize(win32_DATA,0);
      }
 
   if (handle>0) FindClose(handle);
   
   ExpertRemove();
//----
   return(0);
  }
  
//------------------------------------
int deinit()
{
   return(0);
}

//------------------------------------
int start()
  {
//----

//----
   return(0);
  }  
  
//+------------------------------------------------------------------+
//|  read text from buffer                                           |
//+------------------------------------------------------------------+ 
string bufferToString(int buffer[])
   {
   string text="";
   
   int pos = 10;  
   for (int i=0; i<64; i++)
      {
      pos++;
      int curr = buffer[pos];
      text = text + CharToStr(curr & 0x000000FF)
         +CharToStr(curr >> 8 & 0x000000FF)
         +CharToStr(curr >> 16 & 0x000000FF)
         +CharToStr(curr >> 24 & 0x000000FF);
      }
   return (text);
   }  
//+------------------------------------------------------------------+
 
mallymally:

hi!

i am trying to get through all files of a directory. i found this code, and wrote an EA with it, but it doesnt work.

This code should print all names of the files, but it doesnt. do you know why?

thanks for help

Why did you want to upload the contents of the entire folder over FTP ?
 

hi marco, i dont need to transfer data via ftp any more. i found a way to use the data of the files in metatrader. i have a special analysis software who stores the result in its own folder, and i wanna use this data to read it into my metatrader and combine it with my analysis there to generate trades. you know what i mean? ;)

in the code above i would say the bufferToString() function doesnt work well. i am a bit confused.

 

You should use FindFirstFileW function.

Metatrader uses UTF-8 functions since build 600. 
 

I think that you have to use Windows ..W() functions of kernel32.dll since b600+

#import "kernel32.dll"
int  FindFirstFileW(string path, int& answer[]);
bool FindNextFileW(int handle, int& answer[]);
bool FindClose(int handle);

BTW: an error code or message would be helpful for those who want to help you!

 

Oh yeah, thats great! i played with it and it works:

#import "kernel32.dll"
int  FindFirstFileW(string path, int& answer[]);
bool FindNextFileW(int handle, int& answer[]);
bool FindClose(int handle);
//#import
 
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int init()
  {
//----
   int win32_DATA[79];
   
   string param= "C:\\analysis\\*.*";
   Print (param);
      
   int cnt = 0;
   int handle = FindFirstFileW( param ,win32_DATA);
   Print("file",cnt++, bufferToString(win32_DATA));
   ArrayInitialize(win32_DATA,0);
 
   while (FindNextFileW(handle,win32_DATA))
      {
      Print("file",cnt++, bufferToString(win32_DATA));
      ArrayInitialize(win32_DATA,0);
      }
 
   if (handle>0) FindClose(handle);
   
   ExpertRemove();
//----
   return(0);
  }
  
//------------------------------------
int deinit()
{
   return(0);
}

//------------------------------------
int start()
  {
//----

//----
   return(0);
  }  
  
//+------------------------------------------------------------------+
//|  read text from buffer                                           |
//+------------------------------------------------------------------+ 
string bufferToString(int buffer[])
   {
   string text="";
   
   int pos = 10;  
   for (int i=0; i<64; i++)
      {
      pos++;
      int curr = buffer[pos];
      text = text + CharToStr(curr & 0x000000FF)
         +CharToStr(curr >> 16 & 0x000000FF);
      }
   return (text);
   } 


i changed to the FindFirstFileW() and the bufferToString() function. now it works. thx!