Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Programmatically deleting old files with a C program

Programmatically deleting old files with a C program

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChemmieBro
    wrote on last edited by
    #1

    I have an ancient program that creates logs named using the time when they are created. I need to delete the oldest files in the folder, only keeping the most recent 20, during runtime. Is there any way to do this in C? I can use Remove(filename), but then I have to go through each filename and compare dates. Seems like a hassle. Is there any function that returns the date of a file for comparison? Thanks.

    D L P 3 Replies Last reply
    0
    • C ChemmieBro

      I have an ancient program that creates logs named using the time when they are created. I need to delete the oldest files in the folder, only keeping the most recent 20, during runtime. Is there any way to do this in C? I can use Remove(filename), but then I have to go through each filename and compare dates. Seems like a hassle. Is there any function that returns the date of a file for comparison? Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      ChemmieBro wrote:

      Is there any way to do this in C?

      Yes.

      ChemmieBro wrote:

      Is there any function that returns the date of a file for comparison?

      How about GetFileTime()?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      1 Reply Last reply
      0
      • C ChemmieBro

        I have an ancient program that creates logs named using the time when they are created. I need to delete the oldest files in the folder, only keeping the most recent 20, during runtime. Is there any way to do this in C? I can use Remove(filename), but then I have to go through each filename and compare dates. Seems like a hassle. Is there any function that returns the date of a file for comparison? Thanks.

        L Offline
        L Offline
        loyal ginger
        wrote on last edited by
        #3

        Your program has to check all the log files to find which 20 files are the latest. Then the program can delete all the old ones. The program can get file times using CFileFind::GetLastWriteTime() (if you are using MFC). You said the filenames contain the time also. In that case you can also parse the filenames to get the times. You can load the file times (together with the pathnames) into an array and sort them to find out the files that should be deleted. Sorting can be done easily by STL utilities. Seems to be a hassle, but it's done by your program. The only hassle to you will be to write this program, just once. By using MFC and STL to implement this, it's not a big deal at all. Good luck!

        C 1 Reply Last reply
        0
        • L loyal ginger

          Your program has to check all the log files to find which 20 files are the latest. Then the program can delete all the old ones. The program can get file times using CFileFind::GetLastWriteTime() (if you are using MFC). You said the filenames contain the time also. In that case you can also parse the filenames to get the times. You can load the file times (together with the pathnames) into an array and sort them to find out the files that should be deleted. Sorting can be done easily by STL utilities. Seems to be a hassle, but it's done by your program. The only hassle to you will be to write this program, just once. By using MFC and STL to implement this, it's not a big deal at all. Good luck!

          C Offline
          C Offline
          ChemmieBro
          wrote on last edited by
          #4

          That's about what I thought. (parsing the names) I'm stuck with standard C with no MFC support. This is an old, and very basic program, that I'm modifying. Looks like I'll just have to sort and use the Remove("filename.txt") function. Even with getting the file times, I'll still have to do some comparing/sorting. I'm actually thinking of just changing the file naming to adding a number to the end of the file. Since there are only 20 or 30 files (whatever I choose), I'll just increment those each time and delete when there gets to be 31 in the folder... it is crude, but easy. Thanks for the input. Thank goodness for programming advances since this was written...

          1 Reply Last reply
          0
          • C ChemmieBro

            I have an ancient program that creates logs named using the time when they are created. I need to delete the oldest files in the folder, only keeping the most recent 20, during runtime. Is there any way to do this in C? I can use Remove(filename), but then I have to go through each filename and compare dates. Seems like a hassle. Is there any function that returns the date of a file for comparison? Thanks.

            P Offline
            P Offline
            Patcher32
            wrote on last edited by
            #5

            Since your program creates log files names based on date and time, you can parse file names for the time when they were created. That would be better method, than finding the file's last write time.

            V 1 Reply Last reply
            0
            • P Patcher32

              Since your program creates log files names based on date and time, you can parse file names for the time when they were created. That would be better method, than finding the file's last write time.

              V Offline
              V Offline
              vasu_sri
              wrote on last edited by
              #6

              just create a file with number like file1.txt,file2.txt ..... file20.txt. after that, check like this,

              for (int i=0;i<20;i++)
              {
              	CString str;
              	str.Format(\_T("file%d"),i);
              	if(!PathFileExists(str))
              		break;
                          DeleteFile(str);
              
              }
              

              if file doesn't exists then break the loop.

              Regards, Srinivas

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups