我正在将图像下载到我的应用程序,几周之后用户就不会关心了。我将它们下载到应用程序中,这样就不必每次发布都下载它们。问题是我不希望Documents文件夹变得比它随时间变大。所以我认为我可以“清理”超过一个月的文件。
问题是,那里会有一些文件会超过一个月,但我不想删除。它们将是静态命名文件,因此它们很容易识别,只有3或4个。虽然我想删除几十个旧文件。所以这是一个例子:
picture.jpg <--Older than a month DELETE
picture2.jpg <--NOT older than a month Do Not Delete
picture3.jpg <--Older than a month DELETE
picture4.jpg <--Older than a month DELETE
keepAtAllTimes.jpg <--Do not delete no matter how old
keepAtAllTimes2.jpg <--Do not delete no matter how old
keepAtAllTimes3.jpg <--Do not delete no matter how old
我如何有选择地删除这些文件?
提前致谢!
答案 0 :(得分:12)
删除超过两天的文件的代码。最初我回答here。我测试了它,它在我的项目中工作。
<强> P.S。在删除Document目录中的所有文件之前要小心,因为这样做可能会导致丢失数据库文件(如果您正在使用.. !!),这可能会给您的应用程序带来麻烦。这就是为什么我保持条件在那里。 : - ))
// Code to delete images older than two days.
#define kDOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
NSFileManager* fileManager = [[[NSFileManager alloc] init] autorelease];
NSDirectoryEnumerator* en = [fileManager enumeratorAtPath:kDOCSFOLDER];
NSString* file;
while (file = [en nextObject])
{
NSLog(@"File To Delete : %@",file);
NSError *error= nil;
NSString *filepath=[NSString stringWithFormat:[kDOCSFOLDER stringByAppendingString:@"/%@"],file];
NSDate *creationDate =[[fileManager attributesOfItemAtPath:filepath error:nil] fileCreationDate];
NSDate *d =[[NSDate date] dateByAddingTimeInterval:-1*24*60*60];
NSDateFormatter *df=[[NSDateFormatter alloc]init];// = [NSDateFormatter initWithDateFormat:@"yyyy-MM-dd"];
[df setDateFormat:@"EEEE d"];
NSString *createdDate = [df stringFromDate:creationDate];
NSString *twoDaysOld = [df stringFromDate:d];
NSLog(@"create Date----->%@, two days before date ----> %@", createdDate, twoDaysOld);
// if ([[dictAtt valueForKey:NSFileCreationDate] compare:d] == NSOrderedAscending)
if ([creationDate compare:d] == NSOrderedAscending)
{
if([file isEqualToString:@"RDRProject.sqlite"])
{
NSLog(@"Imp Do not delete");
}
else
{
[[NSFileManager defaultManager] removeItemAtPath:[kDOCSFOLDER stringByAppendingPathComponent:file] error:&error];
}
}
}
答案 1 :(得分:4)
您可以获取文件创建日期,查看此SO Post,然后只比较日期。并为需要删除的文件创建两个不同的数组,而不是要删除..
答案 2 :(得分:1)
我的两分钱值得。变更符合要求以适应。
import java.sql.*;
public class InsertPrepared {
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306//test","sanjib","");
PreparedStatement stmt=con.prepareStatement("insert into employee values(???)");
stmt.setInt(1,101);
stmt.setString(2,"Sampa");
int i=stmt.executeUpdate();
System.out.println(i+"Records is inserted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
答案 3 :(得分:0)
要查找文件的创建日期,您可以参考一个非常有用的StackOverflow帖子:
iOS: How do you find the creation date of a file?
请参阅此帖子,这可能会帮助您删除它们。您可以大致了解从文档目录中删除这些数据需要做些什么:
How to delete files from iPhone's document directory which are older more than two days
希望这会对你有所帮助。
答案 4 :(得分:0)
这是一个函数,它不对日期使用字符串比较并预取枚举器中的修改时间:
$scope.caller = function (somefunc) {
$http(..).then(somefunc));
};
如果您不关心被删除的文件,可以让它返回+ (NSArray<NSURL *> *)deleteFilesOlderThan:(NSDate *)earliestDateAllowed
inDirectory:(NSURL *)directory {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator<NSURL *> *enumerator =
[fileManager enumeratorAtURL:directory
includingPropertiesForKeys:@[ NSURLContentModificationDateKey ]
options:0
errorHandler:^BOOL(NSURL *_Nonnull url, NSError *_Nonnull error) {
NSLog(@"Failed while enumerating directory '%@' for files to "
@"delete: %@ (failed on file '%@')",
directory.path, error.localizedDescription, url.path);
return YES;
}];
NSURL *file;
NSError *error;
NSMutableArray<NSURL *> *filesDeleted = [NSMutableArray new];
while (file = [enumerator nextObject]) {
NSDate *mtime;
if (![file getResourceValue:&mtime forKey:NSURLContentModificationDateKey error:&error]) {
NSLog(@"Couldn't fetch mtime for file '%@': %@", file.path, error);
continue;
}
if ([earliestDateAllowed earlierDate:mtime] == earliestDateAllowed) {
continue;
}
if (![fileManager removeItemAtURL:file error:&error]) {
NSLog(@"Couldn't delete file '%@': %@", file.path, error.localizedDescription);
continue;
}
[filesDeleted addObject:file];
}
return filesDeleted;
}
以指示是否有任何错误,或者只是BOOL
如果您只是想尽力而为尝试。
要有选择地保留一些文件,要么将正则表达式参数添加到应该保留的文件中,并在while循环中添加一个检查(似乎最适合您的用例),或者如果有的话一个具有不同模式的离散数量的文件,您可以接受void
文件名以保留并检查是否包含在集合中,然后再进行删除。
这里也只是提到这一点,因为它可能与某些相关:iOS和OSX上的文件系统不会以超过一秒的精度存储mtime,因此如果您需要毫秒精度或类似的话,请注意。< / p>
如果您愿意,可以将相应的测试用例放入测试套件中:
NSSet
答案 5 :(得分:0)
在Swift 3和4中,删除DocumentsDirectory中的特定文件
do{
try FileManager.default.removeItem(atPath: theFile)
} catch let theError as Error{
print("file not found \(theError)")
}