[c++]Search and delete files

Started by
4 comments, last by Zakwayda 16 years, 3 months ago
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
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.
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.
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
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
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 :-|

This topic is closed to new replies.

Advertisement