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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds