File copy issues [non-Xojo related)

I have an app (Swift) that stores image file paths in a database. At some point it needs to copy all of those images to a central location.

I have tried everything and keep getting strange errors

here is the relavant part of the code

print("SRC=\(srcURL)")
print("DST=\(dstURL)")
do {
   try fileMANAGER.copyItem(at: srcURL, to: dstURL)
} catch let error as NSError { print(error)}

the output is

SRC=file:///Volumes/Drive%20#1/SWIFT-Projects/Swift_Framework2021/extractGUI/morgan.png

NEW=file:///Volumes/Drive%20%231/SWIFT-Projects/Swift_Framework2021/extractGUI/simple%20SWIFT/Assets.xcassets/morgan.imageset/morgan.png

Error Domain=NSCocoaErrorDomain Code=260 "The file “Drive ” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Volumes/Drive , NSUnderlyingError=0x600000978c90 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

both Src and Dst are URLs, but the strange thing is the error

 "The file “Drive ” couldn’t be opened

uh… what happened to the rest of the path, “copyItem” is a macOS function, not something I wrote

The # character in the first URL should be encoded using %23, as it’s considered an “unsafe” character for URLs. Notice how the second URL does use %23.

I solved this by using NSURL then converting to URL… I don’t have the luxury of altering the path names as they are provided by the FileManager API…

Looks like what happens when quotes are omitted in a command line.
I know you have it sorted but I wonder if making the path up with quotes would have worked?

like
SRC= chr(34) + “file:///Volumes/Drive A/SWIFT-Projects/Swift_Framework2021/…etc”

You can uses quotes on the Terminal command line… but not on an API call (or it didn’t seem to work)

Using NSURL properly escaped things… the problem had only been with the Source, since it was coming from a string read from a database.

1 Like