gpt4 book ai didi

macos - 循环浏览目录并获取所有文件和文件夹的路径

转载 作者:行者123 更新时间:2023-12-03 17:34:13 24 4
gpt4 key购买 nike

你好,我被困在这件事上,我需要循环浏览某个 dir我需要从中获取所有文件名并将路径作为变量获取。

我知道如何创建循环,但要获取目录的内容,我不知道。

感谢任何帮助,谢谢!

最佳答案

尝试这样:

// If your folder is a document.
NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent: @"Documents"];
// else you can give your folder path as well, if you know
// for example like this NSString *docsDir = @"user/desktop/testFolder"

NSFileManager *localFileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];
NSString *file = nil;
NSData *fileContents = [NSData data];
while ((file = [dirEnum nextObject]))
{
NSLog(@"your file name%@",file); // This will give your filename
// Now for getting file path follow below.
// here we are adding path to filename.
NSString *fileNamePath = [docsDir stringByAppendingPathComponent:file];
NSLog(@"your fileNamePath%@",fileNamePath); // This will give your filename path
fileContents = [NSData dataWithContentsOfFile:fileNamePath]; // This will store file contents in form of bytes

}

希望有帮助:)

关于macos - 循环浏览目录并获取所有文件和文件夹的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18683345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com