Sign in to follow this  
englia82

[c++]Search and delete files

Recommended Posts

Hi all I am trying to create a program to help me delete some files. I was going to use a C++ Console program, because i have some basic knowledge programming with it and want to learn more. I have a program hooked up to a machine with controlled axis. That program creates/organizes random filenames/programs for that machine when new updates occurs. Ex. program1.001, program1.002, program1.003, program1.004 <-One program Ex1. gfdkmjer.001, gfdkmjer.002, gfdkmjer.003 <-Another program Ex2. asdf.001, asdf.002, asdf.003, asdf.004, asdf.005, asdf.006 <-Another program Where program1.004, gfdkmjer.003 and asdf.006 are the latest files, and program1.001,asdf.001 and gfdkmjer.001 are the oldest files. I want the program to search through my hdd for files containing more than 2 updates. In my first example i want the program to delete, program1.001 and program1.002, then i want it to rename the last 2 updates to program1.001 and program1.002. Anyone able to help me with my problem :)? -Lian

Share this post


Link to post
Share on other sites
Quote:
Original post by englia82
Hi all

I am trying to create a program to help me delete some files.
I was going to use a C++ Console program, because i have some basic knowledge programming with it and want to learn more.

I have a program hooked up to a machine with controlled axis.
That program creates/organizes random filenames/programs for that machine when new updates occurs.

Ex. program1.001, program1.002, program1.003, program1.004 <-One program
Ex1. gfdkmjer.001, gfdkmjer.002, gfdkmjer.003 <-Another program
Ex2. asdf.001, asdf.002, asdf.003, asdf.004, asdf.005, asdf.006 <-Another program

Where program1.004, gfdkmjer.003 and asdf.006 are the latest files, and program1.001,asdf.001 and gfdkmjer.001 are the oldest files.


I want the program to search through my hdd for files containing more than 2 updates. In my first example i want the program to delete, program1.001 and program1.002, then i want it to rename the last 2 updates to program1.001 and program1.002.

Anyone able to help me with my problem :)?

-Lian
If I had to write this program in C++, I would probably use boost::filesystem to enumerate the files in the directory of interest, boost::split to tokenize each file name with '.' as the delimiter, and then store the results in, say, a map of strings to sets of strings or integers (with the result being a map of file names - e.g. 'program1' - to ordered sets of extensions - e.g. '001', '002', etc.). From there it would be a simple matter of deleting all but the last two files in the set, and then renaming the files that are left.

I'm sure there are other (maybe better) ways to do it, but this is what first comes to mind.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by englia82
Anyone able to help me with my problem :)?

-Lian
If I had to write this program in C++, I would probably use boost::filesystem to enumerate the files in the directory of interest, boost::split to tokenize each file name with '.' as the delimiter, and then store the results in, say, a map of strings to sets of strings or integers (with the result being a map of file names - e.g. 'program1' - to ordered sets of extensions - e.g. '001', '002', etc.). From there it would be a simple matter of deleting all but the last two files in the set, and then renaming the files that are left.

I'm sure there are other (maybe better) ways to do it, but this is what first comes to mind.


Sounds reasonably sane to me :) I would look at using the extension() function from the Boost.Filesystem library instead of using split(), however.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
I would look at using the extension() function from the Boost.Filesystem library instead of using split(), however.
Yup. It's a bit puzzling why I didn't suggest this myself, given that I use the extension() function fairly frequently in my own code :-|

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this