Getting ShFileOperation window APIs for recursively deleting files in Delphi

I use the following code to delete a large number of files

function FastDelete(const fromDir: string): Boolean; var fos: TSHFileOpStruct; begin ZeroMemory(@fos, SizeOf(fos)); with fos do begin wFunc := FO_DELETE; fFlags := FOF_FILESONLY or FOF_NOCONFIRMATION or FOF_NO_CONNECTED_ELEMENTS or FOF_NOERRORUI or FOF_NO_UI; pFrom := PChar(fromDir+'\*.*' + #0); end; Result := (0 = ShFileOperation(fos)); end; 

How do I get to recursively delete all files in a path?

MSDN Documentation

EDIT

The problem is FOF_FILESONLY After deletion, files are recursively deleted.

+1
source share
2 answers

From the MSDN Documentation :

FOF_NORECURSION

Perform the operation only in the local directory. Do not recursively work in subdirectories , which is the default behavior .

It seems your answer is right there. It should automatically return unless you ask him not to.

EDIT: There seems to be a problem with your flags. You need OR them together, not add them together. Since FOF_NO_UI already includes FOF_NOERRORUI , adding it again can change the value, and you can accidentally add some things together to add FOF_NORECURSION . It should look like this:

  fFlags := FOF_FILESONLY or FOF_NOCONFIRMATION or FOF_NO_CONNECTED_ELEMENTS or FOF_NOERRORUI or FOF_NO_UI; 
+4
source

Do you also need to save the directory? If not, you can just go through

 pFrom := PChar(fromDir+#0); 

Another option is to create a list of file paths with delimiters # 0 and pass this with extra # 0, from msdn :

Although this member is declared as a string with a null character, it is used as a buffer for storing multiple file names. Each file name must be interrupted by a single NULL character. An optional NULL character must be added at the end of the final name to indicate the end of pFrom.

+1
source

Source: https://habr.com/ru/post/902030/


All Articles