iOS 对文件夹中的文件进行遍历

在制作个人播放器的时候,需要遍历本地音乐文件夹,于是知道了遍历文件的方法,现记录如下,方便日后查找

NSArray *fileList = [[NSFileManager  defaultManager]  directoryContentsAtPath:deskPath];  //遍历路径下所有文件

NSArray *fileList = [[[NSFileManager defaultManager]  directoryContentsAtPath:DOCUMENTS_FOLDER]

                pathsMatchingExtensions:[NSArray arrayWithObject:@"png"]] ;      // 遍历后缀为png的文件
在C++,可以使用文件流(fstream)来实现拷贝文件夹及其下面的所有文件。 以下是一个简单的例子: ```cpp #include <iostream> #include <fstream> #include <filesystem> void copyFolder(const std::string& source, const std::string& destination) { // 创建目标文件夹 std::filesystem::create_directory(destination); // 遍历文件夹的所有文件和子文件夹 for (const auto& entry : std::filesystem::recursive_directory_iterator(source)) { // 构建目标文件文件夹的路径 std::filesystem::path destinationPath = destination; destinationPath /= entry.path().string().substr(source.length()); if (entry.is_directory()) { // 如果是文件夹,创建目标文件夹 std::filesystem::create_directory(destinationPath); } else { // 如果是文件,拷贝文件 std::ifstream src(entry.path().string(), std::ios::binary); std::ofstream dst(destinationPath.string(), std::ios::binary); dst << src.rdbuf(); } } } int main() { // 拷贝源文件夹及其下面的所有文件到目标文件夹 copyFolder("/path/to/source/folder", "/path/to/destination/folder"); return 0; } ``` 这段代码使用了C++17文件系统库(filesystem)来实现拷贝文件夹及其下面的所有文件。首先,我们创建了目标文件夹,然后使用递归目录迭代器遍历文件夹的所有文件和子文件夹。对于每个文件文件夹,我们构建目标文件文件夹的路径,并检查它是文件还是文件夹。如果是文件夹,我们创建目标文件夹;如果是文件,我们使用文件流(fstream)来拷贝文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值