Jump to content
  • Advertisement
Sign in to follow this  
englia82

[c++]Search and delete files

This topic is 3821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Thanks for the quick response.

I will try completing my task using Boost.Filesystem library.
Never used it before, so i will start reading right away:P

Share this post


Link to post
Share on other sites
Be aware that the documentation for filesystem is not currently in sync with the implementation. This copy of the old documentation may be useful.

Σnigma

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!