Swift: Using "/" slash in filename with createDirectoryAtPath - swift

I have a folder called "My / Project". When I try to call createDirectoryAtPath I get two folders created "My " with a subfolder of " Project".
I have looked at how the terminal represents this:
/Users/currentuser/Documents/Projects/My\ \:\ Project
Here is my code:
let projectName = "My / Project"
let path:NSString = "/Users/currentuser/Documents/Projects/"
let fullPath:NSString = path.stringByAppendingPathComponent(projectName)
if (!NSFileManager.defaultManager().fileExistsAtPath(fullPath:NSString))
{
do
{
try NSFileManager.defaultManager().createDirectoryAtPath(fullPath:NSString, withIntermediateDirectories: true, attributes: nil)
}
catch
{
}
}
I have also tried :
projectName.stringByReplacingOccurrencesOfString("/", withString: "\:")
to match the terminal but Xcode complains about an invalid escape sequence.
Update #1: Encoding the folder name also failed to work.
let encodedPath = projectName.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLUserAllowedCharacterSet())
let fullPath2:NSString = path.stringByAppendingPathComponent(encodedPath!)
NSFileManager.defaultManager().fileExistsAtPath(encodedPath!)
What is the best way of doing this?

The slash is the path delimiter in the file system, and not allowed
in file name components.
The OS X Finder however allows file names with a slash, and that works
by translating between the slash "/" for displayed file names and the colon ":" in the file system. (As a consequence, you cannot use the colon
for file names in the Finder.)
The folder "My / Project" is therefore stored in the file system as "My : Project", and replacing "/" in the file name with an unescaped colon ":" should
solve your problem.
(The colon has a special meaning in the shell, and that is why you see
\: in the Terminal.)

Related

How to make the platform independent filepath slash "/" with GLib? (GJS)

I have a folderPath which has a directory string:
/home/bastian/Pictures
and I have a variable fileName which contains the name.
I can concatenate the two strings together like this, but it only works on UNIX systems:
let filePath = folderPath + '/' + fileName;
Is there a way with GLib I can concatenate the two to each other without making assumptions about the slash or backslash (to stay fx Windows-compatible)?
With help from guadec, I found out I could use GLib's g_build_filenamev () function.
let filePath = GLib.build_filenamev([folderPath, fileName]);
This builds a path to the file and respects the platform at the same time.
Note: it requires that you import GLib first at the top of your GJS file, like this:
const { GLib } = imports.gi;
If you happen to be using a Gio.File object to manipulate the path, you can also do something like this:
const folder = Gio.File.new_for_path(folderPath);
const file = folder.get_child(fileName);

AKAudioFile exportAsynchronously path errors

I'm trying to use AKAudioFile.exportAsynchronously to convert wav to m4a (based on the sample code here: https://audiokit.io/playgrounds/Playback/Exporting%20Files/). I've chosen .documents as my BaseDirectory, but I just keep getting directory <my_dir> isn't valid errors — e.g.:
AKAudioFile+ProcessingAsynchronously.swift:exportAsynchronously(name:baseDir:exportFormat:fromSample:toSample:callback:):379:ERROR AKAudioFile export: directory "/var/mobile/Containers/Data/Application/20C913AD-B2F4-4F26-AAD2-0DFA0C65A886/Documents/All Of Me.mp4" isn't valid
That URL looks completely reasonable, to me, so what's up?
Okay, following #jake's tip, the solution was to handle the spaces explicitly before passing into AKAudioFile's exportAsynchronously(name:baseDir:exportFormat:callback:). I just did:
var name = String(cafURL.lastPathComponent.split(separator: ".")[0])
name = name.replacingOccurrences(of: " ", with: "%20")
let exportFile = try AKAudioFile(readFileName: "\(name).wav", baseDir: .documents)
exportFile.exportAsynchronously(name: name, baseDir: .documents, exportFormat: .m4a, callback: self.callback)

Ionic 3 Cordova File plugin gives error for copyFile operation

I am trying to copy a file from one dir to another using the copyFile(path, fileName, newPath, newFileName) function. It gives an error like {"code":13, "message":"input is not a directory"}. The documentation has only 12 error code and no 13th. I'd like to know what i did wrong please.
Here is a sample of my actual code.
this.path = "file:///storage/emulated/0/TheFolder/thefile.ext";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, fileName, this.newPath, newFileName)
this.path must be a directory but your are showing some file name
change your code as follows
this.path = "file:///storage/emulated/0/TheFolder";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, YOUR_EXISTING_FILE_NAME, this.newPath, NEW_FILE_NAME);
path -Base FileSystem
fileName - Name of file to copy
newPath - Base FileSystem of new location
newFileName - New name of file to copy to (leave blank to remain the same)

C# read from a Word document

I'm trying to read from a Word document and I want the computer to tell me what is written in document not to write itself in other place. So when I say the keyword "word" my program should open a dialog menu and let me to select a word file and tell me what is inside. The other keywords work. So here's my code and also my error.
case "word":
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object readFromPath = null;
Document doc = app.Documents.Open(ref readFromPath);
foreach (Paragraph objParagraph in doc.Paragraphs)
ss.SpeakAsync(objParagraph.Range.Text.Trim());
((_Document)doc).Close();
((_Application)app).Quit();
}
And my error is enter image description here
Application.Documents.Open takes the full path and filename.
The path must end with \ and prefix the string with # (or leave out the # and double the backslashes \ as one backslash is considered to be an escape character)
object readFromPath = #"C:\Users\N.Horatiu\Desktop\s.docx"
Document doc = app.Documents.Open(ref readFromPath);

How can I get the project path in Scala?

I'm trying to read some files from my Scala project, and if I use: java.io.File(".").getCanonicalPath() I find that my current directory is far away from them (exactly where I have installed Scala Eclipse). So how can I change the current directory to the root of my project, or get the path to my project? I really don't want to have an absolute path to my input files.
val PATH = raw"E:\lang\scala\progfun\src\examples\"
def printFileContents(filename: String) {
try {
println("\n" + PATH + filename)
io.Source.fromFile(PATH + filename).getLines.foreach(println)
} catch {
case _:Throwable => println("filename " + filename + " not found")
}
}
val filenames = List("random.txt", "a.txt", "b.txt", "c.txt")
filenames foreach printFileContents
Add your files to src/main/resources/<packageName> where <packageName> is your class package.
Change the line val PATH = getClass.getResource("").getPath
new File(".").getCanonicalPath
will give you the base-path you need
Another workaround is to put the path you need in an user environmental variable, and call it with sys.env (returns exception if failure) or System.getenv (returns null if failure), for example val PATH = sys.env("ScalaProjectPath") but the problem is that if you move the project you have to update the variable, which I didn't want.