Creating a folder with the WinAPI function CreateDirectoryW fails

 

Hello guys,

I want to create a folder with the help of mql5.

Can someone tell me why this script doesn't work?

#import "kernel32.dll"
   int CreateDirectoryW(string path, int atrr[]);
#import

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
   int empty[];
   if (CreateDirectoryW("D:\\NEW_FOLDER",empty)==0) {
      Print(GetLastError());
   }
}
 

Code:

//+------------------------------------------------------------------+
//|                                                   CreateFile.mq5 |
//|                         Copyright © 2019-2022, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019-2022, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.001"
#property script_show_inputs
#include <WinAPI\fileapi.mqh>
//--- input parameters
input string   InpDirectory="E:\\MT5_Export_History\\";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string path_name=InpDirectory;
   PVOID security_attributes=0;

   int result=CreateDirectoryW(path_name,security_attributes);

   HANDLE handle=-1;
   string file_name=Symbol()+".csv";

   uint desired_access=0;
   
//+------------------------------------------------------------------+
//| share_mode                                                       |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew&nbsp;                              |
//|  0x00000000 Prevents other processes from opening a file or device if they request delete, read, or write access |
//|  0x00000004 FILE_SHARE_DELETE Enables subsequent open operations on a file or device to request delete access    |
//+------------------------------------------------------------------+
   uint share_mode=0x00000004;
   
//+------------------------------------------------------------------+
//| security_attributes                                              |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew&nbsp;                              |
//|   This parameter can be NULL                                     |
//+------------------------------------------------------------------+
   security_attributes=0;
   
//+------------------------------------------------------------------+
//| creation_disposition                                             |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew&nbsp;                              |
//|  2 CREATE_ALWAYS                                                 |
//+------------------------------------------------------------------+   
   uint creation_disposition=2;
   
//+------------------------------------------------------------------+
//| flags_and_attributes                                             |
//|  https://docs.microsoft.com/ru-ru/windows/win32/api/fileapi/nf-fileapi-createfilew&nbsp;                              |
//|  128 (0x80) FILE_ATTRIBUTE_NORMAL                                |
//+------------------------------------------------------------------+
   uint flags_and_attributes=0x80;
   HANDLE template_file=0;

   handle=CreateFileW(path_name+"\\"+file_name,desired_access,share_mode,security_attributes,creation_disposition,flags_and_attributes,template_file);
   if(handle!=INVALID_HANDLE)
      FileClose(handle);
   int d=0;
  }
//+------------------------------------------------------------------+
Files:
 
Vladimir Karputov #:

Code:

Thank you very much, Vladimir!

Your code is pretty complex for me and I need to understand fileapi.mqh.

Could you please tell me what the problem with my code is? I just want to understand it.

Thank you!

 
Marbo # :

Thank you very much, Vladimir!

Your code is pretty complex for me and I need to understand fileapi.mqh.

Could you please tell me what the problem with my code is? I just want to understand it.

Thank you!

'#include <WinAPI\fileapi.mqh>' is a special file that comes with the standard distribution of the terminal. I tried to comment on the code as much as possible. Read comments - explanations are taken from WinAPI help.

 
Vladimir Karputov #:

'#include <WinAPI\fileapi.mqh>' is a special file that comes with the standard distribution of the terminal. I tried to comment on the code as much as possible. Read comments - explanations are taken from WinAPI help.

Got it! Thanks a lot!