admin管理员组文章数量:1356442
I have a PowerShell script OpenFile.ps1
# path to notepad++
$notepadPlusPlusPath = "C:\Program Files\Notepad++\notepad++.exe"
# Because the path contains whitespaces "`"$filePath`""
$filePath = "`"C:\Users\Path with spaces\Output.xml`""
# open file in notepad++
Start-Process -FilePath $notepadPlusPlusPath -ArgumentList $filePath
Which I can run successfully with
script_path="C:\\Users\\OpenFile.ps1"
system2("powershell", args = c("-File", shQuote(script_path)))
But the file which should be opened I want to pass as an argument. So the script is transferred to a function named OpenFile_func.ps1
function OpenFile {
param($filePath)
$notepadPlusPlusPath = "C:\Program Files\Notepad++\notepad++.exe"
Start-Process -FilePath $notepadPlusPlusPath -ArgumentList $filePath
}
Which runs without an error message...
func_path="C:\\Users\\OpenFile_func.ps1"
file_path="C:\\Users\\Output.xml"
system2("powershell", args = c("-File", shQuote(func_path), "-ArgumentList", shQuote(file_path)))
... but notepad++ with the required file doesn't open.
What's missing in my code so that Notepad++ with the desired file opens?
I have a PowerShell script OpenFile.ps1
# path to notepad++
$notepadPlusPlusPath = "C:\Program Files\Notepad++\notepad++.exe"
# Because the path contains whitespaces "`"$filePath`""
$filePath = "`"C:\Users\Path with spaces\Output.xml`""
# open file in notepad++
Start-Process -FilePath $notepadPlusPlusPath -ArgumentList $filePath
Which I can run successfully with
script_path="C:\\Users\\OpenFile.ps1"
system2("powershell", args = c("-File", shQuote(script_path)))
But the file which should be opened I want to pass as an argument. So the script is transferred to a function named OpenFile_func.ps1
function OpenFile {
param($filePath)
$notepadPlusPlusPath = "C:\Program Files\Notepad++\notepad++.exe"
Start-Process -FilePath $notepadPlusPlusPath -ArgumentList $filePath
}
Which runs without an error message...
func_path="C:\\Users\\OpenFile_func.ps1"
file_path="C:\\Users\\Output.xml"
system2("powershell", args = c("-File", shQuote(func_path), "-ArgumentList", shQuote(file_path)))
... but notepad++ with the required file doesn't open.
What's missing in my code so that Notepad++ with the desired file opens?
Share Improve this question edited Mar 31 at 22:34 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 31 at 13:46 tueftlatueftla 4991 gold badge4 silver badges20 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2When there are no spaces in the paths
Without changing your OpenFile_func.ps1
you may dot-source the function and then run it within system2 like
func_path="C:\\Users\\OpenFile_func.ps1"
file_path="C:\\Users\\Output.xml"
system2("powershell", args = c("-Command", paste0(". ", shQuote(func_path), "; OpenFile ", shQuote(file_path))))
On Windows you can use shell.exec
shell.exec
will open your .xml
directly with your standard xml editor
without the need of an extra function - it allows for spaces in the path.
file_path <- "C:\\Users\\Path with spaces\\Output.xml"
shell.exec(file_path)
Best practice use processx
As mentioned by SamR, system2
and by extend system
don't quote their arguments which is why you can't pass paths with spaces " " to them. So as SamR suggested, you may use processx
library(processx)
run("C:\\Program Files\\Notepad++\\notepad++.exe",
args = c("C:\\Users\\Path with spaces\\Output.xml"))
本文标签: Open a file in notepad via a PowerShell function with the r function system2() failsStack Overflow
版权声明:本文标题:Open a file in notepad++ via a PowerShell function with the r function system2() fails - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743944073a2566081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
OpenFile
is fine, but you need to call it afterwards (eg.OpenFile -filePath $args[0]
on the last line ofOpenFile_func.ps1
) - otherwise nothing will happen – Mathias R. Jessen Commented Mar 31 at 15:25OpenFile -filePath $args[0]
as last line, notepad++ opens a file named-ArgumentList
. – tueftla Commented Mar 31 at 16:16-ArgumentList
, you can pass arguments directly to the invoked file:system2("powershell", args = c("-File", shQuote(func_path), shQuote(file_path)))
– Mathias R. Jessen Commented Mar 31 at 16:31shQuote()
that's no problem. But something strange happens to my path: From "C:\\Path To File\\With Spaces\\And So On\\Output.xml" the system makes a path like "C:\\File\\Spaces\\On" - the parts Path To, With and And So are cut off. Do you have an idea how to fix it? – tueftla Commented Mar 31 at 19:14