Program which will delete folders and files RRS feed

  • Question

  • Hello!

    Im writing a program wchich will delete folders older that 90days and create a raport in Word(one raport for 1folder).

    For example:

    There are 10folders in every of these folders are other ones and some files(include 1xls). 

    6 of these 10 folders are older than 90days so they should be deleted and raport should be created(xls name is copied to the raport).

    Part with creating raport, dates etc is working but i kinda dont know how should i checking these folder, go in to folder, copy xls name and then delete whole folder.

    Wednesday, May 4, 2016 6:31 AM

All replies

  • Here my suggestion;

    1. Iterate your directories (Read: How to: Iterate Through a Directory Tree on MSDN page)
    2. Find directories older than 90 days using with FileSystemInfo.CreationTime, FileSystemInfo.LastAccessTime or FileSystemInfo.LastWriteTime properties (depends on what you mean by "older" exactly)
    3. Iterate on those directories and also iterates on files inside of them.
    4. Get the file name with FileInfo.Name, log this name and move it with File.Move method.
    5. After you delete all files in a directory, delete directory with Directory.Delete method also.
    • Proposed as answer by sonergonul Wednesday, May 4, 2016 8:20 AM
    • Unproposed as answer by sonergonul Wednesday, May 4, 2016 8:20 AM
    Wednesday, May 4, 2016 8:20 AM
  • First I feel it nessesary to warn you that it might be plain a bad idea:
    Windows XP had a feature to automatically move Shortcuts of the Desktop if they were not used for 60 days and was set to be on by default.
    Like Clippy people hated it, because it was guessing. Few things are as dumb as a programm that tries to be smart by guessing stuff like "no logner in use" based on last access date or creation date.

    Secondly I have to ask a bunch of questions:

    0. What exactly is the problem you try to solve here?
    It feels like one of those cases where you picked a totally faulty solution to a very real problem, because you were not aware there is a better way to do it all along.
    Happens a lot. I would prefer to solve the actuall problem, rather then help you with your custom hack you thought was the best way to solve it.

    1. What is the exact defintion of a "folder being older then 90 days"?
    The Folder Properties only seem to carry a "Created" date value. The "Last Change" date we can see in a list overview - I have no idea where that comes from. Could be calculated from the newest value of every contained folder and file.

    2. What exactly should be the behavior with deeply nested folders?
    What if a folder contains a folder older then 90 days, along with other newer folders? Should the whole parent be deleted, or only to child that came of age?
    What if a folder only contains a single file older then 90 days, but the folder itself is not so old?

    3. How will this code be run? Explicitly by user? Is it permanently running in background like a Windows Service? Run by windows Task Sheduler?
    Should the code run and complete, or keep running in background after first start?

    4. What about access problems? A file might be in use. You might not have the rights to affect a file.
    In order to do it's job the code propably needs to be run as elevated 24/7.
    And then you still have to deal with files being in use. What will you do with those?

    5. Word is one of the harder formats to make a report in. It is possible without many issues if you keep to the new .docx Format. the older .doc format is a pain.
    But a text file, email or even HTML file might still be easier to create and read.

    Wednesday, May 4, 2016 9:19 AM
  • Hello!

    Tkanks for these replyes,

    Here are my answers :):

    1. It will be CreationDate, for now im using this:

    DataUtworzenia.AddDays(90) < DataTeraz

    Where DataUtworzenia = CreationDate

    and DataTeraz = TodayDate

    I will change it to Last Change date when i will know it work correctly.

    2. For example i have path:


    And in this path i have couple folders and in these folder i have another ones, but in folders like this:


    I have .xls file (exacly 1 file in folder), so it has to go to like 1st child and then copy this name, after that it should delete whole folder (even  if folders and files inside are newest than 90days).

    The path which i will declare in program is D:\Files\Folders and it has to go to all folders in this path and copy the xls name, xls is in 1st child (D:\Files\Folders\Test1\blabla.xls),(D:\Files\Folders\Test2\bblublub.xls)

    3. It will be in Task Sheduler

    4. I have all rights to the files because im Administrator and these are normal files(not system ones), files aret in use by anybody.

    5. Word file already work, this is in docx and i use :

    using Word = Microsoft.Office.Interop.Word

    Wednesday, May 4, 2016 12:13 PM