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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds