简体   繁体   中英

How can I use Powershell to open a file with spaces in it in an auxiliary application, using a function?

Function npp {
    Param([String]$filepath)
    start 'D:\Program Files (x86)\Notepad++\notepad++.exe' &($filepath)
}

Function nteract {
    $file = $args[0]
    start 'D:\Program Files\nteract\nteract.exe' &($file)
}

I wrote two beginner functions for the purpose of recreating the much easier aliases in bash and fish. I have tried two ways of capturing a file argument as shown above. Neither of them work. Instead I receive the following.

Say I am opening the file '.\01 Getting started.ipynb' in nteract.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
7      Job7            BackgroundJob   Running       True            localhost            Microsoft.PowerShell.Man…
.\01. Getting started.ipynb

This displays in my console, and opens a default instance of nteract with an empty notebook. The same happens with Notepad++ with other files.

Insert a complaint here about how confusing it is to get this functionality working compared to Linux shells. What am I doing wrong?

-==-

EDIT: This question has been answered sufficiently, but I did notice strange behavior the first time I commented out the functions I wrote in my profile.

PS D:\julitory\JuliaBoxTutorials\introductory-tutorials\intro-to-julia> . $profile
PS D:\julitory\JuliaBoxTutorials\introductory-tutorials\intro-to-julia> nteract '.\02. Strings.ipynb'

Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------
5      Job5            BackgroundJob   Running       True            localhost
.\02. Strings.ipynb

For some reason, this occured the first time I commented them out. So I uncommented them, and it began to work again... then, the next time I commented them, they continued to work. I think I'm just too tired for this, but thanks all.

PowerShell has Set-Alias for that purpose. Define, for example:

Set-Alias -Name nteract -Value "D:\Program Files\nteract\nteract.exe"

Then use the alias as:

nteract ".\01 Getting started.ipynb"

Aliases defined this way are only available during the current PowerShell session. For ways to make them persist see How to create permanent PowerShell Aliases .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM