AMH
AMH

Reputation: 6451

Get the latest Created file name in directory

I have folder contains audio and text files

I need to get the last created text file and audio file path without looping on the folder or without using mutable array and order using modified data

any idea

Upvotes: 0

Views: 1340

Answers (2)

Nimit Parekh
Nimit Parekh

Reputation: 16864

Another option for the when you are download the file that time you need to manage the .plist file where auto generated and that last value fetch and justify the latest download file name.

show here the one tutorial for that. Following code is use for the download the audio file and adding the .plist dynamically created

NSString *strAudioURL=@"Your sound ";

    // check first locally exists or not
    NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@",
                                   [(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
                                   AudioFolder];

    NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache];

    if([dOfAudios valueForKey:strAudioURL]) {
        [APP_DEL initAndPlayMovie:[NSURL fileURLWithPath:[dOfAudios valueForKey:strAudioURL]] andViewController:self];
    } else {
        NSURL *audioURL = [NSURL URLWithString:strAudioURL];
        NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
                                     [strAudioURL lastPathComponent]];

        if(!self.rqstForAudio || [self.rqstForAudio isFinished]) {
            self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL];
            [self.rqstForAudio setDelegate:self];
            [self.rqstForAudio setAllowResumeForFileDownloads:YES];
            [self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy];
            [self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
            [self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)];
            [self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)];
            [self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]];
            [self.rqstForAudio setDownloadDestinationPath:strPathToDownload];
            [self.rqstForAudio startAsynchronous];
        }

    }

Upvotes: 0

Mundi
Mundi

Reputation: 80265

There are two solutions:

  1. either you have no complete information about the files in the folder beforehand - then you will have to loop through them and check the modified date.
  2. or you do have control over all the things that go into that folder from the beginning - you could then keep a preference that stores the latest file's URL.

Implementation for looping:

NSString *documentsDirectory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSArray *docFileList = [[NSFileManager defaultManager] subpathsAtPath:documentsDirectory];
NSEnumerator *docEnumerator = [docFileList objectEnumerator];
NSString *docFilePath;
NSDate *lastModifiedDate = [NSDate dateWithTimeIntervalSince1970:0];
NSString *lastModifiedFilePath = @"";

while ((docFilePath = [docEnumerator nextObject])) {
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:docFilePath];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager]  attributesOfItemAtPath:fullPath error:nil];
    NSDate currentModifiedDate = [fileAttributes fileModificationDate];

    if (lastModifiedDate < fileModificationDate) {
        fileModificationDate = lastModifiedDate;
        lastModifiedFilePath = fullPath;
    }
}

return lastModifiedFilePath;

Upvotes: 3

Related Questions