Re: How do I delete a folder through code?



In that case you should try to provide the actual code you have problems with.
We're not telepathic.

I did!


Why use dangerous and inefficient memset when you can just do (assuming C++)

SHFILEOPSTRUCTW info = {};

Habit. But yours is a better way of doing this.



TCHAR szBuffer[2048];
memset(szBuffer, 0, sizeof(szBuffer));

See earlier comment about memset.

Normally I do:

TCHAR szBuffer[2048] = {_T("")};

This situation is different. Here I need to do more than create a
character array with a single leading null terminator. With this
SHFileOperation function the string field is designed to handle a
series of NULL terminated strings. The final string ends with a
double terminator. An easy way of doing this is to fill the entire
array with zeros up front and then I'm guaranteed to have a full
string.




_tcscpy(szBuffer, _T("\\\\?\\"));

I'm not sure, but I think this prefix works only for Unicode.

This folder contains a long chain of subfolders that exceeds Window's
MAX_PATH character path length. These folders cannot be deleted in
Windows Explorer due to this situation. Since "\\?\" is how you use
the API to access locations beyond this limit I've added it here.



_tcscat(szBuffer, (LPCTSTR)strFolderToDelete);
This is very dangerous: you can easily exceed the buffer size.

Not here. I originally get the name of the folder from a Windows
Common Control, and since Windows has a MAX_PATH (260) character limit
I can never exceed my 2048 character limit. I admit that I
accidentally mixed TCHARs and WCHAR functions, but even then the array
cannot be exceeded.

Plus, this is a little test app. I just need to kill some directories
ASAP and don't really need anything sophisticated and entirely safe.
That's why I wasn't careful with the wide/single byte string mismatch
(multibyte is a terribly misleading name) and it's why I used ugly
hardcoded numbers. This was just a 5 minute task that turned into an
exercise in frustration due to an API that doesn't adequately support
the very basics of interaction with the filesystem (Hell, Windows
doesn't even have a notification to let you know when an flash card
volume is inserted into a reader.)



Anyway, have you checked that te resulting string makes sense as a path?

Yes.
Comes out to something like: "\\?\c:\Folder\Alpha\Whatever"



info.pFrom = szBuffer;
if (0 != SHFileOperationW(&info))

And... What's the actual code? So far you've only set up some buffers.

That's it. SHFileOperationW is the call that's supposed to delete the
folder. It's part of the Windows API. You initialize the structure
then pass it into that function. It fails and that's the bug.




Assuming this is all the code you're using to delete folder, that's the reason
it's not deleted. You need to isse some function call that deletes the folder.
For example, a call to SHFileOperation().

Um... *scratches head*
That's what I did and you questioned for the location of the actual
code.



- Alf

BTW, I loved your TV show.
.



Relevant Pages

  • Re: Event Log Files
    ... On the Windows menu, click HKEY_LOCAL_ MACHINE on Local Machine. ... Type the new drive and path in the String box, ... any help in this regard will be greatly appreciated. ...
    (microsoft.public.win2000.security)
  • Re: Other version information
    ... header to locate the resource table. ... Windows PE files ... marked by a literal string. ... files in a folder, ...
    (microsoft.public.scripting.vbscript)
  • Re: just to clarify ...
    ... Generally it's correct, but if for some reason the call fails, Ret will be 0 ... string if the call failed. ... For example purposes I used small Access dbase located in System32 folder. ... Will it work on any Windows based systems? ...
    (microsoft.public.vb.general.discussion)
  • Re: locate a file using vba - I am stack
    ... why not use the standard Windows File dialog (see ... >C:\0Templates folder when the user click the button? ... >>> Dim strPath As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: recover compacted folders
    ... Because of this tendency of attachments to infect, ... , then wasted space is generated in that file, and the folder must be ... Windows 2000 and Windows XP. ... Windows XP Service Pack 2 Resources for IT Professionals ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)

Quantcast