discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copying files with special characters


From: address@hidden
Subject: Re: Copying files with special characters
Date: Tue, 9 Sep 2008 12:17:53 -0700 (PDT)
User-agent: G2/1.0

On 9 Sep., 19:43, Andreas Höschler <ahoe...@smartsoft.de> wrote:
> Dear all,
>
> I would like to programmatically copy files with
>
>     system([[NSString stringWithFormat:@"cp -r %@ %@", sourcePath,
> destPath] cString]);
>
> or alternatively using NSFileManager. This works as long as sourcePath
> does not contain special characters like ä,ö,ü,...
>
> In a terminal shell I can successfully copy such a file by typing
>
>         cp "Germ
>
> and then using TAB to automatically complete the path to
>
>         cp "German Fa\314\210hrhaus.jpeg" /home/ahoesch/A00
>
> The question for me now is where this magic \314\210 stuff comes from
> and how I can do the conversion in my GNUstep app programmatically
> before building the copy command.
>
>     system([[NSString stringWithFormat:@"cp -r %@ %@", [sourcePath
> magicMethod], destPath] cString]);
>
> Hints are greatly appreciated!
>
> Thanks,
>
>    Andreas

Use

system([[NSString stringWithFormat:@"cp -r '%s' '%s'", [sourcePath
fileSystemRepresentation], [destPath fileSystemRepresentation]]
cString]);

This is NOT safe for file names that include quotes.

Please note the fileSystemRepresentation method. Unfortunately, this
appears not to be well known and most Obj-C programmers assume that
cString always returns a valid file name...

A different solution (which is not fileSystemRepresentation safe!):

NSTask allows to set the parameters individually, i.e.

task=[[NSTask alloc] init];
[task setPath:@"/bin/cp"];
[task setArguments:[NSArray arrayWithObjects:@"cp", @"-r", sourcePath,
destPath]];
[task launch];

-- hns


reply via email to

[Prev in Thread] Current Thread [Next in Thread]