I can't save file with slash in file name. I download file, and if file has slash in the name, it doesn't want to save. For example full name of the song: "H/F ArtistName - Song name.mp3". Is it possible to save files with slash in name? Or how to properly replace slash?
Asked
Active
Viewed 2,397 times
3 Answers
2
From another post:
NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s); // => foobarbazfoo
1
/ is generally used as a separator between files or folders in the operating system. therefore the filename can not contain a slash as that would confuse the H in the name for a folder.
The best idea is to replace it with a whitespace or simple delete it to give: "HF ArtistName - Songname.mp3"

Delusional Logic
- 808
- 10
- 32
-
Try entering slash in filename on MacOSX. It does not matter what is used as separator until file components are properly escaped. – pronebird Dec 21 '15 at 14:40
-
The various Mac/Apple filesystems do not use slash ("/") as the separator, they use colon (":"). So a file or directory can contain slash but cannot contain colon. In the various interfaces (eg terminal, shells) slash, the POSIX directory separator, is used but can be escaped. – timeSmith Apr 18 '21 at 01:25
0
workaround:
Xcode 12.3, iOS 14
fileName.replacingOccurrences(of: "/", with: ":") // just have a try...
full demo code:
public extension Data {
func saveToTemporaryDirectory(fileName: String, completionHandler: @escaping (Result<URL, Error>) -> Void) {
var localURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
localURL.appendPathComponent(fileName.replacingOccurrences(of: "/", with: ":"))
do {
try self.write(to: localURL)
completionHandler(.success(localURL))
} catch {
completionHandler(.failure(error))
}
}
}

hstdt
- 5,652
- 2
- 34
- 34