How to do this task: Before each file name (regular file) insert its number (1, 2, 3, etc.). The number is set in accordance with the order in which the FindFirstFile function, FindNextFile, returns results.

I could only do this:

#include <windows.h> #include <iostream> #include<tchar.h> using namespace std; //strcpy, strcat //_tcscpy,_tcscat int main() { DWORD dwError = 0; setlocale(LC_ALL, "rus"); WIN32_FIND_DATA find; HANDLE hFind = FindFirstFile(_T("C:\\test\\*"), &find); { if (INVALID_HANDLE_VALUE == hFind) { cout << "Каталог не найден!" << endl; system("pause"); return dwError; } do { if (!(find.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) && !(find.dwFileAttributes == FILE_ATTRIBUTE_DEVICE)) _tprintf(_T("%s\n"), find.cFileName); } while (FindNextFile(hFind, &find) != NULL); dwError = GetLastError(); if (dwError == ERROR_NO_MORE_FILES) { FindClose(hFind); cout << "В каталоге нет файлов!" << endl; system("pause"); return dwError; } }////нет проверки на успешность FindFirstFile FindClose(hFind); ////нет проверки на успешность FindClose cout << "Файлы выведены на экран" << endl; system("pause"); return 0; } 

And how to rename files do not understand

    3 answers 3

    To rename files there is a rename function ()

     int rename(const char *old_filename, const char *new_filename) 

    or moveFile ()

     BOOL WINAPI MoveFile( _In_ LPCTSTR lpExistingFileName, _In_ LPCTSTR lpNewFileName ); 

    But note that FindNextFile cannot rename files inside the FindFirstFile / FindNextFile . Otherwise, the loop will find new files.

    In this loop, you need to add all the file names to an array, call FindClose() , and then run through this array and rename the files.

    • It still will not work correctly if someone else does something in the directory. In an amicable way, you need to set up notification for events in the directory, then start the monitor of these events, simultaneously renaming files. The above is simply not atomic. - 0andriy

    Since your code has a function system("pause") it is not a sin to use it:

     system("rename " + old_name + " " + new_name); 

      Files are renamed with the MoveFile function.

      The MoveFile function moves (renames) a file or directory (including its children), either in the same directory or in all directories.