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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds