问题
I tend to have a lot of cmd prompts open at a time from running multiple batch files at once, so I figured the new Windows Terminal would be a neat way to tidy things up with tabs.
Problem is, I can't quite figure out how to change the default behavior of running batch files through a cmd instance to the desired running batch files through a cmd instance that is inside of a Windows Terminal instance.
I'm able to easily open Windows Terminal, start a cmd instance, cd to the correct dir, and run the batch file.. but that's a lot of effort. I want to just be able to run a batch file, and have it work through a cmd prompt that's inside Windows Terminal.
回答1:
You can have this behaviour on double click by changing HKCR\batfile\shell\open\command
default value from:
"%1" %*
to:
"C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*
or by using ftype
command:
ftype batfile="C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*
You have to change <user>
with the current user name directory and of course, this wt.exe
path (C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\
) is if you've installed Windows Terminal from Microsoft Store, if you haven't, then you have to find the path to wt.exe
and use that instead. Windows enviroment variables %LOCALAPPDATA%
and %USERPROFILE%
did not work for me, so I'm using full path here.
You can create a registry entry to have an option to opem a BAT
file using Windows Terminal and having it running with CMD
:
- Open
regedit
and navigate toHKEY_CLASSES_ROOT\batfile\shell
key. Create a key with the name
wtopen
and the default valueOpen with Windows Terminal
:- Create a subkey
HKEY_CLASSES_ROOT\batfile\shell\wtopen\command
with the default value:"C:\Users\<user>\AppData\Local\Microsoft\WindowsApps\wt.exe" -p "Command Prompt" "%1" %*
And now you'll have a new entry "Open with Windows Terminal" when you right click on BAT
files and when you click it it will open a new Windows Terminal with a Command Promt panel running the selected batch file:
Tested with Windows Terminal 0.11.1121.0
The default name for the Command Promt (cmd
) profile is "Command Promt". You'll have to change it using the -p "<name>"
parameter if you have other name than the default.
来源:https://stackoverflow.com/questions/56191386/how-can-i-get-batch-files-to-run-through-the-new-windows-terminal