A folder is not deleted if it contains unclosed files - page 7

 

Tomorrow I'll try to make a script that fully reproduces this problem.

So people won't say I'm doing "fiction" here).

Thank you all so much for your willingness to help and assistance in solving it. ))

 

There was a floating error like this (the sequence is described and a library .ex5 file was attached). It is difficult to document this kind of thing

Forum on trading, automated trading systems and strategy testing

Bugs, bugs, questions

A100, 2016.08.04 00:05

Error opening the file by the Expert Advisor. What if for example.

1. Remove "some irrelevant code" (see below)

2. or remove ::Sleep(1000)

there is no error

//Test.mq5
#import "Test001.ex5"
        string f( string file );
#import
string __Period() { return StringSubstr( EnumToString((ENUM_TIMEFRAMES)Period()), 7 ); }
void OnInit()
{
        Print( Symbol(), ",", __Period(), ", результат=", f( "test.txt" ));
}

Sequence of actions:

1. Create a new profile (e.g. "Test")

2. open two new charts attaching the Test.ex5 Expert Advisor to both of them (see picture)

4. Select any other existing profile

5. Select "Test" profile

result:

While Test001.mq5 looks like this:

//Test001.mq5
#property library
//---
//здесь некий код не имеющий отношения к делу
//---
string f( string file ) export
{
        ::ResetLastError();
        int hFile = ::FileOpen( file, FILE_READ | FILE_SHARE_READ | FILE_TXT | FILE_ANSI | FILE_COMMON );
        if ( hFile == INVALID_HANDLE )
                return ::StringFormat( "Error(%d): Не был открыт файл: %s", ::GetLastError(), file );
        ::Sleep( 1000 );
        ::FileClose( hFile );
        return "OK";
}

Tried now with the library file attached there: result is the same (the test file is opened separately by both EAs, and if simultaneously, one of the EAs reports an error)
2017.08.28 23:40:43.623 Test (EURUSD,M15) EURUSD,M15, result=OK
2017.08.28 23:40:52.389 Test (GBPUSD,M15) GBPUSD,M15, result=OK

2017.08.28 23:41:14.387 Test (GBPUSD,M15) GBPUSD,M15, result=Error(5004): File was not opened: test.txt
2017.08.28 23:41:15.484 Test (EURUSD,M15) EURUSD,M15, result=OK


 

Found application number #1530548, but no comments - may have already been corrected. If not, please review again

 

Реter Konow:

The file is opened by my application (file navigator) to be overwritten in another folder.

When the file is opened for reading , we get the file handle.

The tag is Konow:

After overwriting I need to close the file but I can't - (no handle) and just erase the file.

To write to another file, I need the handle of the other file, so it makes sense to store it in another variable. After writing to a new file, the handles of the two files are stored in two variables, and both files are closed using those handles.

Tag Konow:

There is no handle because the file was created unknown when and unknown by whom.

If the file was created by John Doe and John Doe, it's VERY likely that it's not used by any application. If you open the file for some purpose, then you already have a handle obtained by the FileOpen function and you should close it before erasing the folder.

Konow tag:

That said, the file exists within the Files folder and can be copied to another folder and erased afterwards.

When copying a file from one accessible program to an mql program, the file's handle is absolutely unnecessary.

Reg Konow:

But you cannot close that file after you have copied it. There is no handle.

After copying the file, the file is not open and there is no need to close it.

Konow tag:

This is probably why further erasing FolderClean() with erased files, doesn't work.

This is an absolute mistake.

RetagKonow:

Question: How can I get a handle to this file in an MQL program?

From the above, this question doesn't make sense. File handle is only obtained when opening a file to determine where the file pointer is FileTell and FileSeek from where to read or write to, to force dump data to disk FileFlush and of course to close the file to release it for other applications to work on it.


Quite by accident I discovered what your problem is:

During an experiment, a folder is open in Explorer or another file manager to monitor the creation and deletion of a file. And once the file is deleted, the folder remains busy and cannot be deleted. The result is

2017.08.29 09:42:15.657 Demo_FolderDelete (CADJPY,H4)   Не удалось удалить папку nonempty. Код ошибки=5024

But the fact that after an unsuccessful attempt to delete a folder, this folder remains busy, alas, is a fact. This will now be the subject of an application to the SD.

ps; Alas, I also messed up during the experiment. After writing to the file, reset the data to disk, tried to open it with notepad, but got in response ...(figure) to cancel the attempt to open forgot and that's what kept the folder and did not let it be deleted.

Summary: No problems with mql files.

 

Alexey Viktorov:

Completely by accident, I have discovered what your problem is:

During an experiment, a folder is open in Explorer or another file manager to monitor the creation and deletion of a file. And after deleting the file, the folder remains occupied and cannot be deleted. The result is

2017.08.29 09:42:15.657 Demo_FolderDelete (CADJPY,H4)   Не удалось удалить папку nonempty. Код ошибки=5024

But the fact that after an unsuccessful attempt to delete a folder, this folder remains busy, alas, is a fact. This will now be reported to the SR.

I've come across this behaviour under completely different circumstances - also keeping a folder open for deletion, resulting in programs/scripts/SVN etc failing to delete that folder.

And the cure is always the same - manually tearing down that folder. So it's hardly a terminal or editor problem.

 
Rashid Umarov:

I've come across this behaviour under completely different circumstances - also keeping a folder open for deletion, with the result that programs/scripts/SVN etc couldn't delete the folder.

And it's always treated the same way - by manually tearing down the given folder. So it's hardly a terminal or editor problem.

I supplemented my post a bit later.

Forum on trading, automated trading systems and testing trading strategies

Not deleting a folder if there are unclosed files in it

Alexey Viktorov, 2017.08.29 08:55


ps; Alas, I messed up during the experiment too. After writing to file, reset data to disk, tried to open it with notepad, but got in response ...(figure) to cancel opening attempt I forgot and that's what kept folder and didn't let it be deleted.

Summary: No problem with mql files.


Strange that you quoted my post without this addendum.

 
Alexey Viktorov:

I supplemented my post a little later.


Strange that you quoted my post without that addition.

Just so you are prepared for the SD's reply.

 
Rashid Umarov:

Just so you are prepared for the SD's response.

So if I understood the reason for the problem, why write crap from the SD? I just didn't say out loud that I refused to write it... I figured it was obvious to everyone.
 
Rashid Umarov:

I've encountered this behaviour under completely different circumstances - also keeping a folder open for deletion, resulting in programs/scripts/SVN etc not being able to delete the folder.

And the cure is always the same - manually tearing down that folder. So it's hardly a terminal or editor problem.

And the fact of opening a folder in a file manager, NOT a FILE, does not affect its removal by mql.

 
Alexey Viktorov:

1. While opening a file for reading , get the file handle.

2. To write to another file we need another file handle, so it is reasonable to store it in another variable. After writing to a new file, the handles of the two files are stored in two variables, and both files are closed according to these handles.

3. if the file was created unknown when and by unknown person, there is a VERY good chance that the file is not occupied by any application. If you are opening the file for some purpose, you already have a FileOpen handle and should close it before clearing the folder.

4. When copying a file from one available program to the mql, the file handle is absolutely unnecessary.

5. Once a file is copied, that file is not open and there is no need to close it.


1 The handle is only needed for the FileClose function. I do not open the file. It was created before me. I only copy it and erase it. I can't close it because there is no handle.

2. To copy (FileCopy), to move (FileMove), the file handle is not needed. Please refer to the documentation.

3. If I open a file created by someone else using FileOpen function, I will not get its handle. It is not known at all which handle I will get. It may be his or may be quite a different one. I tried to get the handle of someone else's file so I could close FileClose it after the copy operation. It didn't work.

4. When copying, the handle is not needed. It is copied without any problems. But the file is automatically opened for copying. Further, I can't close it with FileClose because there is no handle. This is probably why FolderClean does not work after erasing the copied files.

5. How do you know the file is not open after copying? It was not explicitly opened by the FileOpen function, but it was being copied with, which means it is open. Hence it remains open after copying and even after erasing. Probably.

Reason: