My little file copy/backup program
My little file copy/backup program
- Subject: My little file copy/backup program
- From: "Bobby B" <email@hidden>
- Date: Wed, 22 Mar 2006 12:16:49 -0400
Hey guys;
I made a small little backup program. It's very basic, does almost
what I needed in a program (just to update the non-existing files,
throughout a directory tree, much like "cp -Rnv". Here it is.. It
first goes through and gives you a list of what it will copy, and then
copies it. It's really basic, and there is a few unnecessary things
in there, as I was learning bindings at the same time.
I'm posting this cause maybe there is one person out there who knows
less Cocoa than I, so it may help them :) If anyone has comments on
why I'm doing something stupidily, please say!
Bobby B
-(IBAction) copyAction:(id)sender {
NSFileManager * fm = [NSFileManager defaultManager];
NSString * srcPath = [NSString stringWithString:[myObj mySourcePath]];
NSString * destPath = [NSString stringWithString:[myObj myDestinationPath]];
NSString * file;
NSDirectoryEnumerator * dirEnum = [[NSFileManager defaultManager]
enumeratorAtPath: srcPath];
NSMutableArray * newFiles = [NSMutableArray array];
// This goes through the files files the user wants to backup, and
checks which ones exist
// and which ones don't. The ones that don't exist are added to the
array "newFiles"
while (file = [dirEnum nextObject]) {
NSString * testStr = [NSString stringWithFormat:@"%@/%@",destPath,file];
// If the file exists...
if ([fm fileExistsAtPath: testStr]) {
// And if the file does not exist, we should copy it, and put it in
the text thing
} else {
[newFiles addObject: file];
}
}
// This updates our object's array of files.. Not really necessary
[myObj setMyNewFiles: newFiles];
// This whole enumerator goes through the files that need to be
copied, and makes a string of them,
// to show in the text box, and give to the user as a present, haha.
NSEnumerator * buildUpEnum = [newFiles objectEnumerator];
NSMutableString * buildUpStr = [NSMutableString stringWithString:@""];
while (file = [buildUpEnum nextObject]) {
[buildUpStr appendString: file];
[buildUpStr appendString:@"\n"];
}
[myObj setMyFilesToCopy:buildUpStr];
// Now that we have all the files that need to be copied in newFiles,
and a string of them in buildUpStr, lets get to copying.
NSEnumerator * copyEnum = [newFiles objectEnumerator];
NSMutableString * sourceToCopy = [NSMutableString stringWithString:@""];
NSMutableString * destToCopy = [NSMutableString stringWithString:@""];
BOOL isDir;
//int counter = 0;
[progressOutlet setMaxValue: [newFiles count]];
[progressOutlet setMinValue: 1];
[progressOutlet setDoubleValue:1];
while (file = [copyEnum nextObject]) {
sourceToCopy = [NSMutableString stringWithFormat:@"%@/%@",srcPath,file];
destToCopy = [NSMutableString stringWithFormat:@"%@/%@",destPath,file];
if ([fm fileExistsAtPath:sourceToCopy isDirectory:&isDir] && isDir) {
[fm createDirectoryAtPath: destToCopy attributes:nil];
} else
{
[fm copyPath:sourceToCopy toPath:destToCopy handler:nil];
}
[progressOutlet incrementBy:1];
// don't need this at all..
// counter++;
// NSNumber * myCounter = [NSNumber numberWithInt:counter];
// NSString * myProgresss = [NSString stringWithString:[myCounter stringValue]];
} // while
// Now that's finished copying..
[myObj setMyFilesToCopy:@"Finished"];
} // method
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden