I am using appjs * and I want to execute a command to open a folder.
What I have
var path = __dirname + '/folder to open/';
// path = C:\Program Files\myapplication/folder to open/
require("child_process").exec("start " + path);
Error
Could not find file C:\Program
What I tried
I already tried to escape the spaces, that didn't work.
var path = __dirname + '/folder to open/';
path = path.replace(' ', '\ ');
// path = C:\Program Files\myapplication/folder to open/
require("child_process").exec("start " + path);
When I put the path between quotes, No folder is opened, only another prompt.
var path = "\"" + __dirname + "/folder to open/\"";
path = path.replace(' ', '\ ');
// path = "C:\Program Files\myapplication/folder to open/"
require("child_process").exec("start " + path);
Related bug https://github.com/isaacs/npm/pull/2479
Does anyone has a fix or a workaround?
* link removed
To open a path than contains spaces, you must replace with a double backslash.
In your code you escaped the space character:
"\ "
What you need to do is escape the backslash character so it makes it into the output string:
"\\ "
Try this:
var path = __dirname + '/folder to open/';
// Notice the double-backslashes on this following line
path = path.replace(/ /g, '\\ ');
require("child_process").exec("start " + path);
Well, I fixed it.
Or something like it.
Instead of using
"start " + path
I used
"%SystemRoot%\\explorer.exe \"" + path + "\""
Notice the quotes and the forward slashes.
In my case it is fixed by adding double quotes for the path except the first drive name or letter.
import * as path from 'path'; // npm module
const filePath = 'C:/Program Files/my application/file to open/test.txt';
const rootName = path.parse(filePath).root; // "C:/"
const filePathTo = `${rootName}"${filePath.replace(rootName, '')}"`; // C:/"Program Files/my application/file to open/test.txt"
require("child_process").exec(`start ${filePathTo}`);
The text file will be opened.
this works for me
f= file.replace(/ /g,"\\\ ")
You can also use the old-style 8-character-max/no-space names for each path.
The one I always used was always coding c:\PROGRA~1 instead of c:\Program Files, although this only works on english systems.
If the first 8 characters of any path with more chars are unique, I expect you can do something like newPath = origPath.sub(/\W/g, '').substr(0, 6) + "~1"
Don't have a windows system here, just going by memory.
Related
I wrote a script in Matlab on windows system. Now I changed to linux system and I tried to use my script on linux in matlab also. But it does not work.
I got a problem with the data import part of my script:
data = {};
for i = 1:numel(filelist)
filename = filelist{i};
filename = [selpath '\' filename];
delimiter = ',';
startRow = 2;
formatSpec = '%*q%*q%*q%q%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
tmp_data = str2double(dataArray{1});
data{i} = tmp_data;
end
If I run my script I got the following error from matlab:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in justus_tem (line 21)
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError',
false, 'EndOfLine', '\r\n');
When I run the same script on windows I do not get the Error. In linux system the fileID is always -1
Has somebody a tip or knows what I do wrong? I tried different permissions for fopen but it does not work either.
Linux uses a forward slash (/) as the file separator, you have hard-coded the Windows compatible backward slash (\). Instead, consider either
Using filesep for a system-dependent file separator
filename = [selpath, filesep, filename];
Or use fullfile to build the path for you
filename = fullfile(selpath, filename);
I am working on a program that clicks a button when it changes color, and at the end of the program, it screenshots the webpage, saves the screenshot, and then moves it to a different directory. Everything seems to be working fine except for moving the file into a different folder. The code I am using to move the file is here:
os.replace("'\\'" + fileName, "'\\'" + saveName + "'\\'" + fileName)
I get the error:
FileNotFoundError: [WinError 3] The system cannot find the path specified: "'\'0.png" -> "'\'saves216'\'0.png"
I don't know how to get the backslash to escape without becoming a double backslash
Remove the extra quotes:
os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)
You directly escape a \ with another one:
>>> s = "\\" + "filename"
>>> print(s)
\filename
os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)
extra '' in yours
change the filename variable too if it has ''
In an application, I can get the path to a file which resides in a directory as a string:
"/path/to/the/file.txt"
In order to write another another file into that same directory, I want to change the string "/path/to/the/file.txt" and remove the part "file.txt" to finally only get
"/path/to/the/"
as a string
I could use
string = "/path/to/the/file.txt"
string.split('/')
and then glue all the term (except the last one) together with a loop
Is there an easy way to do it?
You can use os.path.basename for getting last part of path and delete it with using replace.
import os
path = "/path/to/the/file.txt"
delete = os.path.basename(os.path.normpath(path))
print(delete) # will return file.txt
#Remove file.txt in path
path = path.replace(delete,'')
print(path)
OUTPUT :
file.txt
/path/to/the/
Let say you have an array include txt files . you can get all path like
new_path = ['file2.txt','file3.txt','file4.txt']
for get_new_path in new_path:
print(path + get_new_path)
OUTPUT :
/path/to/the/file2.txt
/path/to/the/file3.txt
/path/to/the/file4.txt
Here is what I finally used
iter = len(string.split('/'))-1
directory_path_str = ""
for i in range(0,iter):
directory_path_str = directory_path_str + srtr.split('/')[i] + "/"
I am trying to split a path into parent and the name.
When trying
String path = "/root/file"
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)
We get
Name: file
Parent: /root
With the Windows path C:\\root\\file.exe we get
Name: C:\root\file.exe
Parent: null
Is this the intended behaviour? And if it is how do I get the same result for a Windows path? (If possible please without using regular expressions)
use .replace to change the "\" to "/
String path = "C:\\root\\file.exe"
path = path.replace("\\","/")
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)
I've written a vanilla server (using Node.js and Express) to browse files and directories (based off the directory middleware). On a Windows machine, it gets confused by the backslashes, and quickly breaks by providing invalid/broken links as you navigate -- links becomes a soup of forward and backslashes, with dir names incorrectly repeated over and over in them, etc.
So or example, going to localhost:8888, clicking on 'lib' folder, then '..', gives me:
/ \lib / \\lib\..\ /
Here's the code.
var express = require('express');
var server = express();
server.configure(function(){
server.use(express.static('./stuff'));
server.use(express.directory('./stuff'));
});
server.listen(8888);
What do I need to do to get this to work on a Windows machine?
#verybadalloc and #user2524973 found a bug in the express directory.js middleware that caused this problem. In the comments to the original question they also provided a fix.
Find the lines that says
return '<li><a href="' + join(dir, file) + '" class="' + classes.join(' ') + '"' + ' title="' + file + '">'
It should be around line number 176. Change it to:
return '<li><a href="' + join(dir, file).replace(/\\/g, '/') + '" class="' + classes.join(' ') + '"' + ' title="' + file + '">'
... and it should work better on Windows.
It seems like this fix was added to the main repository, but later removed, so this fix might have some unforeseen consequence? Works for me anyway, I'm just using it to list my music folder.
I posted this answer since I almost missed that they had solved it in the comments. Thanks.