Tobi Weißhaar
Tobi Weißhaar

Reputation: 1677

Objective-C - How to convert file path to shell conform file path

I want to execute a shell command (I want to touch a file). I use system("shell command") to execute the command.

For example I want to touch the file at path /Users/username/New Folder/. Now I need to convert the NSString in a format that is conform to shell commands like /Users/username/New\ Folder.

Is there any method that does a conversion like this?

NOTE: It is NOT just replacing a whitespace with \. If you have a special character in the path like /Users/username/Folder(foo)/ the "shell path" looks like this /Users/username/Folder\(foo\)/

Upvotes: 0

Views: 74

Answers (3)

that other guy
that other guy

Reputation: 123400

Don't use system. It's insecure and unpredictable. Surrounding the string with quotes is not sufficient.

Use the execve style functions instead. They are simple and secure.

Upvotes: 0

Merlevede
Merlevede

Reputation: 8170

You can enclose the parameters that contain spaces with " " marks.

touch "/Users/username/New Folder/"

At least this works at the shell prompt

Upvotes: 1

CRD
CRD

Reputation: 53000

There is no need to convert the path, you can surround it in single quotes. Just use:

touch 'path'

Upvotes: 1

Related Questions