1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Need advice on this code for copying a file

Discussion in 'Mac Programming' started by trey5498, Jun 19, 2008.

  1. macrumors regular

    #1
    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.

    Code:
    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:

    Code:
    - (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:

    Code:
    if([checkboxname isEqualToString:@"Printer1"]  == YES) {
    
      printerarray[i] = Printer1;
    } else if{
    the other 17 checkboxes
    }
    
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    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.
     
  3. macrumors regular

    #3
    Code:
    [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.
     
  4. Moderator

    robbieduncan

    Staff Member

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

    #5
    Crap,

    didnt notice where to declare it.

    How do I declare the FileManager?
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    Assuming that it's meant to be a pointer to the global NSFileManager object then

    Code:
    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.
     
  7. macrumors regular

    #7
    So this will work correctly? I just want to make sure.



    Code:
    //
    //  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
    
    
     
  8. Moderator

    robbieduncan

    Staff Member

    #8
    What is

    Code:
    NSFileManager *fileManager = [dvrchk];
    
    meant to be doing? This makes no sense to me...
     
  9. macrumors regular

    #9
    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:

    Code:
    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:

    Code:
    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
     
  10. Moderator

    robbieduncan

    Staff Member

    #10
    Right lets look at this.

    Code:
    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.

    Code:
    [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

    Code:
    //
    //  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

    Code:
    BOOL dvrchk;
    
    It doesn't need to be a pointer, simply a BOOL.
     
  11. macrumors regular

    #11
    Code:
    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 ;)
     
  12. Moderator

    robbieduncan

    Staff Member

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

    Code:
    @implementation DvrChk
    NSFileManager *fileManager;
    
    -(id) init
    {
    if ([super init])
    {
      fileManager = [NSFileManager defaultManager];
    }
    return self;
    }
    
     
  13. macrumors regular

    #13
    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?
     
  14. Moderator

    robbieduncan

    Staff Member

    #14
    Cocoadevcentral has lots of examples including basic Cocoa (init methods etc)
    Cocoadev has metric tones of code!
     
  15. macrumors regular

    #15
    Thank you again :)
     

Share This Page