Delete Routine

Environment: VC++, Win NT, Win 2000

This piece of code will delete a folder that isn't empty in Windows NT or Windows 2000.

The code also takes care of security aspects of the files inside the folder. Before deleting the file it sets the security(on an NTFS filesystem especially), so that it can be deleted by the process that is running it. The code also takes care of any readonly attributes set on the file to be deleted.

This is a recursive function which delete all the directories and files inside the directory which is sent as a command line parameter.

To compile:

You have to run the compile.bat file from a windows console.

To run it:

C:\Test\DeleteDirectory.exe test1

where test1 is the directory to be deleted, and is inside the Test directory.

Note:
This will delete all the contents, I repeat ALL contents inside the directory which has to be passed as a command line argument. So please be very careful.

Downloads

Download source - 16 Kb


Comments

  • Helllp. need to get file timestamp/ file properties!!

    Posted by Legacy on 09/26/2002 12:00am

    Originally posted by: Din Krop

    Hi,

    Can anybody tell me how to get file timestamp or file propeties! Anyhow thanx in advance.

    Reply
  • What's the point

    Posted by Legacy on 09/21/2001 12:00am

    Originally posted by: Charles Godwin

    This function is available in Win 9x using the DOS command
    "deltree dirname"
    and in windows NT, 2000 and up with
    "rd /s /q dirname"

    Reply
  • Some drawbacks.

    Posted by Legacy on 09/20/2001 12:00am

    Originally posted by: Dmitriy Sholomov

    1) You have to restart FindFirstFile - FindNextFile
    construction after you delete some file or directory
    from the folder.

    2) Delete directory is a transactional operation.
    If some files in the directory are locked by another
    process your DeleteDir function will delete some
    unlocked files and after it'll try to delete locked
    file it'll return error. In this case you have to abort
    transaction.

    3) DeleteDir works only under Windows NT/2000 ?

    4) Do not use any message boxes in such kind of functions,
    use something like GetLastError. Give the chance to the
    functions to handle errors. Final users are not obliged to
    handle low-level errors with their own eyes.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

  • Live Event Date: July 22, 2015 @ 2:00 p.m. ET / 11:00 a.m. PT Today's increasingly mobile workforce creates new challenges for organizations that rely heavily on collaboration. Businesses need to be both secure and collaborative, and they can't sacrifice one over the other. The plethora of devices and clouds used by mobile employees only complicates these challenges. Attend this eSeminar and learn how your business can enable mobile access to files from anywhere while it helps to maintain information …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date