- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力寻找解决方案来完成本来应该非常简单的任务。我需要将某种类型的文件(本例中为所有 zip 文件)移动到另一个目录中。我尝试过 NSTask 和 NSFileManager 但结果是空的。我可以一次移动一个,但我想同时一次移动它们。
- (void)copyFilesTo :(NSString*)thisPath {
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:thisPath];
NSString *filename = nil;
while ((filename = [direnum nextObject] )) {
if ([filename hasSuffix:@".zip"]) {
[fileManager copyItemAtPath:thisPath toPath:newPath];
}
}
}
失败 - 复制的文件 = 零
- (void)copyFilesMaybe :(NSString*)thisPath {
newPath = [newPath stringByAppendingPathComponent:fileName];
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/find"];
[task waitUntilExit];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: thisPath, @"-name", @"*.zip", @"-exec", @"cp", @"-f", @"{}", newPath, @"\\", @";", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
}
同样的悲伤结果,没有复制文件。我到底做错了什么?
最佳答案
在第一种情况下,您在复制调用中没有使用filename
。您需要通过将 filename
与 thisPath
组合并尝试复制来构造文件的完整路径。另外,该方法是-copyItemAtPath:toPath:error:
。您遗漏了最后一个参数。尝试:
NSError* error;
if (![fileManager copyItemAtPath:[thisPath stringByAppendingPathComponent:filename] toPath:newPath error:&error])
// handle error (at least log error)
在第二种情况下,我认为你的arguments
数组是错误的。我不确定为什么它包含 @"\\"
。我怀疑是因为在 shell 中你必须用反斜杠 (\;
) 转义分号。但是,需要转义分号是因为 shell 会解释它并且不会将其传递给 find
。由于您没有使用 shell,因此不需要这样做。 (此外,如果您确实需要转义它,它不应该是参数数组的单独元素。它应该与分号位于同一元素中,例如 @"\\;"
。 )
另外,您确定任务已经完成吗?您显示了启动,但没有显示出观察或等待其终止。鉴于您已经为其输出设置了一个管道,您必须从该管道中读取数据,以确保子进程不会在写入时卡住。
我不确定您为什么在启动任务之前调用 -waitUntilExit
。不过,这可能是无害的。
关于objective-c - NSFileManager 或 NSTask 移动文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734221/
我在文档目录中有一个名为a.caf的文件。我想在用户在 UITextField 中输入内容并按下更改时重命名它(在 UITextField 中输入的文本应该是新文件名)。 我该怎么做? 最佳答案 您可
我正在尝试为我的应用程序添加 iCloud 支持,并且一直在关注 Apple 自己的 iCloud Design Guide .该指南说我应该通过调用 [NSFileManager ubiquityI
如果我使用 NSFileManager:URLForDirectory 获取沙箱的应用程序支持目录,那么我会得到以下 URL: file://localhost/var/mobile/Applicat
在 iOS 中,我知道有时候 [[NSFileManager alloc] init] 更好——特别是当 [NSFileManager defaultManager] 可能不是线程安全的时候。引用:h
我想对 NSFilePosixPermissions 使用八进制权限(用于 chmod)。 这是我现在所做的: NSFileManager *manager = [NSFileManager defa
我正在尝试编写一些代码来检查或更改(不是恶意或类似的)用户设备上安装的其他 iOS 应用程序。特别是在越狱设备上。在越狱设备上,我能够查看应用程序沙箱之外的文件,当我使用 NSFileManager
在我的 iPhone 应用程序中,我使用默认的 NSFileManager 来复制和移动一些数据。数据量可能有几 MB,因此可能需要几秒钟才能完成复制。如果用户现在启动复制过程并退出应用程序,我可以继
你能发现这行代码中的错误吗?它返回零!该应用程序完全沙盒化,但启用了下载文件夹访问。 NSArray*array = [[NSFileManager defaultManager] contentsO
我有一个动态库,我假设它使用 NSFileManager 的实例。对于我的应用程序,我需要使用 NSFileManager 写入与动态库相同的文件。如果动态库的 NSFileManager 和我的 N
我在我编写的以下代码片段中发现了内存泄漏 NSFileManager *fileManager=[[NSFileManager alloc] init]; fileList=[[fileManager
我需要从应用程序的资源文件夹中复制一些示例文件,并将它们放在应用程序的文档文件夹中。我想出了附加的代码,它编译得很好,但不起作用。我引用的所有目录都存在。我不太确定我做错了什么,有人可以指出我正确的方
如何删除目录中的所有内容而不删除目录本身?我想基本上清空一个文件夹,但保持它(和权限)完好无损。 最佳答案 例如通过使用目录枚举器: NSFileManager *fileManager = [[NS
复制文件时,我想在我的iOS应用程序中包含OSX行为:如果存在,则在其上附加一个计数器: myFile.png => myFile 1.png myFile20.png => myFile20 1.p
我正在尝试创建两个日志文件并将第二个文件中的内容替换为第一个文件中的内容。 我在 AppDelegate 中创建日志的代码 NSArray *paths3 = NSSearchPathForDi
我使用的应用程序可以从互联网下载图像并缓存它们,以便在用户在图像之间来回滑动时实现更快的性能。 我想在关闭 UIViewController 时清除缓存文件夹。所以我创建了这个静态方法。但是,我不断收
我正在使用 NSFileManager 搜索“index.png”文件。如果文件不存在,那么我将尝试显示默认图像。我正在使用下面的代码,但它不起作用。我在这里做错了什么吗? NSString *pat
有没有办法告诉,如果调用 [[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL]; 会失败吗? 最佳答案 我想你想看
我一直在尝试使用 NSFileManager 将图像缓存到我的沙盒应用程序的缓存目录中。不幸的是,我的代码似乎不起作用。 - (BOOL)saveImageToCacheFolder { NS
我正在尝试将数据保存到 NSFileManager 创建的目录中,但是当我尝试检索数据和 NSLog 时,我得到的是空值。另外,当您创建一个目录时,是否意味着您在指定的 url 路径下创建了一个文件夹
我在 Apple 托管我的应用内购买内容。我已成功下载内容,现在我想将其保存在设备中。 [[NSFileManager defaultManager] removeItemAtPath:dstPath
我是一名优秀的程序员,十分优秀!