我添加到系统路径脚本一直无法预测地弄乱路径

时间:2015-09-22 08:57:21

标签: windows batch-file cmd environment-variables

我做了这个很好的批处理,应该在Windows上添加给系统%PATH%的给定路径:

@echo off
if exist %1\ (
   setx PATH "%PATH%;%1"
) else (
   echo %1 is NOT a folder
   pause
)

exist %1\是检查路径是否为文件夹的技巧。我甚至为它做了一个很好的上下文菜单项:

image description

但问题是它在系统路径中造成了大量重复的条目。您可能会注意到我省略了/M参数。这是故意的,我只想配置用户的系统路径。这就是说我想我也需要能够专门获得这条路径。在这一刻,我认为它融合了系统和用户路径,并做了一些额外的神秘混乱。

1 个答案:

答案 0 :(得分:0)

如果您的问题是如何清理路径,可以在批处理中添加以下行pathmgr.cmd /clean /user /y,然后从PathMgrhere下载here (gist)批处理脚本

>pathmgr /clean /user /?

CLEAN
Will clean up the PATH variable in the registry by removing
trailing and leading spaces, remove any enclosing double quotes,
remove duplicate paths and if /v specified will remove invalid paths.
The scope indicates wheteher user, system or both paths are cleaned.
By default will prompt if you want to make a permanaent
change to the registry. Note that any changes made to the path variable
not made permanent in the registry will be lost.
If you do not have administrator privileges you will not be able
to make changes to the system path.

SYNTAX:         pathmgr /clean [scope] [modifiers]

[scope]         /user|/system|/all

[modifiers]      /v Will also delete any invalid paths
                 /p Display prompt to make registry change.
                 /y Do not ask, just make registry change (batch mode).
                 /n Do not make registry change (batch mode).

它可以做更多。

pathmgr Version 1.0.2
Tool to manage the PATH environment variable.
Provides tools to add to , delete from, clean up,
backup and restore the PATH environment variable
either on a session basis or make permanent changes
in the registry. To get help type "pathmgr /?".
Whenever pathmgr is run a temporary environment
variable pathmgr_PATH is created that holds the
current path of the pathmgr command file. The
/path switch may be used to add the path of pathmgr itself.

SYNTAX:         pathmgr [/?|-?|] [action][modifier][scope][path]

[/?|-?]         Display help, further help can be obtained on
                a topic by including an action switch.
                Example: pathmgr /? /List  will provide help on list action.

[action]        The management action to perform. Perermited values are:-
                /list | /add | /del[ete] | /clean |/backup | /restore | /refresh | /path | /[env]ironment

[modifier]      Some actions may have additional options.
                         /x: Expand environment variables.
                         /r: Reverse order.
                         /v: validate a path(s) existence.
                        /nv: no validation of path(s) existence.
                         /p: display all prompts
                         /y: hide prompts and choose yes
                         /n: hide prompts and choose no
                         /+: add current pathmgr to PATH
                         /-: delete current pathmgr from PATH

[scope]         The scope within the registry to perform the action.
                Permited values are:-  /user | /system | /all.
                The default scope is user as there are fewer
                policy restrictions. System and All scopes may require
                administrative privileges.

[path]          Depending on context this will be either a folder or file path.
                If the path is the only argument then the default action is /add.
                Environment variables may be used in paths. The easiest way to do this
                is to enclose the variable name within question marks '?' rather than
                using the percent sign '', as an example ?systemroot?.
                This practice has the advantage of working either from the commandline or
                working within a batch. The alternative is to escape the variable as below.
                If used on the command line use:-  pathmgr /add ?systemroot?" or
                pathmgr /add "^%systemroot^%". If used within a batch use
                pathmgr /add   "?systemroot?" or pathmgr /add "%%systemroot%%".
                If these escaped paths are not used the path will be expanded prior to being
                interpreted so if you really want the path to be C:\Windows without escaping
                the path will be added as "c:\windows".