Overriding Windows cmd internal commands

泄露秘密 提交于 2020-01-14 19:09:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!