【问题标题】:Is there an equivalent of 'which' on the Windows command line?Windows 命令行上是否有相当于“which”的功能?
【发布时间】:2010-09-23 04:33:55
【问题描述】:

由于我有时会遇到路径问题,其中我自己的一个 cmd 脚本被另一个程序(路径上的早期)隐藏(隐藏),我希望能够在 Windows 命令上找到程序的完整路径行,仅给出其名称。

是否有等同于 UNIX 命令 'which' 的命令?

在 UNIX 上,which command 打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。

【问题讨论】:

  • Foredecker: "which" 在 PATH 中搜索将在 shell 提示符下键入命令时运行的可执行文件。
  • 例如,如果您安装了 5 个版本的 Java,但您不知道正在使用哪个版本,您可以键入“which java”,它会为您提供二进制文件的路径
  • @Foredecker,MR 说它在 Win2k3 中的“位置”,但 Win2k3 不是问题的一部分。如果“where”不在其他 Windows 版本中,则其他答案也有效。 IMNSHO,适用于所有 Windows 版本的答案是最好的。此外,其他答案并没有错,只是方法不同。
  • 我知道这个问题出现在 SuperUser 之前,但它可能属于那里。
  • 标准 Unix 中没有 which 命令。 POSIX 实用程序是type。 C Shell 有一个 which 命令,有些系统将它作为外部可执行文件。例如,在 Debian Linux 上,which 来自一个名为 debutils 的包。这个外部which 不会“看到”shell 内置函数、别名或函数。 type 确实; Bash 的 type 有一个选项来抑制它,只需进行路径查找。

标签: windows command-line path-variables


【解决方案1】:

Windows Server 2003 及更高版本(即 Windows XP 32 位之后的任何版本)提供了 where.exe 程序,它执行 which 的一些功能,但它匹配所有类型的文件,而不仅仅是可执行命令。 (它不匹配像cd 这样的内置shell 命令。)它甚至可以接受通配符,因此where nt* 会在您的%PATH% 和当前目录中查找名称以nt 开头的所有文件。

请尝试where /? 寻求帮助。

请注意,Windows PowerShell 将where 定义为the Where-Object cmdlet 的别名,因此如果您想要where.exe,您需要键入完整名称而不是省略.exe 扩展名。或者,您可以为其设置别名:

Set-Alias which where.exe

更新: 建议使用Get-Command(别名:gcm),因为它是 PS 原生的,可以获取所有命令类型:别名、cmdlet、可执行文件和函数。示例:

gcm notepad*

【讨论】:

  • 不,因为 grep 会检查其输入的 contents,您必须明确给出。 whichwhere.exe 只查看 PATH 环境变量中设置的一组目录中的文件的名称
  • @Ajedi32 - 正确,which 不在 XP 中。正如我所说,“Windows Server 2003 及更高版本”。
  • 当心wont work in powershell 除非你输入 where.exe
  • 请记住where.exe 不是内置的shell,您需要在%PATH% 上有%windir%\system32 - 可能不是这样,因为使用where 表明您可能正在工作关于你的路径的问题!
  • 另一个答案中提到的 powershell Get-Commandgcm 等效于 where
【解决方案2】:

虽然更高版本的 Windows 具有 where 命令,但您也可以在 Windows XP 中使用环境变量修饰符来执行此操作,如下所示:

c:\> for %i in (cmd.exe) do @echo.   %~$PATH:i
   C:\WINDOWS\system32\cmd.exe

c:\> for %i in (python.exe) do @echo.   %~$PATH:i
   C:\Python25\python.exe

您不需要任何额外的工具,并且不限于PATH,因为您可以替换您希望使用的任何环境变量(当然是路径格式)。


而且,如果您想要一个可以处理 PATHEXT 中的所有扩展(就像 Windows 本身一样),这个可以解决问题:

@echo off
setlocal enableextensions enabledelayedexpansion

:: Needs an argument.

if "x%1"=="x" (
    echo Usage: which ^<progName^>
    goto :end
)

:: First try the unadorned filenmame.

set fullspec=
call :find_it %1

:: Then try all adorned filenames in order.

set mypathext=!pathext!
:loop1
    :: Stop if found or out of extensions.

    if "x!mypathext!"=="x" goto :loop1end

    :: Get the next extension and try it.

    for /f "delims=;" %%j in ("!mypathext!") do set myext=%%j
    call :find_it %1!myext!

:: Remove the extension (not overly efficient but it works).

:loop2
    if not "x!myext!"=="x" (
        set myext=!myext:~1!
        set mypathext=!mypathext:~1!
        goto :loop2
    )
    if not "x!mypathext!"=="x" set mypathext=!mypathext:~1!

    goto :loop1
:loop1end

:end
endlocal
goto :eof

:: Function to find and print a file in the path.

:find_it
    for %%i in (%1) do set fullspec=%%~$PATH:i
    if not "x!fullspec!"=="x" @echo.   !fullspec!
    goto :eof

它实际上会返回所有可能性,但您可以很容易地针对特定的搜索规则对其进行调整。

【讨论】:

  • 嘿,我希望我已经学会了!太糟糕了,它不适用于 MS-DOS 或 Win9x(即 command.com)。 (Raymond Chen 有一个更“精致”的版本,你可以将其转换成批处理文件:blogs.msdn.com/oldnewthing/archive/2005/01/20/357225.aspx
  • @Michael,如果您仍在使用 DOS 或 Win95,在路径上查找可执行文件是您最少的问题:-)
  • windows 将不止 .exe 识别为可执行文件。上次我在 W95/DOS 时代编写了 which 时,搜索顺序是 - 当前目录,然后是每个路径目录,对于 cmd.com,然后是 cmd.exe,然后是 cmd.bat 所以,甚至 cmd.bat 在当前目录在路径中的 cmd.exe soemwhere 之前执行
  • @mawg,原版用于您知道扩展名的地方,因为它反映了 UNIX 下的扩展名(不会发生扩展名添加技巧)。我现在添加了一个可以做你想做的事,但它不再是一个简单的命令,而是一个脚本。它首先尝试简单的命令,然后尝试每个扩展命令。希望有帮助。您可以根据自己的需要对其进行调整(例如,如果您想要与 Windows 相同的搜索顺序 - 这个显示了所有可能性)。
  • 要将其转换为批处理脚本,请创建一个名为“which.bat”的文件:@echo off for %%i in (%1) do @echo. %%~$PATH:%i 要将其添加到您每次运行 cmd.exe 时加载的 alias.bat 脚本中(将上面的脚本在一个名为 C:\usr\aliases 的新目录中):DOSKEY which=C:\usr\aliases\which.bat $* 然后您可以使用 alias.bat 文件创建一个脚本来启动 cmd.exe:cmd.exe /K E:\usr\aliases\alias.bat
【解决方案3】:

在 PowerShell 下,Get-Command 将在 $Env:PATH 的任何位置找到可执行文件。

$ Get-Command eventvwr

CommandType   Name          Definition
-----------   ----          ----------
Application   eventvwr.exe  c:\windows\system32\eventvwr.exe
Application   eventvwr.msc  c:\windows\system32\eventvwr.msc

而且由于 powershell 让你定义别名,which 可以这样定义。

$ sal which gcm   # short form of `Set-Alias which Get-Command`
$ which foo
...

PowerShell 命令 不仅仅是可执行文件(.exe.ps1 等)。它们也可以是 cmdlet、函数、别名、在 $Env:PATHEXT 中设置的自定义可执行后缀等。Get-Command 能够找到并列出所有这些命令(非常类似于 Bash 的 type -a foo)。仅此一项就使其优于 where.exewhich.exe 等,后者通常仅限于查找可执行文件。

仅使用部分名称查找可执行文件

$ gcm *disk*

CommandType     Name                             Version    Source
-----------     ----                             -------    ------
Alias           Disable-PhysicalDiskIndication   2.0.0.0    Storage
Alias           Enable-PhysicalDiskIndication    2.0.0.0    Storage
Function        Add-PhysicalDisk                 2.0.0.0    Storage
Function        Add-VirtualDiskToMaskingSet      2.0.0.0    Storage
Function        Clear-Disk                       2.0.0.0    Storage
Cmdlet          Get-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          New-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          Remove-PmemDisk                  1.0.0.0    PersistentMemory
Application     diskmgmt.msc                     0.0.0.0    C:\WINDOWS\system32\diskmgmt.msc
Application     diskpart.exe                     10.0.17... C:\WINDOWS\system32\diskpart.exe
Application     diskperf.exe                     10.0.17... C:\WINDOWS\system32\diskperf.exe
Application     diskraid.exe                     10.0.17... C:\WINDOWS\system32\diskraid.exe
...

查找自定义可执行文件

与 UNIX 不同,其中可执行文件是设置了可执行 (+x) 位的文件,Windows 上的可执行文件是存在于 $PATH 环境中指定的目录之一中的文件。其文件名后缀在 $PATHEXT 环境中命名的变量。变量(默认为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL)。

Get-Command 也尊重这个环境。变量,它可以扩展为列出自定义可执行文件。例如

$ $Env:PATHEXT="$Env:PATHEXT;.dll;.ps1;.psm1;.py"     # temporary assignment, only for this shell's process

$ gcm user32,kernel32,*WASM*,*http*py

CommandType     Name                        Version    Source
-----------     ----                        -------    ------
ExternalScript  Invoke-WASMProfiler.ps1                C:\WINDOWS\System32\WindowsPowerShell\v1.0\Invoke-WASMProfiler.ps1
Application     http-server.py              0.0.0.0    C:\Users\ME\AppData\Local\Microsoft\WindowsApps\http-server.py
Application     kernel32.dll                10.0.17... C:\WINDOWS\system32\kernel32.dll
Application     user32.dll                  10.0.17... C:\WINDOWS\system32\user32.dll

有关更多选项和示例,请参阅 Get-Command

【讨论】:

  • 它发现的不仅仅是可执行文件。它还捕获命令文件
  • @TheIncorrigible1 - 如果您的意思是 命令文件,例如批处理文件(.BAT.CMD 等),它们被视为可执行文件,因为它们的扩展名在PATHEXT 变量(默认为 PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL)。可以通过添加文件扩展名并创建与assoc/ftype 的可执行关联来添加其他可执行类型(例如.py.rb 等) - 例如docs.python.org/3.3/using/…
  • 现在已经是 2020 年了,老实说,这应该被选为最佳答案,因为最初的答案是在 2008 年发布的。时代变了。 PowerShell 是一种方式,尤其是这个答案现在对于 PowerShell 存在的任何地方都是跨平台的。
【解决方案4】:

在 Windows PowerShell 中:

set-alias which where.exe

【讨论】:

    【解决方案5】:

    如果您安装了 PowerShell(我推荐),则可以使用以下命令作为大致等效命令(将 programName 替换为可执行文件的名称):

    ($Env:Path).Split(";") | Get-ChildItem -filter programName*
    

    更多内容在这里: My Manwich! PowerShell Which

    【讨论】:

    • 我一直在寻找这个精确的 powershell 命令。我一直在使用 where.exe,但在解析其输出时不得不处理错误代码远不如原生 powershell 解决方案。谢谢!
    • 但是($Env:Path).Split(";") | Get-ChildItem -filter programName* 很容易输入... ;-)
    • 如果您的路径中有一个通常由系统解析的变量(又名 %JAVA_HOME%),这也会失败。
    • 我无法让 which.exe 工作,我试过了,它成功了。
    • gcm 的答案在今天会更好地使用 PowerShell:stackoverflow.com/a/27140194/5340149
    【解决方案6】:

    GnuWin32 工具有 which,以及一大堆其他 Unix 工具。

    【讨论】:

      【解决方案7】:

      在 Windows CMD 中 which 调用 where:

      $ where php
      C:\Program Files\PHP\php.exe
      

      【讨论】:

      • 哪里会列出所有可用的资源。对我来说,我使用了where pip,得到了三个结果。我不确定哪一个是被执行的。
      • 您的答案与the accepted answer 没有本质区别,也不是很有用,因为它没有添加任何新的价值或信息。请避免编写重复的答案,编辑您的答案以增加价值或完全删除它,这将确保网站上的所有问题和答案仍然有用,而不是分散/重复。
      【解决方案8】:

      Cygwin 是一个解决方案。如果您不介意使用第三方解决方案,那么 Cygwin 就是您的最佳选择。

      Cygwin 为您提供了在 Windows 环境中使用 *nix 的舒适性(您可以在 Windows 命令 shell 中使用它,或者使用您选择的 *nix shell)。它为您提供了大量适用于 Windows 的 *nix 命令(如 which),您只需将该目录包含在您的 PATH 中即可。

      【讨论】:

      • Ferruccio 前面提到的 GnuWin32 在这种情况下要好得多,因为您可以单独使用本机 where 可执行文件。
      • GnuWin32 很棒,我使用它,但是如果您想要这个功能而不安装 GnuWin32 工具,那么 where.exe 似乎是正确的选择。不过,我确实将 GnuWin32 工具放在了我们网络上的 \bin$ 共享中,这样我就可以在本地没有安装它们的工作站(和批处理文件中)使用它们。
      • 当我们谈到 Windows 中 Cygwin 的使用时,我更喜欢: cygpath -w "`which `"
      【解决方案9】:

      在 PowerShell 中,它是 gcm,它提供有关其他命令的格式化信息。如果您只想检索可执行文件的路径,请使用.Source

      例如:gcm git(gcm git).Source

      花絮:

      【讨论】:

        【解决方案10】:

        我的 PowerShell 配置文件中有一个名为“which”的函数

        function which {
            get-command $args[0]| format-list
        }
        

        这是输出的样子:

        PS C:\Users\fez> which python
        
        
        Name            : python.exe
        CommandType     : Application
        Definition      : C:\Python27\python.exe
        Extension       : .exe
        Path            : C:\Python27\python.exe
        FileVersionInfo : File:             C:\Python27\python.exe
                          InternalName:
                          OriginalFilename:
                          FileVersion:
                          FileDescription:
                          Product:
                          ProductVersion:
                          Debug:            False
                          Patched:          False
                          PreRelease:       False
                          PrivateBuild:     False
                          SpecialBuild:     False
                          Language:
        

        【讨论】:

        • 其他解决方案都不适合我,但 &gt; get-command app.exe | format-list 工作得很好!
        【解决方案11】:

        从这里获取 unxutils:http://sourceforge.net/projects/unxutils/

        gold 在 windows 平台上,将所有不错的 unix 实用程序放在标准 windows DOS 上。多年来一直在使用它。

        它包含一个“which”。请注意,它是区分大小写的。

        注意:要安装它,请在某处爆炸 zip 并将 ...\UnxUtils\usr\local\wbin\ 添加到您的系统路径环境变量中。

        【讨论】:

        • 它不区分大小写,我也必须说哪个 java.exe 而不是哪个 java - windows 7
        • 虽然与换行有关,但它有一些挫败感;例如,如果不为 \r 输入 .,grep 将不会匹配 EOL。这是一个 99% 的解决方案,但可以肯定!
        • 是的,它不区分大小写,但是默认情况下,Windows 中的文件名也不区分大小写。
        【解决方案12】:

        如果你能找到一个免费的 Pascal 编译器,你可以编译它。至少它有效并显示了必要的算法。

        program Whence (input, output);
          Uses Dos, my_funk;
          Const program_version = '1.00';
                program_date    = '17 March 1994';
          VAR   path_str          : string;
                command_name      : NameStr;
                command_extension : ExtStr;
                command_directory : DirStr;
                search_dir        : DirStr;
                result            : DirStr;
        
        
          procedure Check_for (file_name : string);
            { Check existence of the passed parameter. If exists, then state so   }
            { and exit.                                                           }
          begin
            if Fsearch(file_name, '') <> '' then
            begin
              WriteLn('DOS command = ', Fexpand(file_name));
              Halt(0);    { structured ? whaddayamean structured ? }
            end;
          end;
        
          function Get_next_dir : DirStr;
            { Returns the next directory from the path variable, truncating the   }
            { variable every time. Implicit input (but not passed as parameter)   }
            { is, therefore, path_str                                             }
            var  semic_pos : Byte;
        
          begin
              semic_pos := Pos(';', path_str);
              if (semic_pos = 0) then
              begin
                Get_next_dir := '';
                Exit;
              end;
        
              result := Copy(Path_str, 1, (semic_pos - 1));  { return result   }
              { Hmm! although *I* never reference a Root drive (my directory tree) }
              { is 1/2 way structured), some network logon software which I run    }
              { does (it adds Z:\ to the path). This means that I have to allow    }
              { path entries with & without a terminating backslash. I'll delete   }
              { anysuch here since I always add one in the main program below.     }
              if (Copy(result, (Length(result)), 1) = '\') then
                 Delete(result, Length(result), 1);
        
              path_str := Copy(path_str,(semic_pos + 1),
                               (length(path_str) - semic_pos));
              Get_next_dir := result;
          end;  { Of function get_next_dir }
        
        begin
          { The following is a kludge which makes the function Get_next_dir easier  }
          { to implement. By appending a semi-colon to the end of the path         }
          { Get_next_dir doesn't need to handle the special case of the last entry }
          { which normally doesn't have a semic afterwards. It may be a kludge,    }
          { but it's a documented kludge (you might even call it a refinement).    }
          path_str := GetEnv('Path') + ';';
        
          if (paramCount = 0) then
          begin
            WriteLn('Whence: V', program_version, ' from ', program_date);
            Writeln;
            WriteLn('Usage: WHENCE command[.extension]');
            WriteLn;
            WriteLn('Whence is a ''find file''type utility witha difference');
            Writeln('There are are already more than enough of those :-)');
            Write  ('Use Whence when you''re not sure where a command which you ');
            WriteLn('want to invoke');
            WriteLn('actually resides.');
            Write  ('If you intend to invoke the command with an extension e.g ');
            Writeln('"my_cmd.exe param"');
            Write  ('then invoke Whence with the same extension e.g ');
            WriteLn('"Whence my_cmd.exe"');
            Write  ('otherwise a simple "Whence my_cmd" will suffice; Whence will ');
            Write  ('then search the current directory and each directory in the ');
            Write  ('for My_cmd.com, then My_cmd.exe and lastly for my_cmd.bat, ');
            Write  ('just as DOS does');
            Halt(0);
          end;
        
          Fsplit(paramStr(1), command_directory, command_name, command_extension);
          if (command_directory <> '') then
          begin
        WriteLn('directory detected *', command_directory, '*');
            Halt(0);
          end;
        
          if (command_extension <> '') then
          begin
            path_str := Fsearch(paramstr(1), '');    { Current directory }
            if   (path_str <> '') then WriteLn('Dos command = "', Fexpand(path_str), '"')
            else
            begin
              path_str := Fsearch(paramstr(1), GetEnv('path'));
              if (path_str <> '') then WriteLn('Dos command = "', Fexpand(path_str), '"')
                                  else Writeln('command not found in path.');
            end;
          end
          else
          begin
            { O.K, the way it works, DOS looks for a command firstly in the current  }
            { directory, then in each directory in the Path. If no extension is      }
            { given and several commands of the same name exist, then .COM has       }
            { priority over .EXE, has priority over .BAT                             }
        
            Check_for(paramstr(1) + '.com');     { won't return if file is found }
            Check_for(paramstr(1) + '.exe');
            Check_for(paramstr(1) + '.bat');
        
            { Not in current directory, search through path ... }
        
            search_dir := Get_next_dir;
        
            while (search_dir <> '') do
            begin
               Check_for(search_dir + '\' + paramstr(1) + '.com');
               Check_for(search_dir + '\' + paramstr(1) + '.exe');
               Check_for(search_dir + '\' + paramstr(1) + '.bat');
               search_dir := Get_next_dir;
            end;
        
            WriteLn('DOS command not found: ', paramstr(1));
          end;
        end.
        

        【讨论】:

        • 哇,还有人在用 Pascal 吗? :-)
        • 我想有。但不是我。你看到了吗 program_date = '17 March 1994';
        • 单位my_funk; 是不必要的。感谢您发布 Pascal 程序,让我想起了我的青春!可惜帕斯卡没有进化。
        • 哦,但确实如此。例如,它现在是面向对象的。 lazarus-ide.org 有一个很棒的免费跨平台实现和 IDE,Borland 的直系后代仍然住在德尔福 embarcadero.com/products/delphi,这非常昂贵(imo),入门版 299 美元,“可用”版本。然而,它是跨平台的——windows、iOs、Mac、Android。获取试用版或使用 Lazarus,感觉年轻 20 岁,-)
        • @yannis “很遗憾 Pascal 没有进化”……除了'Turbo Pascal' Anders 继续设计 C#,你的意思是?
        【解决方案13】:

        Windows 没有现货,但它由Services for Unix 提供,并且有几个简单的批处理脚本可以完成同样的事情,例如this one

        【讨论】:

        • 除了你链接的命令只输出PATH变量,甚至不检查是否在那里找到文件。
        【解决方案14】:

        我在 Windows 上找到的最佳版本是 Joseph Newcomer 的“whereis”实用程序,可从his site 获得(有源代码)。

        关于“whereis”开发的文章值得一读。

        【讨论】:

        • 后期评论:在Win 7 64位下查找64位可执行文件有问题。
        【解决方案15】:

        我在 Internet 上找到的 Unix 的 Win32 端口没有一个是令人满意的,因为它们都有以下一个或多个缺点:

        • 不支持 Windows PATHEXT 变量。 (它定义了在扫描路径之前隐式添加到每个命令的扩展列表,以及添加顺序。)(我使用了很多 tcl 脚本,并且没有公开可用的工具可以找到它们。)
        • 不支持 cmd.exe 代码页,这使它们无法正确显示非 ascii 字符的路径。 (我对此非常敏感,我的名字中有 ç :-))
        • 不支持 cmd.exe 和 PowerShell 命令行中的不同搜索规则。 (没有公开可用的工具可以在 PowerShell 窗口中找到 .ps1 脚本,但在 cmd 窗口中却找不到!)

        所以我最终写了自己的哪个,它正确地支持了上述所有内容。

        在那里可用: http://jf.larvoire.free.fr/progs/which.exe

        【讨论】:

        • 仅供参考,我已经在 github.com/JFLarvoire/SysToolsLib 上开源了我上面提到的 which.exe 工具以及许多其他工具。您可能会在那里获得最新版本、报告问题等。
        【解决方案16】:

        此批处理文件使用 CMD 变量处理来查找将在路径中执行的命令。注意:当前目录总是在路径之前完成)并且根据使用的 API 调用,在路径之前/之后搜索其他位置。

        @echo off
        echo. 
        echo PathFind - Finds the first file in in a path
        echo ======== = ===== === ===== ==== == == = ====
        echo. 
        echo Searching for %1 in %path%
        echo. 
        set a=%~$PATH:1
        If "%a%"=="" (Echo %1 not found) else (echo %1 found at %a%)
        

        请参阅set /? 寻求帮助。

        【讨论】:

          【解决方案17】:

          您可以先从 Downloading Git 安装 Git,然后打开 Git Bash 并输入:

          which app-name
          

          【讨论】:

            【解决方案18】:

            我正在使用 GOW(Windows 上的 GNU),它是 Cygwin 的轻量级版本。您可以从 GitHub here 获取它。

            GOW(Windows 上的 GNU)是 Cygwin 的轻量级替代品。它用 一个方便的 Windows 安装程序,安装了大约 130 个非常 有用的开源 UNIX 应用程序编译为本机 win32 二进制文件。它被设计为尽可能小,大约 10 MB,如 与 Cygwin 相反,它可以运行超过 100 MB,具体取决于 选项。 - About Description(Brent R. Matzelle)

            GOW 中包含的命令列表截图:

            【讨论】:

              【解决方案19】:

              我创建了类似于 Ned Batchelder 的工具:

              Searching .dll and .exe files in PATH

              虽然我的工具主要用于搜索各种 dll 版本,但它会显示更多信息(日期、大小、版本),但它不使用 PATHEXT(我希望尽快更新我的工具)。

              【讨论】:

                【解决方案20】:

                只需发布此 Windows 的一个内衬批处理文件:

                C:>type wh.cmd
                @for %%f in (%*) do for %%e in (%PATHEXT% .dll .lnk) do for %%b in (%%f%%e) do for %%d in (%PATH%) do if exist %%d\%%b echo %%d\%%b
                

                测试:

                C:>wh ssh
                C:\cygwin64\bin\ssh.EXE
                C:\Windows\System32\OpenSSH\\ssh.EXE
                

                如果您将代码包装在 setlocal enableextensionsendlocal 中,则不完全是单行代码。

                【讨论】:

                • 希望多行这样我可以理解。 ;-)
                【解决方案21】:

                对于 Windows XP 用户(没有内置 where 命令),我编写了一个名为 whichr 的 ruby​​gem 的“where like”命令。

                要安装它,请安装 Ruby。

                然后

                gem install whichr
                

                像这样运行它:

                C:> whichr cmd_here

                【讨论】:

                • 我怀疑你被否决了,因为你建议在 Windows XP 上做任何事情。
                • 没有投反对票,但安装 Ruby 来实现一个微不足道的命令也很难卖。上面有一个 for 循环,可以放入批处理脚本中。
                • 如果您在详细模式下运行它,它是否会打印 The Witcher 主题曲的开头行? ;)
                【解决方案22】:

                JPSoft 的 TCC 和 TCC/LE 是 CMD.EXE 替代品,增加了重要功能。与 OP 的问题相关,which 是 TCC 系列命令处理器的内置命令。

                【讨论】:

                  【解决方案23】:

                  我使用 npm 的 which 模块已经有一段时间了,效果很好:https://www.npmjs.com/package/which 这是一个很棒的多平台替代品。

                  现在我切换到 Git 附带的 which。只需将来自 Git 的 /usr/bin 路径添加到您的路径中,该路径通常位于 C:\Program Files\Git\usr\bin\which.exewhich 二进制文件将位于 C:\Program Files\Git\usr\bin\which.exe。它速度更快,也能按预期工作。

                  【讨论】:

                    【解决方案24】:

                    试试这个

                    set a=%~$dir:1
                    If "%for%"=="" (Echo %1 not found) else (echo %1 found at %a%)
                    

                    【讨论】:

                      【解决方案25】:

                      可以从以下 GitHub 存储库下载为 Windows 编译的所有 UNIX 命令,包括 whichhttps://github.com/George-Ogden/UNIX

                      【讨论】:

                        【解决方案26】:

                        这是我用来查找可执行文件的函数,类似于 Unix 命令“WHICH”

                        app_path_func.cmd:

                        @ECHO OFF
                        CLS
                        
                        FOR /F "skip=2 tokens=1,2* USEBACKQ" %%N IN (`reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%~1" /t REG_SZ  /v "Path"`) DO (
                         IF /I "%%N" == "Path" (
                          SET wherepath=%%P%~1
                          GoTo Found
                         )
                        )
                        
                        FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe %~1`) DO (
                         SET wherepath=%%F
                         GoTo Found
                        )
                        
                        FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES%" %~1`) DO (
                         SET wherepath=%%F
                         GoTo Found
                        )
                        
                        FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%PROGRAMFILES(x86)%" %~1`) DO (
                         SET wherepath=%%F
                         GoTo Found
                        )
                        
                        FOR /F "tokens=* USEBACKQ" %%F IN (`where.exe /R "%WINDIR%" %~1`) DO (
                         SET wherepath=%%F
                         GoTo Found
                        )
                        
                        :Found
                        SET %2=%wherepath%
                        :End
                        

                        测试:

                        @ECHO OFF
                        CLS
                        
                        CALL "app_path_func.cmd" WINWORD.EXE PROGPATH
                        ECHO %PROGPATH%
                        
                        PAUSE
                        

                        结果:

                        C:\Program Files (x86)\Microsoft Office\Office15\
                        Press any key to continue . . .
                        

                        https://www.freesoftwareservers.com/display/FREES/Find+Executable+via+Batch+-+Microsoft+Office+Example+-+WINWORD+-+Find+Microsoft+Office+Path

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 2021-08-20
                          • 2012-06-18
                          • 2023-03-31
                          • 1970-01-01
                          • 2014-04-02
                          • 2019-10-31
                          • 1970-01-01
                          相关资源
                          最近更新 更多