windows 递归路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5585109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:35:33  来源:igfitidea点击:

Path recursively

windowspath

提问by Rumel

I tried searching this on Google and didn't really learn anything as the search results usually pertain to other recursive subjects. What I would like to know is if a folder is in Path is it defined recursively (on Windows)?

我尝试在谷歌上搜索这个,但并没有真正学到任何东西,因为搜索结果通常与其他递归主题有关。我想知道的是,如果文件夹在 Path 中,它是否递归定义(在 Windows 上)?

I want to create a C:\StandaloneProgramsand add that to path. It will contain a bunch of programming languages and other programs that usually come from zip files. I want to know that if by adding the program directory to it I can call all of the programs.

我想创建一个C:\StandalonePrograms并将其添加到路径中。它将包含一堆通常来自 zip 文件的编程语言和其他程序。我想知道,如果通过向其中添加程序目录,我可以调用所有程序。

For example if I have C:\StandalonePrograms\SomeProgramcan I open up a command prompt type someCommandand expect it to run from the C:\StandalonePrograms\SomeProgram\binfolder?

例如,如果我C:\StandalonePrograms\SomeProgram可以打开命令提示符类型someCommand并期望它从C:\StandalonePrograms\SomeProgram\bin文件夹中运行?

Or do I need to explicitly define C:\StandalonePrograms\SomeProgram\binin my Path?

还是我需要C:\StandalonePrograms\SomeProgram\bin在我的路径中明确定义?

If I can't are there any workarounds to achieve the situation I want?

如果我不能有任何解决方法来实现我想要的情况?

回答by Patrick Georgi

You need to specify each directory individually, the PATH mechanism doesn't walk through subdirectories.

您需要单独指定每个目录,PATH 机制不会遍历子目录。

A workaround could be a directory full of batch files (of some sort) that start the real tools with full path

解决方法可能是一个装满批处理文件(某种形式)的目录,这些文件以完整路径启动真正的工具

回答by Alexander Gelbukh

Here is a workaround. Save this as "SetMyPath.bat" (or with another name):

这是一个解决方法。将其另存为“SetMyPath.bat”(或使用其他名称):

@echo off
set dir=%*
setlocal EnableDelayedExpansion
for /f "delims=" %%i in ('dir /s /ad /o:d /b "%dir:"=%"') do set path=%%i;!path!
cmd

(Here, "%dir:"=%"is only needed to permit you to omit quotation marks around the directories with space in the names when calling this file. If you don't need this, then %1would do instead.)

(这里,"%dir:"=%"只需要允许您在调用此文件时省略名称中带有空格的目录周围的引号。如果您不需要这个,那么%1可以代替。)

This file takes one command-line argument: the directory. It will launch a new copy of cmd.exe, where files under the given directory will be available:

该文件采用一个命令行参数:目录。它将启动 的新副本cmd.exe,其中给定目录下的文件将可用:

C:\> mysqldump.exe
File not found.
C:\> SetMyPath.bat C:\Program Files\MySQL
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\> mysqldump.exe
Usage: mysqldump [OPTIONS] database [tables]
C:\> exit

In this example, the first command shows that mysqldump.exeis not on the path. After you execute the batch file, a new cmd.exeis launched, where mysqldump.exeis available. When you finish working with it, exitreturns you to the original copy of cmd.exe.

在此示例中,第一个命令显示mysqldump.exe不在路径上。执行批处理文件后,将cmd.exe启动一个新文件,其中mysqldump.exe可用。当您完成使用它时,exit将您返回到 的原始副本cmd.exe

If there are two copies of the .exefile under different subdirectories, the copy in the most recently updated directory will be launched (because of /o:d). In this example, assuming that the directory of the most recent version of MySQL was updated last, the most recent version of mysqldump.exewill be launched.

如果.exe在不同的子目录下有文件的两个副本,则会启动最近更新目录中的副本(因为/o:d)。在本例中,假设最新版本的 MySQL 目录最后更新,mysqldump.exe则将启动最新版本的 MySQL 。

The batch file can be modified to guarantee that the most recent copy of the .exebe launched (ask me in a comment if you need it).

可以修改批处理文件以确保.exe启动最新的副本(如果需要,请在评论中问我)。