1

我正在编写一个循环并删除文件夹中所有文件的程序。我习惯NSDirectoryEnumerator这样做。问题是,代码只循环一次,处理一个文件,然后就存在了。我从 NSLog 语句中知道这一点。它适用于单个文件和包含单个文件的文件夹。

以下是相关代码:

NSString *fileToNuke = filePath;
NSLog(@"Incinerating %@", fileToNuke);
[spinner startAnimation:self];
#ifdef DEBUG
    NSLog(@"Incinerating %i times.", timesToIncinerate);
#endif

if ([fileManager fileExistsAtPath:filePath isDirectory:NULL] && isDir) {
        NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:filePath];
        //NSString *file;

        //file = [directoryEnumerator nextObject];
        for (NSString *file in directoryEnumerator) {
            // Construct the path of the file to incinerate.
            NSString *finalPath = [NSString stringWithFormat:@"%@/%@", filePath, file];
#ifdef DEBUG
            NSLog(@"%@", finalPath);
#endif

            if (!incinerate(timesToIncinerate, [finalPath cStringUsingEncoding:(NSASCIIStringEncoding)], (int)eraseFile, useNonBlockingRandom, incinerateCallback)) {
                [spinner stopAnimation:self];
                erasing = NO;
                break;
            }
            [spinner stopAnimation:self];
            erasing = NO;

            //file = [directoryEnumerator nextObject];
        }
    } else if (!isDir) {
        if (!incinerate(timesToIncinerate, [fileToNuke cStringUsingEncoding:(NSASCIIStringEncoding)], (int)eraseFile, useNonBlockingRandom, incinerateCallback)) {
            [spinner stopAnimation:self];
        }
        [spinner stopAnimation:self];
        erasing = NO;
}

// Tell the application waiting on the main thread to finish up with our task by notifing the user of
// our completion.
[self performSelectorOnMainThread:@selector(finishIncineration) withObject:nil waitUntilDone:YES];

我怀疑这与我的while循环的设置方式有关,但我欢迎任何建议。我只知道我在做一些愚蠢的事情...

4

0 回答 0