-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkPath.cmd
148 lines (145 loc) · 5.46 KB
/
checkPath.cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION&::(Don't pollute the global environment with the following)
::*********************************************************************
SET $NAME=%~n0
SET $DESCRIPTION=List or test/add directory to environment PATH
SET $AUTHOR=Erik Bachmann, ClicketyClick.dk [ErikBachmann@ClicketyClick.dk]
SET $SOURCE=%~f0
::@(#)NAME
::@(-) The name of the command or function, followed by a one-line description of what it does.
::@(#) %$NAME% -- %$DESCRIPTION%
::@(#)
::@(#)SYNOPSIS
::@(-) In the case of a command, a formal description of how to run it and what command line options it takes.
::@(-) For program functions, a list of the parameters the function takes and which header file contains its definition.
::@(-)
::@(#) %$NAME%
::@(#) %$NAME% "dir"
::@(#) %$NAME% -check:"dir"
::@(#) %$NAME% -add:"dir"
::@(#) %$NAME% -del:"dir"
::@(#)
::@(#)OPTIONS
::@(-) Flags, parameters, arguments (NOT the Monty Python way)
::@(#) -check:"dir" The dir that should be tested in PATH
::@(#) -add:"dir" The dir that should be appended to PATH if not found
::@(#) -del:"dir" The dir that should be removed from PATH if found
::@(#)
::@(#)DESCRIPTION
::@(#) The bare command will list each element in PATH
::@(#) Given an argument, this is tested against PATH - and appended to PATH
::@(#)
::@(#)EXAMPLES:
::@(#) ::List current elements
::@(#) CALL %$NAME%
::@(#)
::@(#) "C:\Windows\system32"
::@(#) "C:\Windows"
::@(#)
::@(#) :: Check a specific dir in path
::@(#) CALL %$NAME% -check:"C:\Windows"
::@(#)
::@(#) "C:\Windows" exists in PATH
::@(#)
::@(#) :: Adding an existing dir to path
::@(#) CALL %$NAME% -add:"C:\Windows"
::@(#)
::@(#) "C:\Windows" exists in PATH
::@(#)
::@(#) :: Adding an new dir to path
::@(#) CALL %$NAME% -add:"C:\msdos\"
::@(#)
::@(#) Appending [C:\msdos\]
::@(#)
::@(#) :: Removing and existing dir from path
::@(#) CALL %$NAME% -del:"C:\msdos\"
::@(#)
::@(#) Appending [C:\msdos\]
::@(#)
::@(#)REQUIRES
::@(-) Dependecies
::@(#) _Debug.cmd Setting up debug environment for batch scripts
::@(#)
::@ (#)EXIT STATUS
::@ (-) Exit status / errorlevel is 0 if OK, otherwise 1+.
::@ (#) 0 OK
::@ (#) 1 Usage (or error)
::@ (#)
::@ (#)ENVIRONMENT
::@ (-) Variables affected
::@ (#)
::@ (#)BUGS / KNOWN PROBLEMS
::@ (-) If any known
::@ (#)
::@(#)REQUIRES
::@(-) Dependencies
::@(#) _Debug.cmd Setting up debug environment for batch scripts
::@(#) _getopt.cmd Parse command line options and create environment vars
::@(#)
::@(#)REFERENCES:
::@(#) URL: http://stackoverflow.com/questions/141344/how-to-check-if-directory-exists-in-path
::@(#) Please note that this implementation assumes no semicolons or quotes are present inside a single path item.
::@(#) should even deal with ++ in the path environment variable (as in Notepad++).
::@(#)
::@(#)SOURCE
::@(#) %$Source%
::----------------------------------------------------------------------
::History
::SET $VERSION=xx.xxx&SET $REVISION=YYYY-MM-DDThh:mm:ss&SET COMMENT=Init/Description
SET $VERSION=2016-07-07&SET $REVISION=14:30:00&SET $COMMENT=Initial / ErikBachmann
SET $VERSION=2017-01-11&SET $REVISION=10:00:00&SET $COMMENT=Adding delete / ErikBachmann
::**********************************************************************
::@(#)(c)%$Version:~0,4% %$Author%
::**********************************************************************
CALL "%~dp0\_debug"
CALL "%~dp0\_getopt" %*
:Init
SET _EXISTS=
SET _STATUS=0
:: Get single name
IF DEFINED @%$NAME%.1 SET _DIR=!@%$NAME%.1!!
:: Get name to add
IF DEFINED @%$NAME%.check SET _DIR=!@%$NAME%.check!!
::&SET _APPEND=!@%$NAME%.add!!
IF DEFINED @%$NAME%.add SET _DIR=!@%$NAME%.add!!&SET _APPEND=!@%$NAME%.add!!
IF DEFINED @%$NAME%.del SET _DIR=!@%$NAME%.del!!&SET _DELETE=!@%$NAME%.del!!
:: Remove quotes
IF DEFINED _DIR SET _DIR=%_DIR:"=%
:Process
IF DEFINED _DIR ( REM If argument
REM :: Check for match in path
@FOR %%P IN ("%path:;=";"%") DO ( REM For each element
@IF /i %%P=="%_DIR%" ( REM If matching new dir
ECHO %%P EXISTS IN PATH
CALL SET "_EXISTS=1"
)
)
IF NOT DEFINED _EXISTS ( REM If dir is not in path
%_DEBUG_% NOT EXISTING
IF DEFINED _APPEND ( REM AND append flag
ECHO Appending [%_DIR%] to PATH
rem ENDLOCAL&
CALL SET "PATH=%PATH%;%_DIR%;"
) ELSE (
ECHO Not found [%_DIR%] in PATH
SET _Status=1
)
) ELSE (
%_DEBUG_% EXISTING
IF DEFINED _DELETE ( REM AND append flag
ECHO Deleting [%_DIR%] from PATH
:: remove from path - trailing \ is optional
CALL SET "PATH=!PATH:%_DIR%\;=!"
CALL SET "PATH=!PATH:%_DIR%;=!"
rem ENDLOCAL&CALL SET "PATH=%PATH%;%_DIR%;"
rem ENDLOCAL&CALL SET "PATH=!PATH!"
)
)
) ELSE ( REM :: Listing elements in PATH
FOR %%P IN ("%PATH:;=";"%") DO IF NOT "!"=="!%%~P" ECHO %%P
)
ENDLOCAL&CALL SET "PATH=%PATH%"
SET PATH=%PATH:;;=;%
%_DEBUG_% %PATH%
EXIT /B %_STATUS%
::*** End of File *****************************************************