A
A
AF2014-05-06 10:01:29
Programming
AF, 2014-05-06 10:01:29

How to rename files in a directory?

Hello.
In any folder, using C ++, you need to rename all the files according to a certain rule. How to get filename and change it?
OS: Windows 8.1

Answer the question

In order to leave comments, you need to log in

5 answer(s)
R
Rsa97, 2014-05-06
@TiPo

opendir + readdir + closedir or findfirst + findnext
rename

G
Gibbon Cho, 2014-05-14
@gibboncho

TSearchRec Search;
      AnsiString Path = "C:\\Windows";
      if (FindFirst(Path + "\\*", faDirectory, Search) == 0) {
        do {
          if
          (Search.Name == "." || Search.Name == ".." )
            continue;
          if (Search.FindData.dwFileAttributes &
            FILE_ATTRIBUTE_DIRECTORY) {
            // Если это папка
            //
          }
          else {
            //копируем файл с новым именем
                                                CopyFile((Path+"\\"+Search.Name).t_str(),(Path+"\\"+newName).t_str(), 0);
                                                //Удаляем файл со старым именем
                                                //Запоминаем путь к файлу, который нужно удалить и после того как освободим Search удаляем.
          };
        }
        while (FindNext(Search) == 0);
        FindClose(Search);
      };

V
v_prom, 2014-05-06
@v_prom

or use OS commands.
or so you can:

#include <stdio.h>
      rename (char *oldname, char *newname);       /* ANSI */

D
DancingOnWater, 2014-05-06
@DancingOnWater

Remaining within the framework of the standard (C++03, C++11 or the upcoming C++1y) it is impossible to get the name of already existing files.
You have many libraries to choose from, the most famous are Qt and boost

A
Arseny, 2014-06-20
@Arseny092

By pressing F2 or by typing in CMD rename

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question