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
  • Neither system nor system2 quotes arguments so you won't be able to pass a filename with spaces in. Try processx. See stackoverflow/questions/77255792/… – SamR Commented Mar 31 at 14:30
  • Defining OpenFile is fine, but you need to call it afterwards (eg. OpenFile -filePath $args[0] on the last line of OpenFile_func.ps1) - otherwise nothing will happen – Mathias R. Jessen Commented Mar 31 at 15:25
  • @MathiasR.Jessen I'm trying and trying, but I'm not getting it to work. When I add your proposed line OpenFile -filePath $args[0] as last line, notepad++ opens a file named -ArgumentList. – tueftla Commented Mar 31 at 16:16
  • Remove -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:31
  • @MathiasR.Jessen When doing your approach notepad++ opens with an error saying Cannot open file because the path does not exist. I have spaces in my path and I thought because of shQuote() 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
 |  Show 1 more comment

1 Answer 1

Reset to default 2

When 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