【问题标题】:Rename all files in a user specified directory重命名用户指定目录中的所有文件
【发布时间】:2014-10-14 04:37:16
【问题描述】:

所以我希望能够自动重命名用户指定目录中的所有文件(我知道 c++ 不是执行此操作的最佳语言/工具)。

例如,一个目录当前包含数百个具有随机字符名称的文件,我希望将它们全部更改为

8 月 1 日、8 月 2 日、8 月 3 日等

代码

用户指定这样的目录

std::string directory;

std::cout << "Enter directory: ";
std::cin >> directory;

目录是这样打开的(使用dirent.h

DIR* pdir = NULL;
struct dirent* pent = NULL;
const char* DIRECTORY;

// convert directory string to const char
DIRECTORY = directory.c_str();

pdir = opendir(DIRECTORY);

这里重命名指定目录下的所有文件

int i = 1;
std::string s, oldname, newname;
const char* OLDNAME, * NEWNAME;

while (pent = readdir(pdir))
{
    // convert int i to str s
    std::stringstream out;
    out << i;
    s = out.str();

    oldname = pent->d_name;
    newname = "August " + s;

    OLDNAME = oldname.c_str();
    NEWNAME = newname.c_str();

    rename(OLDNAME, NEWNAME);

    i++;
}

一切正常,直到 while 循环似乎什么也没做,这就是我卡住的部分。

然而,奇怪的是,这个用于显示目录内容的 while 循环(使用与非工作循环相同的逻辑和语法)完美地工作

while (pent = readdir (pdir))
{
    std::cout << pent->d_name << "\n";
}

Win7上使用MSVS2012


我想我会遇到的另一个问题是,在目录中,不同的文件具有不同的扩展名(这可以通过保存原始名称的最后 4 个字符并将其附加到新名称来解决,但不确定,将不胜感激)。

【问题讨论】:

  • 我认为您可能遇到了权限问题...您是否尝试过以管理员身份运行您的程序?

标签: c++ algorithm directory file-rename std-filesystem


【解决方案1】:

使用&lt;filesystem&gt;,现在可以做到,如下所示。我希望这可能对未来的读者有所帮助:

#include <iostream>
#include <string>
#include <filesystem>  //std::filesystem::path, std::filesystem::recursive_directory_iterator

std::string changeFileName(const std::string& currentFileName, const std::string& extension, int number)
{
   std::cout << "Current file name: " << currentFileName << "\n";

   // ....other logics regarding current filename

   const std::string newFileName{ "August - " + std::to_string(number) + extension};    
   std::cout << "File name after renaming: " << newFileName << "\n";
   return newFileName; // new file name
}

int main()
{
   const std::filesystem::path myPath = ".....full path....";
   const std::string extension{ ".txt" };
   int number{ 0 };
   // iterate through all the files in the directory
   for (const auto& dirEntry : std::filesystem::directory_iterator(myPath))
   {
      // if the file is meant for changing name!
      if (std::filesystem::is_regular_file(dirEntry) && dirEntry.path().extension() == extension)
      {
         const std::string currentFileName{ dirEntry.path().filename().string() };
         // following function is meant for the logic to provide the new file names in the directory
         // in your case it cout have been simply: "August - " + std::to_string(number++) + "extension"
         const std::string newFileName{ changeFileName(currentFileName, extension, number++) };
         try
         {
            std::filesystem::rename(myPath / currentFileName, myPath / newFileName);
         }
         catch (std::filesystem::filesystem_error const& error)  // catch the errors(if any)!
         {
            std::cout << error.code() << "\n" << error.what() << "\n";
         }
      }

   }
}

【讨论】:

    【解决方案2】:

    问题在于pent-&gt;d_name 只是没有关联路径的文件名。因此,当您执行rename 时,程序将在当前目录中查找oldname,由于该目录中不存在它,rename 不会执行任何操作。要解决这个问题,您应该将路径附加到 oldnamenewname,例如:

    oldname = (std::string(DIRECTORY)+pent->d_name).c_str();
    newname = (std::string(DIRECTORY)+"August " + s).c_str();
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 2014-05-16
      • 2015-02-28
      • 2014-11-08
      • 2014-10-09
      • 2017-06-21
      • 1970-01-01
      • 2014-12-30
      相关资源
      最近更新 更多