问题
I'm using a program which uses the type
command to pipe contents of a file to another program like this:
type test.txt | (another program)
This command is hardcoded in my main program. The problem is that the type
command has problems with forward slashes:
E:\babak\git\bin>type e:/babak/git/bin/test.txt
The syntax of the command is incorrect.
Is there a way to override the cmd internal type
with a custom application named type
, which supports forward slashes, so if someone calls type
in cmd, the custom type
application is called?
回答1:
The TYPE internal command will work fine with quotes if you enclose the argument in quotes.
type "e:/babak/git/bin/test.txt"
You can prevent use of an internal command and force use of a batch or exe if you quote the command:
"type" e:/babak/git/bin/test.txt
You can also include path info to force use of an exe or batch
E:\babak\git\bin\type e:/babak/git/bin/test.txt
or
.\type e:/babak/git/bin/test.txt
But if you do not have control over how the program invokes TYPE, then I don't see how you can use any of the above techniques.
The only other option I can think of is to preprocess your path info to convert the forward slashes into backslashes. Then you need to make sure your program gets the modifed path info.
回答2:
You can use doskey to override internal cmds in command.com, e.g. on win7
c:\> date
The current date is: Mon 09/18/2017
Enter the new date: (mm-dd-yy)
c:\> @doskey date=c:\cygwin\bin\date.exe $*
c:\> @doskey echo=c:\cygwin\bin\echo.exe $*
c:\> date +%Y-%m-%d
2017-09-18
回答3:
cmd.exe is just a program that resides in the System32 (or whatever) directory. If you replace it with a new cmd.exe then that one will be run instead whenever a user runs cmd.
There will be permission issues to be solved before replacing the file, but not impossible.
来源:https://stackoverflow.com/questions/13321506/overriding-windows-cmd-internal-commands