3 Replies - 247 Views - Last Post: 29 January 2013 - 08:04 AM Rate Topic: -----

#1 tiongsquare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 13

How can i List out the folders under a directory del if X days old

Posted 29 January 2013 - 07:43 AM

OS: Linux
Code wise can be pure C++ or boost::filesystem also can

I need to code in my program such it will be able to read the list of available folders under a specific directory and delete them if their last write date is X days old but i have yet to figure out how to do that. anyone can help? thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How can i List out the folders under a directory del if X days old

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: How can i List out the folders under a directory del if X days old

Posted 29 January 2013 - 07:47 AM

You need to show what you have tried. Start by studying either the boost file system documentation or the documentation for dirent.h which contains the Linux directory handling functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 tiongsquare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 13

Re: How can i List out the folders under a directory del if X days old

Posted 29 January 2013 - 07:53 AM

Here the list of things my code needs to do 1) list out all the files in a specific directory(and sub directories) in linux 2) check each file last modified date and compare it with current time, if its X days old, delete it

i manage to find 3 piece of codes online but there some missing codes in between which i need before i can obtain the functionality that i want

code 1: It finds list of files in a directory and sort them in ascending order according to last write date What i needed: How can i remove that sorting and how do i literate result_set here?

namespace fs = boost::filesystem;
fs::path someDir("/path/to/somewhere");
fs::directory_iterator end_iter;

typedef std::multimap<std::time_t, fs::path> result_set_t;
result_set_t result_set;

if ( fs::exists(someDir) && fs::is_directory(someDir))
{
  for( fs::directory_iterator dir_iter(someDir) ; dir_iter != end_iter ; ++dir_iter)
{
 if (fs::is_regular_file(dir_iter->status()) )
 {
   result_set.insert(result_set_t::value_type(fs::last_write_time(dir_iter->status()),*dir_iter);
    }
 }

}


code 2: This piece of code reads in a file path and set its modified date to current time(now) What i needed: For the argv[ 1 ] below, will it be same as the path i found in the result_set in code 1 above? How can i compare t and n in the code below and check if is equal or more than X days? lastly how can i delete that particular file?

#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>

int main( int argc , char *argv[ ] ) {
   if ( argc != 2 ) {
      std::cerr << "Error! Syntax: moditime <filename>!\n" ;
      return 1 ;
   }
   boost::filesystem::path p( argv[ 1 ] ) ;
   if ( boost::filesystem::exists( p ) ) {
      std::time_t t = boost::filesystem::last_write_time( p ) ;
      std::cout << "On " << std::ctime( &t ) << " the file " << argv[ 1 ] 
     << " was modified the last time!\n" ;
      std::cout << "Setting the modification time to now:\n" ;
      std::time_t n = std::time( 0 ) ;
      boost::filesystem::last_write_time( p , n ) ; 
      t = boost::filesystem::last_write_time( p ) ;
      std::cout << "Now the modification time is " << std::ctime( &t ) << std::endl ;
     return 0 ;
   } else {
      std::cout << "Could not find file " << argv[ 1 ] << '\n' ;
      return 2 ;
   }
}


code 3: This is another piece of code i found on the net, not sure if it can use tgt with the above code to obtain what i needed

#include <boost/filesystem.hpp>   // includes all needed Boost.Filesystem declarations
    #include <iostream>              // for std::cout
    using namespace std;


    int main()
    {
        boost::filesystem::directory_iterator iterator(string("."));
        for(; iterator != boost::filesystem::directory_iterator(); ++iterator)
        {
            cout << (iterator->path().filename()) << endl;
        }
        return 0;
    }



Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: How can i List out the folders under a directory del if X days old

Posted 29 January 2013 - 08:04 AM

Quote

i manage to find 3 piece of codes online but there some missing codes in between which i need before i can obtain the functionality that i want


As I said in my first post you need to show what you have done, not what you have found that doesn't work.

You need to read the documentation for the functions or classes you are trying to use and write the code to satisfy your requirements. We are not a code writing service and without knowing your skill level we will be hard pressed to point you in the correct direction. By posting your code you give us some understanding of where you are in and what you do or don't actually understand.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1