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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's no secret what keeps CIOs up at night. Mobile, cloud, data, security, and social have become the "five imperatives," the drivers of business progress, innovation, and competitive differentiation. Business leaders around the world want to hear how other companies are succeeding. How are they applying the latest technologies? How did they get started? What outcomes are they achieving? Read this online magazine for success stories from organizations like the NBA, Pfizer, and San Jose State University as they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds