Jak mogę przekierować STDOUT do argumentu przyrostka w cmd?


0

Chciałbym osiągnąć następujące (pseudokod):

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

w jednej linii od cmd konsola w systemie Windows 7:

where cmd | cd %there%\..

Wymaga to również ścieżki do folderu nadrzędnego cmd jest ekstrahowany, jak where wskazuje na plik wykonywalny.

„cmd” to tylko symbol zastępczy. Może to być „java”, „python”, „kropka” lub dowolne inne narzędzie PATH.

Do tej pory udało mi się przejść do schowka:

where $path:java | clip

W tym miejscu utknąłem, ponieważ nie potrafię wymyślić sufiksu zamiast przedrostka. Przeczytałem o operatorach takich jak &1> i >>, ale nie uważałem ich za pomocne. Domyślam się pewnego rodzaju zmiennych pozycyjnych i używania & byłoby jedno rozwiązanie, ale może nie tak eleganckie ...

Odpowiedzi:


1

… W jednej linii od cmd konsola w systemie Windows 7 :

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

Powyżej polecenia przedstawia wyniki; aby zmienić bieżący katalog, możesz użyć

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

Wyjaśnienie w Argumenty wiersza poleceń (parametry) lub w 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.

Inne podejście wymaga nazwy pliku w tym rozszerzenie :

for %G in (cmd.exe) do @echo %~$PATH:G
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.