从 MATLAB 中查找已安装程序的路径?

发布于 2024-11-04 01:10:26 字数 69 浏览 1 评论 0原文

我可以从MATLAB命令行中找到特定程序的安装路径吗? 或者我可以找到已注册程序的路径(相当于 Windows reg)吗?

Can I find out from the MATLAB command line what is the installation path of specific program?
Or can I find the path for a registered program (Windows reg equivalent)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朦胧时间 2024-11-11 01:10:26

这不会是 100% 可靠,但大多数情况下都会得到正确的答案:

function p = findOnSystemPath(f)
p = '';
path = getenv('path');
dirs = regexp(path,pathsep,'split');
for iDirs = 1:numel(dirs)
    tp = fullfile(dirs{iDirs},f);
    if exist(p,'file')
        p = tp;
        break
    end
end

示例用法:

>> findOnSystemPath('runemacs.exe')

ans =

C:\Program Files (x86)\emacs\bin\runemacs.exe

根据您的操作系统,您可能可以直接从系统获取此信息:

which可在安装了 Cygwin 的 Unix 系统和 Windows 系统上使用:

>> [~,p] = system(sprintf('which "%s"',f))

p =

C:/Program Files (x86)/emacs-mw-a/bin/runemacs.exe

where 可在<一href="https://superuser.com/questions/276736/which-versions-configurations-of-microsoft-windows-include-the-where-command">Windows 2003 及更高版本:

>> [~,p] = system(sprintf('where "%s"',f))

p =

C:\Program Files (x86)\emacs-mw-a\bin\runemacs.exe

在某些情况下,您可以使用 winqueryreg,例如:

>> notepadEdit = winqueryreg('HKEY_CLASSES_ROOT','Applications\notepad.exe\shell\edit\command')

notepadEdit =

C:\Windows\system32\NOTEPAD.EXE %1

This won't be 100% reliable, but this will get the right answer most of the time:

function p = findOnSystemPath(f)
p = '';
path = getenv('path');
dirs = regexp(path,pathsep,'split');
for iDirs = 1:numel(dirs)
    tp = fullfile(dirs{iDirs},f);
    if exist(p,'file')
        p = tp;
        break
    end
end

Sample usage:

>> findOnSystemPath('runemacs.exe')

ans =

C:\Program Files (x86)\emacs\bin\runemacs.exe

Depending on your OS, you might be able to get this information from the system directly:

which is available on Unix systems and Windows systems with Cygwin installed:

>> [~,p] = system(sprintf('which "%s"',f))

p =

C:/Program Files (x86)/emacs-mw-a/bin/runemacs.exe

where is available on Windows 2003 and later:

>> [~,p] = system(sprintf('where "%s"',f))

p =

C:\Program Files (x86)\emacs-mw-a\bin\runemacs.exe

And in some cases, you can pull this information from the registry using winqueryreg, for example:

>> notepadEdit = winqueryreg('HKEY_CLASSES_ROOT','Applications\notepad.exe\shell\edit\command')

notepadEdit =

C:\Windows\system32\NOTEPAD.EXE %1
淡笑忘祈一世凡恋 2024-11-11 01:10:26

调用 DOS/bash 命令 which,例如

!which matlab
!which notepad

(或使用 system 而不是 !。)

编辑: Windows 中似乎没有直接的等效项。我在(Win XP)机器上安装了 cygwin,并进行了尝试,命令成功。或者,请查看 stackoverflow 和 < a href="https://superuser.com/questions/49104/handy-tool-to-find-executable-program-location">超级用户。

Call the DOS/bash command which, e.g.,

!which matlab
!which notepad

(Or use system instead of the !.)

EDIT: It seems that there isn't a direct equivalent in Windows. I had cygwin installed on the (Win XP) machine I tried it on, and the command succeeded. Alternatively, take a look at these answers on stackoverflow and superuser.

白况 2024-11-11 01:10:26

这取决于您对操作系统的了解以及您的程序具有哪些属性。

在 Linux 上我通常会做这样的事情:

[error, path] = system(sprintf('which "%s"',programName));

它看起来不漂亮,而且远非便携(我想它不能在 Windows 上工作,也许只有当你安装 Cygwin 或类似的东西时)。在 Unix 中要容易得多,因为大多数可执行文件都可以从“路径”(环境变量“路径”)访问,而在 Windows 中,大多数可执行文件要么存储在 Windows 目录中(位于默认路径中,因此可以找到它们),要么在 Program Files 目录中,据我所知,该目录并不存在。

当找到程序时,错误 = 0,并且 path 显然包含可执行文件的路径。

对于 Windows,我想您可以搜索该程序的所有目录,但这可能有点乏味。

This depends on what you know about the OS and what properties your program has.

On Linux I usually do something like:

[error, path] = system(sprintf('which "%s"',programName));

It doesn't look pretty and it's far from portable (I suppose it will not work on Windows, perhaps only if you install Cygwin or something similar). It's far easier in Unix since most executables are accessible from the "path" (the environment variable "path"), while in Windows most executables are either stored in the Windows directory (which is in the default path, so they are found) or in a Program Files directory which is not as far as I recall.

Error = 0 when the program is found and path then obviously contains the path to the executable.

For Windows I guess you can search all directories for the program, but that might be somewhat tedious.

£噩梦荏苒 2024-11-11 01:10:26

MATLAB 并不是真正设计用来作为搜索驱动器上任何位置的文件的工具。这是一项最好留给操作系统的任务,Egon 建议的就是你应该做的。只需将 which 替换为 DOS 中的等效项(您应该已经知道这一点,否则只需在 MS-DOS/Windows 标记中询问另一个问题。它可能已经得到解答。)。

如果您真的一心想要使用 MATLAB 来搜索驱动器,那么您可以执行以下操作

addpath(genpath('C:\')); %#' I am not sure which way the slash is
which filename

,请注意,第一步需要一段时间。

MATLAB is not really designed to be used as a tool to search files anywhere on the drive. That's a task best left to the OS, and what Egon suggested is what you should be doing. Simply replace which with the equivalent in DOS (you should know this already, else just ask another question in the MS-DOS/Windows tag. It probably has already been answered.).

If you are really hell bent on using MATLAB to search the drive, then you can do the following

addpath(genpath('C:\')); %#' I am not sure which way the slash is
which filename

Beware, the first step will take a while.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文