Re: How do I delete a folder through code?



See below...
On Wed, 7 May 2008 15:14:52 -0700 (PDT), scs0 <scs0@xxxxxxx> wrote:

First of all, this code is going into a test app that will have no
shelf life. It doesn't need to be robust. It just needs to be able
to delete a folder ASAP.


Second: The TCHAR / WCHAR mismatch. I didn't catch that when I
pieced this functionality together. Yea, this is bad because I am
potentially mixing basic string types and will even correct this for
test app code. BTW: There's nothing wrong with the TCHAR/_T
convention except for the situation where people forget about it (like
I had done)


Third: Why don't I provide a piece of sample code? I did!


Forth: Why am I not using CStrings? Because the SHFileOperation
structure wants a character array. Somewhere along the line I'm going
to have to convert a CString into a character array pointer. Since I
need one with a double NULL terminator sequence this was the easiest
way.
****
What, exactly, did I miss here. What "conversion"? GetBuffer() works fine, and does not
need any "conversion". To get the double-NUL termination (that's NUL; NULL is a pointer
value, NUL is the name of the character whose value is 0, check any book that talks about
character sets) you would do

CString s;
s = ... something;
int len = s.GetLength();
LPTSTR p = s.GetBuffer(len+2);
p[len+1] = _T('\0');
....do stuff
s.ReleaseBuffer();

Note that no "conversion" (implied copying) is required, so there is no need to allocated
fixed-size buffers or do other ugly things.

Note that if you are going to insist on the \\?\ notation, you would use a CStringW
instead.

CStringW s;
s = ...something Unicode
int len = s.GetLength();
LPWSTR p = s.GetBuffer(len + 2);
p[len+1] = L"\0";
....do stuff
s.ReleaseBuffer();


****


Fifth: Why am I prepending "\\?\" to my string? Because these are
paths that exceed the 260 "MAX_PATH" imposed by Windows but are well
below the maximum length imposed by the NTFS filesystem (which is
~32000 characters)
****
But key here: does the SHFileOperation support this? There is no documented restriction,
but in the last few months I've found over 100 documentation errors, mostly sins of
omission (and if you discover it fails, please let me know so I can add this to the list).
By the way, NEVER say "fails" unless you are prepared to give the details of what failure
actually means, such as specific error codes which are returned.
****


Sixth: Why am I using Hungarian notations? I like readable code. I
like looking at a variable and knowing immediately if it's a member
variable, a function parameter, or a local variable. I like being
able to get a general idea of what kind of variable it is as well. I
know it's trendy among programming elitists to write unreadable code
for job security, but I prefer to take a more professional approach
that encourages others to be able to follow code. I've seen all sorts
of terrible formatting like placing { brackets after the rightmost
character instead of placing it on its own line, excessive use of the
return character within a single call to a function (Monitors ain't 40
characters across anymore people), single-character variable names,
failing to use {} around single statement "if"/"else"blocks, and
spaghetti-code goto statements *shudder*. Even if someone doesn't
like adding a descriptor to the beginning of a variable, I can't see
how that's worse than those other things I mentioned.
****
I like readable code too, which is why I don't use HN. I consider HN an artifact of the
days when we wrote K&R C using NotePad, and in modern programming environments it has
little value. The compiler will catch stupid errors like assigning a string value to an
integer, calling a function with the wrong argument type, and so on, and there is little
value in creating redundant and error-prone names that require massive editing if the type
changes (take a look at how many errors exist in the Platform SDK, such as LPVOID
lpstrAlgorithm, WPARAM wParam (it ain't a 16-bit unsigned value) and so on)

It is common practice to put one parameter per line on complex APIs; it makes the code
easier to read. The argument against vertical programming also seems to be rooted in the
belief that monitors can only display 22 lines of code (allow two lines for status bars
and command input lines) and that scrolling is a complex operation.

But allocating a fixed-size 2048 byte character array? Using dangerous and obsolete
functions like _tcscpy and _tcscat? Now *that's* poor programming! At least gotos, weird
punctuation styles, and HN don't tend to generate headlines of the form "Flaw in program
allows malware attack of half-million computers in first hour". But buffer overruns can.
Even for throwaway code, it is poor style. Don't rant about harmless programming styles
when you commit one of the most horrendous sins against reliable programming that can be
done.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Syntax, expressiveness and the beauty of Tcl
    ... since it is only one character. ... when you are used to other programming languages. ... But if we take the discussion serious, my oppinion is to stick with real Tcl ...
    (comp.lang.tcl)
  • Re: Literature authors with similar styles
    ... Those are character names, a few of thousands of famous character ... >From a programming point of view, ... Her ability to cite hard data such as science ...
    (alt.usage.english)
  • Re: Online C Programming Quizzes
    ... Business, Math to programming languages. ... For C language, I have 3 set of quizzes that anyone is welcome to try ... assignment expressions have a value too... ... the null character is *still* ...
    (comp.lang.c)
  • Re: backslash-issues
    ... As i did not have my 'The F programming language' book at hand ... charis of type character and length one. ... in position i in the ASCII collating sequence. ... But unfortunately, if i compile this as being Fortran95, i still cannot ...
    (comp.lang.fortran)
  • Re: CString and UTF-8
    ... How can it find a multibyte character? ... CString are not that great. ... There are API/members that are locale aware (including MBCS, ...
    (microsoft.public.vc.mfc)