Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save file in document dicretory

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 294
    Comment on it

    Following code will help you to add or delete file from document directory.

    -(NSString *)saveFileWithOldName:(NSString*)oldNameStr
    {
        NSArray *dirPaths;
        NSString *docsDirs;
        NSString *tempStr2;
        dirPaths = NSSearchPathForDirectoriesInDomains(
                                                       NSDocumentDirectory, NSUserDomainMask, YES);
        docsDirs = [dirPaths objectAtIndex:0];
    
    
       docsDirs = [docsDirs stringByAppendingPathComponent:folderrName];
    
        NSString *tempSoundPath;
    
        NSString *soundFilePath = [docsDirs
                                   stringByAppendingPathComponent:oldNameStr];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:soundFilePath])
        {
            int i = 1;
    
            NSString *tempStr = [oldNameStr stringByReplacingOccurrencesOfString:@".caf" withString:@""];
    
            //NSLog(@"saveFileWithOldName:tempStr:%@",tempStr);
            tempStr2 = [tempStr stringByAppendingFormat:@"-%d.caf",i];
            //NSLog(@"saveFileWithOldName:tempStr2:%@",tempStr2);
            tempSoundPath = [docsDirs
                                       stringByAppendingPathComponent:tempStr2];
            //NSLog(@"saveFileWithOldName:tempSoundPath:%@",tempSoundPath);
    
    
            while ([fileManager fileExistsAtPath:tempSoundPath])
            {
                i++;
                tempStr2 = [tempStr stringByAppendingFormat:@"-%d.caf",i];
                //NSLog(@"saveFileWithOldName:tempStr2:%@",tempStr2);
                tempSoundPath = [docsDirs
                                 stringByAppendingPathComponent:tempStr2];
                //NSLog(@"saveFileWithOldName:tempSoundPath:%@",tempSoundPath);
            }
    
            // rename File
            [fileManager moveItemAtPath:[docsDirs                    stringByAppendingPathComponent:oldNameStr] toPath:[docsDirs                                                                                          stringByAppendingPathComponent:tempStr2] error:NULL];
        }
    
        return tempSoundPath;
    }
    
    
    -(void)deleteFile:(NSString *)str
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSArray *dirPaths;
        NSString *docsDirs;
        dirPaths = NSSearchPathForDirectoriesInDomains(
                                                       NSDocumentDirectory, NSUserDomainMask, YES);
        docsDirs = [dirPaths objectAtIndex:0];
    
        NSString *temp = [docsDirs stringByAppendingPathComponent:str];
        if ([fileManager fileExistsAtPath:temp])
        {
            [fileManager removeItemAtPath:temp error:nil];
        }
    
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: