简体   繁体   English

当已经存在同名文件夹时,如何使用boost创建新文件夹?

[英]How to create a new folder using boost when a folder with the same name already exists?

I am using boost::filesystem to create an empty folder (in Windows). 我正在使用boost::filesystem创建一个空文件夹(在Windows中)。 Let say that the name of the folder that I want to create is New Folder . 假设我要创建的文件夹的名称是New Folder When I run the following program, a new folder with the required name is created, as expected. 当我运行以下程序时,将按预期方式创建一个具有所需名称的新文件夹。 When the run the program for the second time, I want New Folder (2) to be created. 当第二次运行该程序时,我希望创建新文件夹(2) Though it is an unreasonable expectation, that is what I want to achieve. 尽管这是不合理的期望,但这是我想要实现的目标。 Can someone guide me? 有人可以指导我吗?

#include <boost/filesystem.hpp>
int main()
{
     boost::filesystem::path dstFolder = "New Folder";
     boost::filesystem::create_directory(dstFolder);
     return 0;
}

Expected output: 预期产量:

预期产量

It should be easy to accomplish what you want without using anything platform specific... 无需使用任何特定于平台的平台即可轻松实现所需的目标...

std::string dstFolder = "New Folder";
std::string path(dstFolder);

/*
 * i starts at 2 as that's what you've hinted at in your question
 * and ends before 10 because, well, that seems reasonable.
 */
for (int i = 2; boost::filesystem::exists(path) && i < 10; ++i) {
  std::stringstream ss;
  ss << dstFolder << "(" << i << ")";
  path = ss.str();
}

/*
 * If all attempted paths exist then bail.
 */
if (boost::filesystem::exists(path))
  throw something_appropriate;

/*
 * Otherwise create the directory.
 */
boost::filesystem::create_directory(path);

This clearly can not be achieved using boost alone. 显然,仅使用boost无法实现。 You need to check whether folder exists and manually generate new names. 您需要检查文件夹是否存在并手动生成新名称。 On Windows you can use PathMakeUniqueName and PathYetAnotherMakeUniqueName shell functions for this purpose. 在Windows上,可以PathMakeUniqueName使用PathMakeUniqueNamePathYetAnotherMakeUniqueName Shell函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查文件夹中是否已经存在文件名? - Check whether file name already exists in a folder or not? boost :: filesystem :: rename:该文件已经存在时无法创建该文件 - boost::filesystem::rename: Cannot create a file when that file already exists 如何确定文件夹是否存在以及如何创建文件夹? - How to find out if a folder exists and how to create a folder? 如何使用C ++在%APPDATA%中创建一个新文件夹? - How to create a new folder in %APPDATA% using C++? 如何在c ++中已经存在的情况下通过增加索引来创建新文件夹 - how to create a new folder with increasing its index if it already exist in c++ 无法创建文件夹时忽略 Boost.Log 上的异常 - Ignore exceptions on Boost.Log when unable to create folder 使用mkdir()创建文件夹后,如何让程序将文件和其他信息保存到新文件夹中 - After using mkdir() to create a folder how do I have the program save files and other information into the the new folder 如何创建一个程序,可以使用Boost和OpenCV读取文件夹中的所有图像? - How to create a program that can read all the images in folder using Boost and OpenCV? 在 Qml 中创建一个新文件夹 - To create a new folder in Qml Visual Studio - 使用“从现有源向导创建项目”时的新过滤器而不是新文件夹 - Visual Studio - New Filter instead of New Folder when using Create Project From Existing Source Wizard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM