如何将 STDOUT 重定向到 cmd 中的后缀参数?

如何将 STDOUT 重定向到 cmd 中的后缀参数?

我想实现以下(伪代码):

set a = where cmd
cd %a%\..

cmd在 Windows 7 的控制台中只需一行:

where cmd | cd %there%\..

这还要求提取父文件夹的路径cmdwhere指向可执行文件。

“cmd” 只是一个占位符。它可以是“java”、“python”、“dot”或 PATH 上的任何其他实用程序。

到目前为止我已经能够将内容传输到剪贴板:

where $path:java | clip

这就是我被困住的地方,因为我不知道如何将管道连接到后缀而不是前缀。我读过有关诸如&1>and 之类的运算符的文章>>,但我发现它们没什么用。我猜某种位置变量和使用&可能是一种解决方案,但可能不那么优雅……

答案1

cmd在 Windows 7 的控制台中,只需一行即可

for /F "delims=" %G in ('where cmd') do @echo %~dpG

以上命令仅仅节目结果;要更改当前目录,您可以使用

for /F "delims=" %G in ('where cmd') do @CD /D "%~dpG"

解释命令行参数(参数)for /?

… In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

另一种方法需要文件名包括扩展

for %G in (cmd.exe) do @echo %~$PATH:G

相关内容