PDA

View Full Version : Need advice on this code for copying a file




trey5498
Jun 19, 2008, 09:35 AM
I have decided to use the FileManager built in with Xcode for the checking and coping of files.

Will this code work correctly? I ask here because I haven't gotten that far in the gui to test it and I took a break from that frustration to work on the behind the sceens.


NSString *source = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz";
NSString *tmpsource = @"/tmp/HP DesignJet 1055CM PS3.gz";

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:source];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsource toPath:@"/Library/Printers/PPDs/Contents/Resources/" handler:nil];
}



I also have this for the next button that will open the new window and close the current window. However the interface building will only let me have one window action for the button though:


- (IBAction)NextButton:(ID)sender {
[prnSelector performClose:self];
[prnOverview orderFront:self];
}


What did I do wrong on the new window and will this prvent me from calling another sub that will check the checkboxes and placing them in an array?

and is the following coded correctly:


if([checkboxname isEqualToString:@"Printer1"] == YES) {

printerarray[i] = Printer1;
} else if{
the other 17 checkboxes
}



robbieduncan
Jun 19, 2008, 10:12 AM
NSString *source = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz";
NSString *tmpsource = @"/tmp/HP DesignJet 1055CM PS3.gz";

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:source];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsource toPath:@"/Library/Printers/PPDs/Contents/Resources/" handler:nil];
}


This won't do what you expect. The toPath: has to be the complete path you want to copy the file to including the filename, not just the directory you want to copy into.

trey5498
Jun 19, 2008, 10:56 AM
[fileManager copyPath:tmpsource toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz" handler:nil];


would that work and do what I need it to?

What about the other questions.

robbieduncan
Jun 19, 2008, 10:58 AM
[fileManager copyPath:tmpsource toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz" handler:nil];


would that work and do what I need it to?

What about the other questions.

Should do as long as fileManager is valid. You don't seem to have declared it or initialised it anywhere...

trey5498
Jun 19, 2008, 11:31 AM
Crap,

didnt notice where to declare it.

How do I declare the FileManager?

robbieduncan
Jun 19, 2008, 11:36 AM
Assuming that it's meant to be a pointer to the global NSFileManager object then


NSFileManager *fileManager = [NSFileManager defaultManager];


If you do it above the bit where you check the existence of the file then you can use the variable in place of the [NSFileManager defaultManager] code you currently have too.

trey5498
Jun 19, 2008, 02:37 PM
So this will work correctly? I just want to make sure.




//
// DvrChk.m
// Circa Printer Installer
//
// Created by treyb on 6/19/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "DvrChk.h"


@implementation DvrChk
NSFileManager *fileManager = [dvrchk];
NSString *sourceplotter1 = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz";
NSString *sourcepplotter2 = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet T1100ps 44in.gz";
NSString *sourcebw = @"/Library/Printers/PPDs/Contents/Resources/HP LaserJet 4250.gz";
NSString *sourcecolor = @"/Library/Printers/PPDs/Contents/Resources/Xerox Phaser 7700GX.gz";

NSString *tmpsourcep1 = @"/tmp/HP DesignJet 1055CM PS3.gz";
NSString *tmpsourcep2 = @"/tmp/HP DesignJet T1100ps 44in.gz";
NSString *tmpsourcebw = @"/tmp/HP LaserJet 4250.gz";
NSString *tmpsourcec = @"/tmp/Xerox Phaser 7700GX.gz";

- (void)Plotter1
{

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:sourceplotter1];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcep1 toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz" handler:nil];
}

}

- (void)Plotter2
{

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:sourceplotter2];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcep2 toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet T1100ps 44in.gz" handler:nil];
}

}

- (void)BWQ
{

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:sourcebw];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcebw toPath:@"/Library/Printers/PPDs/Contents/Resources/HP LaserJet 4250.gz" handler:nil];
}

}

- (void)Color
{

dvrchk = [[NSFileManager defaultManager] fileExistsAtPath:sourcecolor];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcec toPath:@"/Library/Printers/PPDs/Contents/Resources/Xerox Phaser 7700GX.gz" handler:nil];
}

}
@end

robbieduncan
Jun 19, 2008, 02:45 PM
What is


NSFileManager *fileManager = [dvrchk];


meant to be doing? This makes no sense to me...

trey5498
Jun 19, 2008, 02:48 PM
Assuming that it's meant to be a pointer to the global NSFileManager object then


NSFileManager *fileManager = [NSFileManager defaultManager];


If you do it above the bit where you check the existence of the file then you can use the variable in place of the [NSFileManager defaultManager] code you currently have too.

Was attempting to use where you said I could use the variable. I get a weird token error before the ']' and it also gave me that dvrchk was not declared however when I delcare it as:


BOOL *dvrchk;



I get a warning about variable without a cast. I that line where I declared the filemaneger seems to not be correct.

However I may have the wrong variable in that line that you were talking about. I now believe you meant change the variable in the lower code to:


dvrchk = [[fileManager] fileExistsAtPath:sourceplotter1];


However when that is changed I still get the "error:syntax error before ']' token", two of them at the NSFileManager *fileManager = [NSFileManager defaultManager]; line and I get two "warning: assignment makes pointer from integer without a cast at the dvrchk = [[fileManager] fileExistsAtPath:sourceplotter1]; line

robbieduncan
Jun 19, 2008, 02:54 PM
Right lets look at this.


BOOL *dvrchk;


Declares a pointer to a BOOL. At this stage it's not pointing at anything. It can only be used to point at a BOOL, not an int, not an NSString, not an NSFileManager etc.


[dvrchk]


is illegal syntax. It is the correct syntax to send a message to an object pointed to by dvrchk, but no message is specified and dvrchk can't point to an object anyway as it's declared as a pointer to a BOOL.

Here's a version that at least makes sense and will work assuming that you have other code to call this


//
// DvrChk.m
// Circa Printer Installer
//
// Created by treyb on 6/19/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "DvrChk.h"


@implementation DvrChk
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *sourceplotter1 = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz";
NSString *sourcepplotter2 = @"/Library/Printers/PPDs/Contents/Resources/HP DesignJet T1100ps 44in.gz";
NSString *sourcebw = @"/Library/Printers/PPDs/Contents/Resources/HP LaserJet 4250.gz";
NSString *sourcecolor = @"/Library/Printers/PPDs/Contents/Resources/Xerox Phaser 7700GX.gz";

NSString *tmpsourcep1 = @"/tmp/HP DesignJet 1055CM PS3.gz";
NSString *tmpsourcep2 = @"/tmp/HP DesignJet T1100ps 44in.gz";
NSString *tmpsourcebw = @"/tmp/HP LaserJet 4250.gz";
NSString *tmpsourcec = @"/tmp/Xerox Phaser 7700GX.gz";

- (void)Plotter1
{

dvrchk = [fileManager fileExistsAtPath:sourceplotter1];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcep1 toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet 1055CM PS3.gz" handler:nil];
}

}

- (void)Plotter2
{

dvrchk = [fileManager fileExistsAtPath:sourceplotter2];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcep2 toPath:@"/Library/Printers/PPDs/Contents/Resources/HP DesignJet T1100ps 44in.gz" handler:nil];
}

}

- (void)BWQ
{

dvrchk = [fileManager fileExistsAtPath:sourcebw];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcebw toPath:@"/Library/Printers/PPDs/Contents/Resources/HP LaserJet 4250.gz" handler:nil];
}

}

- (void)Color
{

dvrchk = [fileManager fileExistsAtPath:sourcecolor];

if (dvrchk == NO)
{
[fileManager copyPath:tmpsourcec toPath:@"/Library/Printers/PPDs/Contents/Resources/Xerox Phaser 7700GX.gz" handler:nil];
}

}
@end


Note that you need to change the declaration of dvrchk to


BOOL dvrchk;


It doesn't need to be a pointer, simply a BOOL.

trey5498
Jun 19, 2008, 03:06 PM
NSFileManager *fileManager = [NSFileManager defaultManager];


Yields an error: "error: initializer element is not a constant

And BTW, Thank you ever so much for your help and patience ;)

robbieduncan
Jun 19, 2008, 03:09 PM
NSFileManager *fileManager = [NSFileManager defaultManager];


Yields an error: "error: initializer element is not a constant

And BTW, Thank you ever so much for your help and patience ;)

Probably because it's just inline. Should be in an init method really:


@implementation DvrChk
NSFileManager *fileManager;

-(id) init
{
if ([super init])
{
fileManager = [NSFileManager defaultManager];
}
return self;
}

trey5498
Jun 19, 2008, 03:17 PM
That worked. Thank you again for your patience for my ignorance.

Know any good sites for tutorials or code examples that I can rip apart to learn a little more on my own?

robbieduncan
Jun 19, 2008, 03:21 PM
Cocoadevcentral (http://www.cocoadevcentral.com/) has lots of examples including basic Cocoa (init methods etc)
Cocoadev (http://www.cocoadev.com/) has metric tones of code!

trey5498
Jun 19, 2008, 03:23 PM
Thank you again :)