File Excistence Check & Rename

Hello,

I am fairly new to C++ so maybe I am asking for the obvious.

However, I am trying to write a C++ script for the Windows 10 OS which does the following:
1. Find a file with certain extension (.JPG/BMP/Etc.)
2. Then alter the filename, while keeping the extension.
3. Do this in a 'while' loop, and increment by 1 every time a filename is changed. So the result would be, if I have 1000 BMP pictures, I have 1.bmp, 2.bmp etc. etc.

I have Googled for hours, even implemented boost:: but I cannot seem to get it working, not because of errors, but because I cannot seem to think of the right script itself for this.

My taught wss something like this;
1. Create a boolean, which searches for JPG/BMP extensions.
2. If this would be true, then use this in a while loop and call a function for example to change the name.

But again... the codes which I found on the net are far to complicated for me to understand right now, most script averages around 40 lines of main coding. I can hardly belief that C++ doesn't have a neat way of handling such design requests. Also those scripts have a other intented actions so manipulation of those scripts seems too hard for me.

My question: can someone point me in the right direction?
I don't think it's possible to make it under 40 line of code.
Well if it is neccesary, I will go for it :)
Seems boost is the 'best' way to do it?
Here is an example: renames all files in current directory with .jpg extention to xxx.jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <sstream>

#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;

int main()
{
    const std::string extention = ".jpg";
    size_t ind = 0;
    std::ostringstream format;
    format.fill('0');
    for(fs::directory_iterator it("."); it != fs::directory_iterator(); ++it) {
        {
            std::string ext = it->path().extension().generic_string();
            std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
            if(ext != extention) continue;
        }
        format.str(""); format << std::setw(3) << ++ind;
        auto target = it->path();
        target.remove_filename();
        target /= (format.str() + extention);
        fs::rename(*it, target);
    }
}

Last edited on
> Seems boost is the 'best' way to do it?

Yes. The filesystem TS (ISO/IEC TS 18822:2015 http://en.cppreference.com/w/cpp/experimental/fs ) is not yet widely available.

Something along these lines, perhaps (caveat:untested):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem ;

void rename_files_in_directory( fs::path dir, std::string ext ) // ext with a . prefix eg. ".txt"
{
    if( fs::exists(dir) && fs::is_directory(dir) ) try
    {
        int file_number = 1 ;
        for( fs::directory_iterator iter(dir) ; iter != fs::directory_iterator() ; ++iter )
        {
            const fs::path path = iter->path() ;
            if( fs::is_regular_file(path) && fs::extension(path) == ext ) // (windows) TODO: compare ignoring case
                fs::rename( path, std::to_string(file_number++) + ext ) ;
        }
    }
    catch( const std::exception& ) { /* error */ }
}
Thanks a lot guys!!
I am going to give this a try and will get back to you!
Topic archived. No new replies allowed.