File mask with Session.RemoveFiles

Advertisement

chrislong2
Joined:
Posts:
19
Location:
USA

File mask with Session.RemoveFiles

Hi Martin,

Something I don't understand:

If I call Session.RemoveFiles with a path like "/myfolder/*.txt"
then it will remove only *.txt files in that folder only (not subfolders). As expected.

Yet if I call with a path like "/myfolder/*.*" then it removes not only all files in that folder, but removes all subfolders too, the same as using "/myfolder/*"

Why is that? How can I remove only all the files in the specified folder without touching the subfolders? What am I missing here?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,440
Location:
Prague, Czechia

Re: File mask with Session.RemoveFiles

The Session.RemoveFiles removes all files and subfolders matching the mask.

You most probably have no subfolder with ".txt" extension.

For any other custom matching, you need to iterate the files yourself (use Session.ListDirectory), calling Session.RemoveFiles individually for each file you want to delete.

Reply with quote

chrislong2
Joined:
Posts:
19
Location:
USA

Re: File mask with Session.RemoveFiles

martin wrote:

The Session.RemoveFiles removes all files and subfolders matching the mask.

You most probably have no subfolder with ".txt" extension.

For any other custom matching, you need to iterate the files yourself (use Session.ListDirectory), calling Session.RemoveFiles individually for each file you want to delete.

This makes no sense to me as 20-year developer with software that itself asks the user for extensions for copying/deleting much as yours does.

If I call Session.RemoveFiles("/myfolder/*.txt")
but I have a file: "/myfolder/subfolder/1.txt"

That 1.txt file is NOT deleted because it is in a subfolder.

Yet if I call Session.RemoveFiles("/myfolder/*.*") then that 1.txt file is deleted (along with every other file regardless of subfolder) as well as all subfolders themselves.

This is inconsistent behavior. It needs to be one way or the other. Either RemoveFiles should parse through all subfolders or it shouldn't. Personally the way I assumed it worked is the way that 7-Zip and other programs handle extensions in that:

\*.txt deletes only .txt files in that exact folder
\*.* deletes only files in that exact folder
\* deletes all files including all subfolders

Or just providing a Boolean recurse option to the function to control this (7-Zip does that also).

What RemoveFiles does right now is a mismatch.

Reply with quote

martin
Site Admin
martin avatar

Re: File mask with Session.RemoveFiles

It works consistently with *nix rm -rf <mask> command, what I consider an industry standard.

Reply with quote

chrislong2
Joined:
Posts:
19
Location:
USA

Re: File mask with Session.RemoveFiles

martin wrote:

It works consistently with *nix rm -rf <mask> command, what I consider an industry standard.

Well, I haven't used unix/linux in a significant way in a long time, so I won't comment on that - you could be right. What I will say is that in my decades on Windows (& DOS before it) and as a Windows developer for 20+ years with several programs that do lots of filehandling, I've never seen that as a standard behavior. If you go to command prompt in Windows right now and type del C:\temp\*.*, it's not going to delete all subfolders too. It's going to delete only the files in that folder. Likewise with the Windows API filehandling functions.

What you are basically saying is there is no way with RemoveFiles function to remove only all files in a specific folder, but you have to delete all files in all folders/subfolders. Yet calling it with a specific extension will only delete the files in that specific folder UNLESS a subfolder happens to also have the extension (which would be VERY rare). Thus practically speaking, you end up being able to delete specific filetypes in a specific folder only, but not all files in a specific folder only. If that's a standard on Linux, it certainly shouldn't be - that is VERY odd behavior to me.

An option of recursion or not would be a helpful addition to this function (i.e. Session.RemoveFiles(<sMyPath>, <bRecurse>)

Finally, just a note Martin, since I also have the other issue posted too and I don't want to appear in posting these issues that I am ungrateful. I do thank you again for WinSCP and your efforts! :)

Specifically, the ScanFingerprint addition to the COM dll you added awhile back was a majorly important needed addition - thank you for adding that! :)

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum