slyfox1186

Windows 10 Path Manager - pathmgr.cmd

Jul 18th, 2019 (edited)
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 46.61 KB | None | 0 0
  1. @echo off
  2. REM ********************************************************************************
  3. REM
  4. REM pathmgr.cmd - cmd file that provides tools to add to, delete from, clean up, backup and restore
  5. REM               the PATH environment variable either on a session only basis or
  6. REM               make the changes permanently in the registry.
  7. REM               To get help type "pathmgr /?"
  8. REM
  9. REM Author:     Pianoboy
  10. REM Date:       04/04/2012
  11. REM
  12. REM Date:       12/02/2013
  13. REM     Fix problem when no initial environment variable path or removing last path from
  14. REM     environment variable       
  15. REM
  16. REM ********************************************************************************
  17.  
  18. setlocal enabledelayedexpansion
  19. set "version=1.0.2"
  20. :: Set default values here
  21. :: scope used to indicate whether changes to user. system or both PATH variables
  22. set "scope=user"
  23. :: If /x indicates that environment variables should be expanded. Default is blank
  24. set "xpand=/"
  25. :: If /r then a dscending order is usedto display a list. Default is nothing
  26. set "order=/"
  27. :: userPrompt :- /p(prompt for choice) | /y(no prompt answer yes)|/n (no prompt answer no)  
  28. set "userPrompt=/p"
  29. :: If /v then validate paths else /nv
  30. set "validate=/v"
  31. :: Name of this filepathmgr
  32. set "fileName=%~n0"
  33. :: Name of this filepathmgr
  34. set "pathmgrPath=%~dp0"
  35. :: Variable to receive input path
  36. set "fsPath="
  37. set "action="
  38. set "help=false"
  39. set "addPathMgr=none"
  40. :ARGUMENTS_LOOP
  41. if "%~1"==""  ( goto SETVARIABLES)
  42.    :: Help switches
  43.     if /I "%~1" == "/?" (set "help=true" & goto SHIFT)
  44.     if /I "%~1" == "-?" (set "help=true" & goto SHIFT)
  45.    :: Scope switches
  46.     if /I "%~1" == "/user" (set "scope=user" & goto SHIFT )
  47.     if /I "%~1" == "/system" (set "scope=system" & goto SHIFT )
  48.     if /I "%~1" == "/all" (set "scope=all" & goto SHIFT)
  49.    :: Action switches
  50.    :: Default modifier for CLEAN is to keep all paths
  51.     if /I "%~1" == "/clean" (set "action=CLEAN" & set "validate=/nv" & goto SHIFT)
  52.     if /I "%~1" == "/backup" (set "action=BACKUP" & goto SHIFT)
  53.     if /I "%~1" == "/restore" (set "action=RESTORE" & goto SHIFT)
  54.     if /I "%~1" == "/refresh" (set "action=REFRESH" & goto SHIFT)
  55.     if /I "%~1" == "/path" (set "action=PATHMGR" & goto SHIFT)
  56.    :: Default modifier for ADD is /v
  57.     if /I "%~1" == "/add" (set "action=ADD" & set "validate=/v" & goto SHIFT)
  58.    :: Default modifier for DELETE is /nv ie only delete the given path, /v will delete
  59.    :: any invalid paths
  60.     if /I "%~1" == "/delete" (set "action=DELETE" & set "validate=/nv" & goto SHIFT)
  61.     if /I "%~1" == "/del" (set "action=DELETE" & set "validate=/nv" & goto SHIFT)
  62.     if /I "%~1" == "/list" (set "action=LIST" & set "validate=/nv" & goto SHIFT)
  63.     if /I "%~1" == "/env" (set "action=ENVIRONMENT" & set "validate=/nv" & goto SHIFT)
  64.     if /I "%~1" == "/environment" (set "action=ENVIRONMENT" & set "validate=/nv" & goto SHIFT)
  65.    :: Action modifier switches pathmgr /list
  66.     if /I "%~1" == "/x" (set "xpand=/x" & goto SHIFT)
  67.     if /I "%~1" == "/r" (set "order=/r" & goto SHIFT)
  68.     if /I "%~1" == "/v" (set "validate=/v" & goto SHIFT)
  69.     if /I "%~1" == "/nv" (set "validate=/nv" & goto SHIFT)
  70.     if /I "%~1" == "/p" (set "userPrompt=/p" & goto SHIFT)
  71.     if /I "%~1" == "/y" (set "userPrompt=/y" & goto SHIFT)
  72.     if /I "%~1" == "/n" (set "userPrompt=/n" & goto SHIFT)
  73.     if /I "%~1" == "/+" (set "addPathMgr=add" & goto SHIFT)
  74.     if /I "%~1" == "/-" (set "addPathMgr=delete" & goto SHIFT)
  75.    :: else parameter a path  
  76.     set "fsPath=%~1"
  77.      
  78.     :: Make expansion safe by replacing % with ?
  79.     set "fsPath=!fsPath:^%%=?!"
  80.     set "fsPath=!fsPath:%%=?!"
  81.  
  82. :SHIFT
  83. Shift
  84. goto ARGUMENTS_LOOP
  85.  
  86. :SETVARIABLES
  87.  
  88. :: Variables used by cal to GETREGVAL to retreive the path environment variable from the
  89. :: appropriate user or system key.
  90. set "keyUser=hkcu\environment"
  91. set  "keySystem=hklm\system\currentcontrolset\control\session manager\environment"
  92. set "value=path"
  93.  
  94. if "%help%" equ "true" goto HELP
  95. :: If an action switch is provided then jump to goto location value
  96. if "%action%" neq "" ( goto CALL_ACTION)
  97. :: If no action is provided but a path is provided try to add the path,
  98. :: else no parameters provided - display the error an provide help
  99. if "%fsPath%" neq "" (set "action=ADD") else  (Call :ERROR "No valid parameters have been provided" & set "help=true" & goto HELP)
  100.  
  101. :CALL_ACTION
  102. echo.
  103. echo Action %action% pathmgr
  104. goto %action%
  105.  
  106. :: LIST action - Call Getlist sub to display list
  107. :LIST
  108. call :GETLIST
  109. goto EXIT
  110.  
  111.  
  112. :: ENVIRONMENT Action list the current Path environment variable
  113. :ENVIRONMENT
  114. call :CLEAN_PATH listPath "%path%"  
  115. call :LIST_PATH "%listPath%" %order% %validate%
  116. goto EXIT
  117.  
  118.  
  119. :: BACKUP action - call backup_path with a destination file path parameter.
  120. :BACKUP
  121. setlocal enabledelayedexpansion
  122. call set "backupPath=!fsPath:?=%%!"
  123. call :BACKUP_PATH "!backupPath!"
  124. goto EXIT
  125.  
  126. :: RESTORE action - if fsPath is a valid file path call restore_path.
  127. :RESTORE
  128. echo Now restoring "%fsPath%"
  129. setlocal enabledelayedexpansion
  130. call set "backupPath=!fsPath:?=%%!"
  131. if Not Exist "%backupPath%" ( call :ERROR "The file %fsPath% to restore does not exist?" & goto EXIT )
  132. call :RESTORE_PATH "%backupPath%"
  133. endlocal & set "newpath=%newPath%"
  134. goto EXIT
  135.  
  136. REM ********************************************************************************
  137. REM
  138. REM REFRESH Action
  139. REM Reset the PATH variable to the current registry value
  140. REM
  141. REM ********************************************************************************
  142. :REFRESH
  143. setlocal enabledelayedexpansion
  144. call :GETREGVAL "%keyUser%" path userPath
  145. call :GETREGVAL "%keySystem%" path systemPath
  146. call :CLEAN_PATH userPath "%userPath%"
  147. call :CLEAN_PATH systemPath "%systemPath%"
  148. call set "userPath=!userPath:?=%%!"
  149. call set "systemPath=!systemPath:?=%%!"
  150. endlocal & set "newPath=%systemPath%%userPath%"
  151. echo PATH now refreshed
  152. goto EXIT
  153.  
  154.  
  155. REM ********************************************************************************
  156. REM
  157. REM PATHMGR Action
  158. REM Add the pathmgr path to PATH environment variable.
  159. REM
  160. REM ********************************************************************************
  161. :PATHMGR
  162. echo %filename% path is %pathmgrPath%
  163. if "%addPathMgr%" equ "add" (set "fsPath=%pathMgrPath%" & goto ADD)
  164. if "%addPathMgr%" equ "delete" (set "fsPath=%pathMgrPath%" & goto DELETE)
  165. goto EXIT
  166.  
  167.  
  168.  
  169. REM ********************************************************************************
  170. REM
  171. REM CLEAN action
  172. REM Description: Will erase any leading or trailing spaces from path and will
  173. REM          remove any duplicate paths.
  174. REM          If scope is /user then duplicate paths in the user path variable will
  175. REM      be removed as well as paths that match paths in the system path.
  176. REM              If the scope is /system then all duplicate paths in the system path
  177. REM      will be renmoved as well as matching paths in the user variable.
  178. REM      The /all switch will remove all duplicates.
  179. REM      Where an expandable environment variable in the path is expanded and matches
  180. REM              a path the environment variable is retained and the expanded version of the the
  181. REM              path is removed.
  182. REM  
  183. REM ********************************************************************************
  184. :CLEAN
  185. echo Now cleaning environment path for %scope% users.
  186. setlocal enabledelayedexpansion
  187. :: regardless of whether user or system we require
  188. :: clean versions of both the user and system path.
  189. call :GETREGVAL "%keyUser%" path userPath
  190. call :CLEAN_PATH userPath "%userPath%" "%validate%"
  191. call :GETREGVAL "%keySystem%" path systemPath
  192. call :CLEAN_PATH systemPath "%systemPath%" "%validate%"
  193. if "%scope%" equ "system" goto REMOVE_SYSTEM_DUPLICATES
  194. call :REMOVE_DUPLICATE_PATHS userPath "%userPath%"
  195. if "%scope%" equ "user" goto USER_AND_SYSTEM
  196.  
  197. :REMOVE_SYSTEM_DUPLICATES
  198. call :REMOVE_DUPLICATE_PATHS systemPath "%systemPath%"
  199.  
  200. :USER_AND_SYSTEM
  201. :: Remove duplicates in user path that are also in system path
  202. call :REMOVE_DUPLICATE_PATHS userPath "%systemPath%" "%userPath%"
  203.  
  204. call :CHOOSE_YN "Do you wish to make a permanent change to the registry Y/N?"
  205. :: If no exit
  206. if errorlevel 2 ( goto END_CLEAN )
  207. :: Need to replace ? with %% in path in path so not expanded
  208. call :UPDATE_REGISTRY "%keyUser%"  "!userPath:?=%%%%!"
  209. if "%scope%" equ "user" goto END_CLEAN
  210. call :UPDATE_REGISTRY "%keySystem%" "!systemPath:?=%%%%!"
  211. :END_CLEAN
  212. :: Replace ? with % so path is expanded
  213. :: Have decided better not to automatically change the path
  214. set "userPath=!userPath:?=%%!"
  215. set "systemPath=!systemPath:?=%%!"
  216. endlocal & call set "newpath=%systemPath%%userPath%"
  217. goto EXIT
  218.  
  219.  
  220.  
  221.  
  222. REM ********************************************************************************
  223. REM
  224. REM BACKUP_PATH
  225. REM Description: Will backup paths to the file name provided as a parameter.
  226. REM              The scope switch determines whether system, user or both paths are backed up.
  227. REM              User paths are preceded with "USER PATH" and system paths are preceded with "SYSTEM PATH"
  228. REM              in the output backup file.
  229. REM Arguments: backupPath - The full path name of the backup file.
  230. REM
  231. REM ********************************************************************************
  232. :BACKUP_PATH
  233. setlocal enabledelayedexpansion
  234. set "folderPath=%~1"
  235. if "%folderPath:~-1%" neq "\" set "folderPath=%folderPath%\"
  236.  
  237. :: If path is a folder path then no file name has been provided
  238. if Exist "%folderPath%" (call :ERROR "The path %~1 is not a valid file name?" & exit /b)
  239. :: If path not valid exit
  240. if not exist %~dp1 (call :ERROR "The path %~dp1 does not exist?"  & exit /b)
  241.  
  242.  
  243.  
  244. echo Now backing up to '%~1'
  245. REM First delete backup file.
  246. erase  "%~1" 2>nul
  247. if "%scope%" equ "system" (set "section=SYSTEM PATH" & goto BACKUP_SYSTEM)
  248. call :GETREGVAL "%keyUser%" path listPath
  249. call :CLEAN_PATH listPath "%listPath%"
  250. echo USER PATH>> "%~1"
  251. call :LIST_PATH "%listPath%" >> "%~1%"
  252. if "%scope%" equ "user"  (exit /b)
  253. :BACKUP_SYSTEM
  254. call :GETREGVAL "%keySystem%" path listPath
  255. call :CLEAN_PATH listPath "%listPath%"
  256. echo SYSTEM PATH>> "%~1"
  257. call :LIST_PATH "%listPath%" >> "%~1"
  258. endlocal
  259. exit /b
  260.  
  261.  
  262. REM ********************************************************************************
  263. REM
  264. REM RESTORE_PATH
  265. REM Description: Will read backup file line by line. If it encounters a "USER PATH"
  266. REM              line it will restore following lines to the user path. If it encounters
  267. REM              a "SYSTEM PATH" line it will restore all following lines to the system path.
  268. REM              Note that /user, /system and /all switches have no effect.
  269. REM Arguments:   userpath - path to backup file
  270. REM
  271. REM ********************************************************************************
  272. :RESTORE_PATH
  273. setlocal enabledelayedexpansion
  274. set "restorePath=%~1"
  275. set "userPath="
  276. set "systemPath="
  277. :: Note change any environment variables marked with "%" to "?" to prevent expansion
  278. for /f "usebackq tokens=*" %%a in ("%restorePath%") DO (    set "var=%%a"
  279.                             set "var=!var:%%=?!"
  280.                             call :BUILD_PATH "!var!")
  281. :: Exit if nothing to restore
  282. if "%restore%" equ "" (echo Unable to restore %restorePath%  & exit /b)
  283.  
  284. :RESTORE_REGISTRY
  285. set "user=false"
  286. set "system=false"
  287. :: need to clean path of trailing spaces - an artefact of redirecting list_path to file.
  288. if "%userPath%" neq "" (set "userPath=!userPath: ;=;!" & set "user=true") else (
  289.                         call :GETREGVAL "%keyUser%" path userPath
  290.                         call :CLEAN_PATH userPath "!userPath!")
  291. if "%systemPath%" neq "" (set "systemPath=!systemPath: ;=;!" & set "system=true") else (
  292.                         call :GETREGVAL "%keySystem%" path systemPath
  293.             call :CLEAN_PATH systemPath "!systemPath!" )
  294.  
  295.  
  296. :: offer choice to save to registry
  297. call :CHOOSE_YN "Do you wish to make a permanent change to the registry Y/N?"
  298. :: If no exit
  299. if errorlevel 2 (goto END_RESTORE )
  300. :: Need to replace ? with %% in path in path so not expanded
  301. if "%user%" equ "true" (call :UPDATE_REGISTRY "%keyUser%"  "!userPath:?=%%%%!" )
  302. if "%system%" equ "true" (call :UPDATE_REGISTRY "%keySystem%" "!systemPath:?=%%%%!")
  303.  
  304. :END_RESTORE
  305. :: Replace ? with % so path is expanded
  306. set "userPath=!userPath:?=%%!"
  307. set "systemPath=!systemPath:?=%%!"
  308. endlocal & call set "newPath=%systemPath%%userPath%"
  309. exit /b
  310.  
  311.  
  312.  
  313. REM ********************************************************************************
  314. REM
  315. REM BUILD_PATH
  316. REM Description: Utility used by restore registry to buildup restore path
  317. REM
  318. REM ********************************************************************************
  319. :BUILD_PATH
  320. if "%~1" equ "USER PATH" set "restore=user" & exit /b
  321. if "%~1" equ "SYSTEM PATH" set "restore=system" & exit /b
  322. if Not Defined restore (call :ERROR "The backup file format is not valid?" & exit /b)
  323. set "pathVar=%~1"
  324. if "%restore%" equ "user" set "userPath=%userPath%%pathVar%;"
  325. if "%restore%" equ "system" set "systemPath=%systemPath%%pathVar%;"
  326.  
  327. exit /b
  328.  
  329.  
  330. REM ********************************************************************************
  331. REM
  332. REM GETLIST
  333. REM Description: Provides a list of paths in user or system or both paths
  334. REM              according to the scope switch. If the xpand switch /x is used
  335. REM              paths will be expanded. If the order switch /r is used the list is in reverse order.
  336. REM
  337. REM ********************************************************************************
  338. :GETLIST
  339. setlocal
  340. set "userPath="
  341. set "systemPath="
  342. set "heading=USER ^& SYSTEM PATH"
  343.  
  344. if "%scope%" equ "system" (set "heading=SYSTEM PATH" & goto LIST_SYSTEM)
  345. call :GETREGVAL "%keyUser%" path listPath
  346. call :CLEAN_PATH listPath "%listPath%"
  347.  
  348. if "%scope%" equ "user"  (set "heading=USER PATH" & goto LIST_OUTPUT)
  349. set "userPath=%listPath%"
  350.  
  351. :LIST_SYSTEM
  352. call :GETREGVAL "%keySystem%" path listPath
  353. call :CLEAN_PATH listPath "%listPath%"
  354. set "listPath=%listPath%%userPath%"
  355. :LIST_OUTPUT
  356. echo %heading%
  357. call :LIST_PATH "%listPath%" "%xpand%" "%order%" "%validate%"
  358. exit /b
  359.  
  360.  
  361. REM ********************************************************************************
  362. REM
  363. REM ADD action
  364. REM Description: Add a new path to the environment variable (default user path)
  365. REM              if that path does not already exist in either the system or user path.
  366. REM              If path is a valid file path then add its path. This allows drag and drop of files
  367. REM      to be used as the source of a path.
  368. REM
  369. REM ********************************************************************************
  370. :ADD
  371.  
  372. setlocal enabledelayedexpansion
  373. set "userPath="
  374. set "systemPath="
  375.  
  376.  
  377.  
  378. :: Need to make path expandable if ^% has been used
  379. call set "xpandPath=!fsPath:?=%%!"
  380. set "folderPath=%xpandPath%"
  381. if "%folderPath:~-1%" neq "\" (call set "folderPath=%folderPath%\")
  382. :: If path is a folder path then no file name has been provided
  383.  
  384. :: do not validate if validate=/nv
  385. if "%validate%" equ "/nv" goto IS_VALIDATED
  386. :: If valid folder path IS_VALIDATED
  387.  
  388. if exist "%folderPath%" goto IS_VALIDATED
  389. :: Can get valid folder path from a file path.
  390. :: However if dealing with expandable paths this may not be possible
  391. :: If fsPath not equal to xpandPath then we have an expandable path
  392. if "%fsPath%" neq "%xpandPath%" (call :ERROR "The folder path '%fsPath%' either does not exist or is invalid."
  393.                                  goto EXIT)
  394.  
  395. :: Now test if valid file path exists by removing the trailing '\'
  396. set "folderPath=%folderPath%###"
  397. call set "folderPath=!folderPath:\###=!"
  398. if not exist !folderPath! (call :ERROR "The folder path '%fsPath%' either does not exist or is invalid."
  399.                                  goto EXIT)
  400.  
  401. :: Now get the path portion
  402. call :GET_PATH "%xpandPath%" folderPath
  403.  
  404. if not exist "%folderPath%" (call :ERROR "The folder path '%fsPath%' either does not exist or is invalid."
  405.                             goto EXIT) else (call set "fsPath=%folderPath%" )
  406.  
  407. call :CHOOSE_YN "Do you wish to add '%folderPath%' to the PATH Y/N?"
  408. :: If no exitlist
  409. if errorlevel 2 (goto EXIT )
  410.  
  411. :IS_VALIDATED
  412.  
  413. :: First check if path already exists in PATH environment variable
  414. :: If it doesn't add it to PATH, however no messages as confusing if subsequently
  415. :: also adding to registry  
  416. call :CLEAN_PATH path "%path%"
  417.  
  418. call :PATH_FOUND "!fsPath!" "%path%"
  419.  
  420. :: Path does not exist so add
  421. if %errorlevel% equ 1 ( set "path=%path%!fsPath:?=%%!" & echo Path '!fsPath:?=%%!' added to PATH variable.) else (
  422.                         echo PATH variable already contains !fsPath:?=%%!)
  423.  
  424. echo Now adding path '!fsPath:?=%%!' to the Path environment variable
  425. call :GETREGVAL "%keyUser%" path userPath
  426.  
  427. call :GETREGVAL "%keySystem%" path systemPath
  428.  
  429. call :CLEAN_PATH userPath "%userPath%"
  430.  
  431. call :CLEAN_PATH systemPath "%systemPath%"
  432.  
  433. call :PATH_FOUND "!fsPath!" "%systemPath%%userPath%"
  434.  
  435. :: Path already exists so finish up
  436. if %errorlevel% equ 0 ( echo Registry already contains path !fsPath! &  goto END_ADD_PATH)
  437.  
  438.  
  439. if "%scope%" equ "user" (set "userPath=%userPath%!fsPath!") else (set "systemPath=%systemPath%!fsPath!")
  440.  
  441. call :CHOOSE_YN "Do you wish to make permanent change to the registry Y/N?"
  442. :: If no exit
  443. if errorlevel 2 (goto END_ADD_PATH )
  444. :: Now add to registry
  445.  
  446. :: Need to replace ? with %% in path in path so not expanded
  447. if "%scope%" == "user" ( call :UPDATE_REGISTRY "%keyUser%"  "!userPath:?=%%%%!;" & echo Path '!fsPath:?=%%!' added to user registry PATH.) else (
  448.              call :UPDATE_REGISTRY "%keySystem%"  "!systemPath:?=%%%%!;" & echo Path '!fsPath:?=%%!' added to system registry PATH.)
  449.  
  450. :END_ADD_PATH
  451. endlocal & call set "newpath=%path%"
  452. goto EXIT  
  453.  
  454. REM ********************************************************************************
  455. REM
  456. REM DELETE action
  457. REM Description: Delete a path from the environment variable.
  458. REM              Will first look in user path, if not found will look
  459. REM              in system path.
  460. REM
  461. REM ********************************************************************************
  462. :DELETE
  463. setlocal enabledelayedexpansion
  464. if "%fsPath%" neq "" goto DEL_START
  465. :: Handle case where just delete no existant paths
  466. if "%validate%" neq "/v" (call :ERROR "No path to delete or modifier switch /v missing." & goto EXIT)
  467. call :CLEAN_PATH path "%path%" "%validate%
  468. goto DEL_REG_ENTRIES
  469.  
  470. :DEL_START
  471. set "userPath="
  472. set "systemPath="
  473.  
  474. :: Need to determine if file has been dragged and dropped.
  475. :: First decide whether a file path or a folder
  476. :: If it ends with '\' then is folder  otherwise need to add '\' and
  477. :: check again
  478.  
  479. if "%fsPath:~-1%" equ "\" (goto FIND_PATH) else (set "folderPath=%fsPath%\")
  480.  
  481. :: If is folder  can ignore
  482. if exist "%folderPath%" (goto FIND_PATH)
  483. :: It is not a folder then check if it exists. If not may just be an invalid path so continue to find.
  484. :: If it exists it is a file that has been dragged and dropped.
  485. :: Thus need to extract the path otherwise simply continue
  486. if not exist "%fsPath%" goto FIND_PATH
  487. :: Now extract path
  488. call ::GET_PATH "%fsPath%" fsPath
  489.  
  490. :FIND_PATH
  491. echo Now removing path '!fsPath!' from the Path environment variable
  492. :: First check if path already exists in PATH environment variable
  493. :: If it doesn't add it to PATH, however no messages as confusing if subsequently
  494. :: also adding to registry  
  495. call :CLEAN_PATH path "%path%" "%validate%
  496.  
  497. call :PATH_FOUND "!fsPath!" "%path%"
  498.  
  499. :: Path does  exist so delete
  500. if %errorlevel% equ 0 ( set "path=!path:%foundPath%;=!")
  501.  
  502.  
  503. :DEL_REG_ENTRIES
  504. :: Delete registry entries
  505.  
  506. :: Need to make path expandable if ^% has been used
  507. call set "xpandPath=!fsPath:?=%%!"
  508.  
  509. :: Need to retrieve both user and system path
  510. call :GETREGVAL "%keyUser%" path userPath
  511. call :CLEAN_PATH userPath "%userPath%" "%validate%
  512. call :GETREGVAL "%keySystem%" path systemPath
  513. call :CLEAN_PATH systemPath "%systemPath%" "%validate%
  514.  
  515. if "%fsPath%" equ "" goto DEL_REGISTRY
  516. call :PATH_FOUND "%fsPath%" "!userPath!"
  517. :: If errorlevel = 0 then path found delete from userpath
  518. if %errorlevel% equ 0 (call set "userPath=!userPath:%foundPath%;=!" & set "scope=user" & goto DEL_REGISTRY)
  519.  
  520. call :PATH_FOUND "%fsPath%" "!systemPath!"
  521. if %errorlevel% equ 1 ( goto END_DEL_PATH)
  522. call set "systemPath=!systemPath:%foundPath%;=!"
  523. set "scope=system"
  524. :DEL_REGISTRY
  525. call :CHOOSE_YN "Do you wish to make permanent change to the registry Y/N?"
  526. :: If no exit
  527. if errorlevel 2 (goto END_DEL_PATH )
  528. :: Now add to registry
  529. echo userPath %userPath%
  530. echo systemPath %systemPath%
  531. :: Need to replace ? with %% in path in path so not expanded
  532. if "%scope%" == "user" ( call :UPDATE_REGISTRY "%keyUser%"  "!userPath:?=%%%%!") else ( call :UPDATE_REGISTRY "%keySystem%"  "!systemPath:?=%%%%!")
  533.  
  534. :END_DEL_PATH
  535.  
  536. echo Path '%fsPath%' removed from Path environment variable.
  537. endlocal & call set "newPath=%path%"
  538. goto EXIT
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546. REM ********************************************************************************
  547. REM
  548. REM REMOVE DUPLICATE PATHS
  549. REM Description:    Will remove duplicate paths from path list.
  550. REM Example:        call :REMOVE_DUPLICATE_PATHS "result variable name" "source path list" "target path list"
  551. REM             If no target path list will remove duplicates from the source path.
  552. REM             If a target path is provided will remove any paths in target path list that match source paths.
  553. REM
  554. REM ********************************************************************************
  555. :REMOVE_DUPLICATE_PATHS
  556.  
  557. setlocal enabledelayedexpansion
  558. set "destinationName=%~1"
  559. set "sourcePath=%~2"
  560. set targetPath=%sourcePath%
  561. set  "targetSupplied=false"
  562.  
  563. if "%~3" neq "" ( set "targetPath=%~3"
  564.         set "targetSupplied=true")
  565.  
  566. :LOOP_REMOVE_DUPLICATES
  567.  
  568. :: Loop through each path in inputPath    
  569. :: and set aPath to the path
  570.    for /f "delims=;" %%a in ("%sourcePath%") do (
  571.         set "aPath=%%a"
  572.  
  573.        
  574.    )
  575.  
  576.  
  577. set "targetPath=!targetPath:%aPath%;=!"
  578.  
  579. :: need to add back aPath to our targetPath
  580. :: if targetSupplied is false as need to retain at least one instance
  581. :: in file
  582. if "%targetSupplied%" equ "false" set "targetPath=!targetPath!%aPath%;"
  583.  
  584. :: if expandable need to expand
  585. :: note clean has replaced % with ? so must put back
  586. :: Check if first character=?
  587.  
  588. if "%aPath:~0,1%" equ "?" ( call set "xpand=!aPath:?=%%!" ) else (goto CONTINUE)
  589. set "targetPath=!targetPath:%xpand%;=!"
  590. set "sourcePath=!sourcePath:%xpand%;=!"
  591.  
  592.  
  593. :CONTINUE
  594. set "sourcePath=!sourcePath:%aPath%;=!"
  595.  
  596. if "!sourcePath!" neq "" ( goto LOOP_REMOVE_DUPLICATES)
  597.  
  598. endlocal & set "%destinationName%=%targetPath%"
  599.  
  600. exit /b
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607. goto EXIT
  608.  
  609. REM ********************************************************************************
  610. REM
  611. REM UTILITIES START HERE
  612. REM
  613. REM ********************************************************************************
  614.  
  615. REM ********************************************************************************
  616. REM
  617. REM CLEAN PATH
  618. REM Description: Cleans up paths by removing any spaces and quotes and also
  619. REM              will reset current path to match registry.
  620. REM Parameters: sourcePathName  - The variable to return the result
  621. REM         sourcePath  - The path string to clean
  622. REM             validate        - if /v will remove invalid paths or paths that don't exist
  623. REM ********************************************************************************
  624. :CLEAN_PATH
  625. :: setlocal local variable only in scope until  endlocal called
  626. :: EnableDelayedExpansion - a variable enclosed in "%" is evaluated at run time not at read time
  627. setlocal enabledelayedexpansion
  628. set "sourcePathName=%1"
  629. set "sourcePath=%~2"
  630.  
  631. REM Return if source path is empty
  632. if "%sourcePath%" equ "" (set "sourcePath=" & exit /b)
  633. if /i "%sourcePathName%" equ "systemPath" set "pathType=System Registry PATH value"
  634. if /i "%sourcePathName%" equ "userPath" set "pathType=User Registry PATH value"
  635. if /i "%sourcePathName%" equ "Path" set "pathType=Environment PATH variable"
  636. if "%~3" equ "" (set "validate=/nv") else (set "validate=%~3")
  637. :: Call to subroutine - need to remove any trailing spaces
  638. :: as this will create a path of spaces
  639. call :TRIM_RIGHT  sourcePath "%sourcePath%"
  640. :: Call to subroutine - need to remove any leading spaces
  641. :: as this will create a path of spaces
  642. call :TRIM_LEFT  sourcePath "%sourcePath%"
  643. :: Also need to make sure that last path ends with ;
  644. :: so add ; if it does not.
  645. if "%sourcePath:~-1%" neq ";" (
  646.     set "sourcePath=%sourcePath%;"
  647. )
  648.  
  649. rem set sourcepath=!sourcepath:"=!
  650.  
  651. :: if validation required
  652. if "%validate%" equ "/v" echo Checking for paths that don't exist in %pathType%.
  653. :LOOP_CLEAN
  654.  
  655. :: Loop through each path in sourcePath    
  656. :: and set aPath to the path
  657.    for /f "delims=; tokens=1*" %%a in ("%sourcePath%") do (
  658.         set "aPath=%%a"
  659.         set "sourcePath=%%b"
  660.  
  661.    )
  662.  
  663. ::echo %aPath%
  664. call :TRIM_RIGHT  aPath "!aPath!"
  665. call :TRIM_LEFT  aPath "!aPath!"
  666.  
  667. :: if validation required
  668. if "%validate%" neq "/v" goto BUILD_CLEANPATH
  669. :: if path exists continue with building up path else
  670.  
  671. call set "xPath=!aPath:?=%%!"
  672. if "!xPath:~-1!" neq "\" call set "xPath=!xPath!\"
  673. if exist "!xPath!" (goto BUILD_CLEANPATH )else ( echo !xPath! does not exist & goto CHECK_LOOP_CONDITION)
  674.  
  675. :BUILD_CLEANPATH
  676. if "%cleanPath%" equ "" (set "cleanPath=%aPath%;") else (set "cleanPath=!cleanPath!%aPath%;")
  677. :CHECK_LOOP_CONDITION
  678. if "!sourcePath!" neq "" goto LOOP_CLEAN
  679. ::echo %cleanPath%
  680. endlocal & call set %sourcePathName%=%cleanPath%
  681.  
  682. exit /b
  683.  
  684.  
  685. REM *******************************************************************************************************
  686. REM Trim character from end ofstring
  687. REM Example call :TRIM_RCHAR VariableName "The String" character
  688. REM *******************************************************************************************************
  689. :TRIM_RCHAR
  690. set "string=%~2###"
  691. set char=%~3
  692. setlocal enabledelayedexpansion
  693. set "string=!string:%char%###=!"
  694. endlocal & call set "string=%string%"
  695. set %1=%string%
  696. exit /b
  697.  
  698. REM *******************************************************************************************************
  699. REM Trim from end of line
  700. REM *******************************************************************************************************
  701. :TRIM_RIGHT
  702. set "string=%2"
  703. :: Remove quotes
  704. set string=###%string%###
  705. set string=%string:"###=%
  706. set string=%string:###"=%
  707. set string=%string:###=%
  708.  
  709.  
  710. :PROCESS_RIGHT_TRIM
  711. if "%string:~-1%" equ " " (
  712.         set "string=%string:~0,-1%"
  713.         goto PROCESS_RIGHT_TRIM
  714.        
  715. )
  716.  
  717. set %1=%string%
  718. exit /b
  719.  
  720. REM *******************************************************************************************************
  721. REM Trim from beginning of line
  722. REM *******************************************************************************************************
  723. :TRIM_LEFT
  724. set "string=%2"
  725. :: Remove quotes
  726. set string=###%string%###
  727. set string=%string:"###=%
  728. set string=%string:###"=%
  729. set string=%string:###=%
  730. :PROCESS_LEFT_TRIM
  731. if "%string:~0,1%" equ " " (
  732.     set "string=%string:~1%"
  733.         goto PROCESS_LEFT_TRIM
  734. )
  735. set %1=%string%
  736. exit /b
  737.  
  738. REM ********************************************************************************
  739. REM
  740. REM GETREGVAL
  741. REM Description: will return the value of a registry key value. Will substitute
  742. REM              a "?" for any "%" so that values are not automatically expanded.
  743. REM Parameters: key - the key string  to find in the registry
  744. REM         value - the key value to return
  745. REM     return - the name of environment variable to return the value.
  746. REM
  747. REM ********************************************************************************
  748. :GETREGVAL
  749. :: The key to query
  750. set "key=%~1"
  751. :: The value to find
  752. set "value=%2"
  753. :: The environment variable for return value
  754. set "return=%3"
  755.  
  756. :: No. of words in value
  757. Set count=1
  758. For  %%j in ("%value%") Do Set /A count+=1
  759. setlocal enabledelayedexpansion
  760. Set "regVal="
  761. REM FOR /F "tokens=%count%*" %%A IN ('REG.EXE QUERY "%key%" /V %value% 2^> nul') DO (SET "regVal=%%B")
  762. REM cannot pipe error to nul if we wish to detect error level
  763. REM For loop seems to bury the error level must pipe error to file
  764. :: Need to access reg twice - unable to access errorlevel in for loop?
  765. :: No longer concerned with registry error
  766. :: If registry corrupted ant path not discoverable - many other problems would exist
  767. ::REG.EXE QUERY "%key%" /V %value% > nul
  768. ::if errorlevel 1 exit /b 1
  769. FOR /F "tokens=%count%*" %%A IN ('REG.EXE QUERY "%key%" /V %value% 2^> nul') DO ( SET "regVal=%%B")
  770.  
  771. :: Was going to check for valid entries in path
  772. :: however, if not valid that is for user to decide.
  773. :: Provide mechanism in clean to eliminate invalid entries.
  774. :: substitute ? for % so that values not automatically expanded
  775. set "regval=!regVal:%%=?!"
  776. :: Remove quotes
  777. set regval=!regVal:"=!
  778. if "%regval%" equ "%%=?" (set "regVal=")
  779.  
  780. endlocal & Set "%return%=%regVal%"
  781.  
  782. exit /b 0
  783.  
  784.  
  785. REM *******************************************************************************************************
  786. REM
  787. REM UPDATE_REGISTRY
  788. REM Description: Update the registry with the given value
  789. REM
  790. REM *******************************************************************************************************
  791. :UPDATE_REGISTRY
  792. setlocal enabledelayedexpansion
  793. :: replace trailing '\'
  794. set "pathToUpdate=%~2"
  795.  
  796. REM check if empty path ie expand
  797. if "%pathToUpdate%" equ "?=%%" (set "pathToUpdate=")
  798.  
  799. if "%pathToUpdate:~-1%" equ "\" ( set "pathToUpdate=!pathToUpdate!###"
  800.                                   set "pathToUpdate=!pathToUpdate:\###=!"        )
  801.  
  802.  
  803.  
  804. reg add "%~1" /v path /t REG_EXPAND_SZ /d "%pathToUpdate%" /f
  805. :: No error action is necessary as GET_REGVAL always precedes UPDATE_REGISTRY
  806. if errorlevel 1 (echo Unable to update path for registry key %1, try executing as administrator?) else (echo Registry key "%~1" updated successfully.)
  807. exit /b
  808.  
  809.  
  810.  
  811. REM *******************************************************************************************************
  812. REM
  813. REM LIST PATH
  814. REM Description: Lists path sorted in either ascending or descending order.
  815. REM      This is done by saving list to file and using sort on file.
  816. REM Parameters: inputPath(required) - The path string to list
  817. REM         /x(optional) -  If present will expand any environment variables in the
  818. REM             path before listing, the default is to leave paths unexpanded.
  819. REM     /r(optional) -  If present will sort list in reverse order, the default is ascending order.
  820. REM             /v(optional) -  If validate = /v then indicate 'does not exist'    
  821. REM *******************************************************************************************************
  822. :LIST_PATH
  823. setlocal enabledelayedexpansion
  824.  
  825. set "mxpand=/"
  826. set "morder=/"
  827. set "mvalidate=/"
  828. :ARGUMENTS_LIST_PATH
  829. if "%~1"==""  ( goto START_LIST)
  830.     if "%~1" == "/" (goto SHIFT_LIST )
  831.     if "%~1" == "/x" (set "mxpand=/x" & goto SHIFT_LIST )
  832.     if "%~1" == "/r" (set "morder=/r" & goto SHIFT_LIST )
  833.     if "%~1" == "/v" (set "mvalidate=/v" & goto SHIFT_LIST )
  834.    :: else parameter a path if ends ?
  835.     set "isPath=%~1"  
  836.     if "%isPath:~-1%" == ";" (    set "inputPath=%~1" & goto SHIFT_LIST )
  837.  
  838.  
  839.    
  840. :SHIFT_LIST
  841. Shift
  842. goto ARGUMENTS_LIST_PATH
  843. :START_LIST
  844.  
  845. :: Create new list file
  846.  
  847. :LOOP_LIST
  848. :: Loop through each path in inputPath    
  849. :: and set aPath to the path
  850.    for /f "delims=; tokens=1*" %%a in ("%inputPath%") do (
  851.         set "aPath=%%a"sort
  852.         set "inputPath=%%b"
  853.  
  854.    )
  855. set "aPath=!aPath:?=%%!"
  856. if "%mvalidate%" neq "/v" goto CHECK_EXPAND
  857. call set "xpandPath=%aPath%"
  858. if not exist "%xpandPath%" set "aPath=%aPath% does not exist"
  859.  
  860. :CHECK_EXPAND
  861. if "%mxpand%" equ "/x" call echo %aPath% >> %Temp%\pathlist.txt
  862. if "%mxpand%" neq "/x" echo %aPath% >>  %Temp%\pathlist.txt
  863. if "%inputPath%" neq "" goto LOOP_LIST
  864. call sort %morder% <  %Temp%\pathList.txt
  865. del  %Temp%\pathlist.txt
  866. endlocal
  867. exit /b
  868.  
  869. REM *******************************************************************************************************
  870. REM
  871. REM CHOOSE_YN
  872. REM Description: Display simple yes/no choice to continue with action.
  873. REM              If userPrompt = /p Display prompt.
  874. REM              If userPrompt = /y Turn prompt off return yes
  875. REM              If userPrompt = /nT urn prompt off return no
  876. REM
  877. REM Example:  Call :CHOOSE_YN "prompt string here"
  878. REM Return:  Yes = errorlevel 1, No = errorlevel 2
  879. REM
  880. REM *******************************************************************************************************
  881. :CHOOSE_YN
  882. :: Exit if prompt mode is /p
  883. if "%userPrompt%" equ "/y" exit /b 1
  884. if "%userPrompt%" equ "/n" exit /b 2
  885. set /p "reply=%~1"
  886.  
  887. if /I "%reply%" equ "Y" (set "return=1") else (set "return=2")
  888. rem choice /C YN /M "%~1"
  889. exit /b %return%
  890.  
  891. REM *******************************************************************************************************
  892. REM
  893. REM PATH_FOUND
  894. REM Description: Used to find if a folder path exists in current path
  895. REM          If found errrlevel = 0 else if not found errorlevel = 1.
  896. REM              The found value may be retreived in the variable foundPath.
  897. REM *******************************************************************************************************
  898. :PATH_FOUND
  899. setlocal enabledelayedexpansion
  900.  
  901. set "pathVar=%~2"
  902. REM if path variable is empty then no paths so no need to look any further
  903. if "%pathVar%" equ  "" (exit /b 1)
  904.  
  905. set "pathToFind=%~1"
  906. :: if path does not end in slash make pathSlash '\'
  907. if "%pathToFind:~-1%" neq "\" (set "pathSlash=\") else (set "pathSlash=")
  908.  
  909. :PATH_LOOP
  910. for /f "delims=;" %%a in ("!pathVar!") do (
  911.         set "aPath=%%a"
  912.        
  913. )
  914.  
  915. :: Remove aPath from path for next iteration
  916. set "pathVar=!pathVar:%aPath%;=!"
  917. call :TRIM_RIGHT  trimPath "!aPath!"
  918. call :TRIM_LEFT  trimPath "!trimPath!"
  919.  
  920. :: if trimpath does not end in slash make trimSlash '\'
  921. if "%trimPath:~-1%" neq "\" (set "trimSlash=\") else (set "trimSlash=")
  922. rem echo %pathToFind%%pathSlash% %trimPath%%trimSlash%
  923. :: if  path to match matches aPath
  924. if /I "%pathToFind%%pathSlash%" equ "%trimPath%%trimSlash%" (
  925.     endlocal & call set "foundPath=%aPath%"
  926.     exit /b 0)
  927.  
  928. :: Now try expanding pathToFind
  929. call set xpathToFind=!pathToFind:?=%%!
  930. rem        echo !xpathToFind!%pathSlash% %trimPath%%trimSlash%
  931. if /I "!xpathToFind:?=%%!%pathSlash%" equ "!trimPath!%trimSlash%"  (
  932.     endlocal & call set "foundPath=%aPath%"
  933.     exit /b 0)
  934.  
  935. :: Finally try expanding trimPath
  936. call set "trimPath=!trimPath:?=%%!"
  937. rem    echo %pathToFind%%pathSlash% %trimPath%%trimSlash%
  938. if /I "%pathToFind%%pathSlash%" equ "%trimPath%%trimSlash%"  (
  939.     endlocal & call set "foundPath=%aPath%"
  940.     exit /b 0)
  941.  
  942.  
  943. if "!pathVar!" neq "" goto PATH_LOOP
  944. exit /b 1
  945.  
  946.  
  947. REM *******************************************************************************************************
  948. REM
  949. REM ERROR
  950. REM Description: Displays error message
  951. REM Example:     call :ERROR "Message here"
  952. REM
  953. REM *******************************************************************************************************
  954. :ERROR
  955. echo.
  956. echo %~1
  957. exit /b
  958.  
  959. REM *******************************************************************************************************
  960. REM
  961. REM GET_PATH
  962. REM Description: Returns a path given a file path
  963. REM Example:     call :GET_PATH [path] [return variable name]
  964. REM
  965. REM *******************************************************************************************************
  966. :GET_Path
  967. set "%2=%~dp1"
  968. exit /b
  969.  
  970. REM *******************************************************************************************************
  971. REM
  972. REM GET_FILE
  973. REM Description: Returns a file name given a file path
  974. REM Example:     call :GET_PATH [path] [return variable name]
  975. REM
  976. REM *******************************************************************************************************
  977. :GET_FILE
  978. set "%2=%~n1"
  979. exit /b
  980.  
  981. REM *******************************************************************************************************
  982. REM
  983. REM GET_FILENAME
  984. REM Description: Returns a file name with extension given a file path
  985. REM Example:     call :GET_PATH [path] [return variable name]
  986. REM
  987. REM *******************************************************************************************************
  988. :GET_FILE
  989. set "%2=%~nx1"
  990. exit /b
  991.  
  992.  
  993.  
  994. REM *******************************************************************************************************
  995. REM
  996. REM GET_EXT
  997. REM Description: Returns a file extension given a file path
  998. REM Example:     call :GET_PATH [path] [return variable name]
  999. REM
  1000. REM *******************************************************************************************************
  1001. :GET_EXT
  1002. set "%2=%~x1"
  1003. exit /b
  1004.  
  1005.  
  1006.  
  1007. REM *******************************************************************************************************
  1008. REM
  1009. REM HELP
  1010. REM Description: Provide help topic listing
  1011. REM *******************************************************************************************************
  1012. :HELP
  1013. if "%action%" equ "" goto HELP_MAIN
  1014. if "%action%" equ "LIST" goto HELP_LIST
  1015. if "%action%" equ "DELETE" goto HELP_DEL
  1016. if "%action%" equ "ADD" goto HELP_ADD
  1017. if "%action%" equ "BACKUP" goto HELP_BACKUP
  1018. if "%action%" equ "RESTORE" goto HELP_RESTORE
  1019. if "%action%" equ "CLEAN" goto HELP_CLEAN
  1020. if "%action%" equ "REFRESH" goto HELP_REFRESH
  1021. if "%action%" equ "PATHMGR" goto HELP_PATHMGR
  1022. if "%action%" equ "ENVIRONMENT" goto HELP_ENVIRONMENT
  1023.  
  1024. :HELP_MAIN
  1025. echo    %filename% Version %version%
  1026. echo        Tool to manage the PATH environment variable.
  1027. echo        Provides tools to add to , delete from, clean up,
  1028. echo        backup and restore the PATH environment variable
  1029. echo        either on a session basis or make permanent changes
  1030. echo        in the registry. To get help type "pathmgr /?".
  1031. echo        Whenever %filename% is run a temporary environment
  1032. echo        variable pathmgr_PATH is created that holds the
  1033. echo        current path of the %filename% command file. The
  1034. echo        /path switch may be used to add the path of %filename% itself.
  1035. echo.
  1036. echo    SYNTAX:     %fileName% [/?^|-?^|] [action][modifier][scope][path]
  1037. echo.
  1038. echo        [/?^|-?]        Display help, further help can be obtained on
  1039. echo                a topic by including an action switch.
  1040. echo                Example: pathmgr /? /List  will provide help on list action.
  1041. echo.
  1042. echo        [action]    The management action to perform. Perermited values are:-
  1043. echo                /list ^| /add ^| /del[ete] ^| /clean ^|/backup ^| /restore ^| /refresh ^| /path ^| /[env]ironment  
  1044. echo.
  1045. echo        [modifier]  Some actions may have additional options.  
  1046. echo                     /x: Expand environment variables.  
  1047. echo                     /r: Reverse order.
  1048. echo                     /v: validate a path(s) existence.
  1049. echo                    /nv: no validation of path(s) existence.
  1050. echo                     /p: display all prompts
  1051. echo                     /y: hide prompts and choose yes
  1052. echo                     /n: hide prompts and choose no
  1053. echo                     /+: add current %filename% to PATH
  1054. echo                     /-: delete current %filename% from PATH
  1055. echo.
  1056. echo        [scope]     The scope within the registry to perform the action.
  1057. echo                Permited values are:-  /user ^| /system ^| /all.
  1058. echo                The default scope is user as there are fewer
  1059. echo                policy restrictions. System and All scopes may require
  1060. echo                administrative privileges.
  1061. echo.
  1062. echo        [path]      Depending on context this will be either a folder or file path.
  1063. echo                If the path is the only argument then the default action is /add.
  1064. echo                Environment variables may be used in paths. The easiest way to do this
  1065. echo                is to enclose the variable name within question marks '?' rather than
  1066. echo                using the percent sign '%', as an example ?systemroot?.
  1067. echo                This practice has the advantage of working either from the commandline or
  1068. echo                working within a batch. The alternative is to escape the variable as below.
  1069. echo                If used on the command line use:-  pathmgr /add ?systemroot?" or
  1070. echo                pathmgr /add "^%%systemroot^%%". If used within a batch use
  1071. echo                pathmgr /add   "?systemroot?" or pathmgr /add "%%%%systemroot%%%%".
  1072. echo                If these escaped paths are not used the path will be expanded prior to being
  1073. echo                interpreted so if you really want the path to be %systemroot% without escaping
  1074. echo                the path will be added as "c:\windows".
  1075. echo.
  1076. goto EXIT
  1077.  
  1078. :HELP_PATH
  1079. echo        [path]      Environment variables may be used in paths. The easiest way to do this
  1080. echo                is to enclose the variable name within question marks '?' rather than
  1081. echo                using the percent sign '%', as an example ?systemroot?.
  1082. echo                This practice has the advantage of working either from the commandline or
  1083. echo                working within a batch. The alternative is to escape the variable as below.
  1084. echo                If used on the command line use:-  pathmgr /add ?systemroot?" or
  1085. echo                pathmgr /add "^%%systemroot^%%". If used within a batch use
  1086. echo                pathmgr /add   "?systemroot?" or pathmgr /add "%%%%systemroot%%%%".
  1087. echo                If these escaped paths are not used the path will be expanded prior to being
  1088. echo                interpreted so if you really want the path to be %systemroot% without escaping
  1089. echo                the path will be added as "c:\windows".
  1090. echo.
  1091. echo.
  1092. exit /b
  1093.  
  1094. :HELP_LIST
  1095. echo        LIST PATHS
  1096. echo        Displays a list of paths in user or system or both registry paths.
  1097. echo        according to the scope switch.
  1098. echo.
  1099. echo        SYNTAX:     %fileName% ^/list [scope][modifiers]
  1100. echo.
  1101. echo        [scope]     ^/user^|^/system^|^/all
  1102. echo.
  1103. echo        [modifiers] /x expand all paths before display.
  1104. echo                /r display paths in reverse order.
  1105. echo                /v paths that are not valid are indicated with 'does not exist'.
  1106. goto EXIT
  1107.  
  1108. :HELP_ENVIRONMENT
  1109. echo        ENVIRONMENT
  1110. echo        Displays a list of the current path environment variable.
  1111. echo.
  1112. echo        SYNTAX:     %fileName% ^/env[ironment] [modifiers]
  1113. echo.
  1114. echo        [modifiers] /r display paths in reverse order.
  1115. echo                /v paths that are not valid are indicated with 'does not exist'.
  1116. goto EXIT
  1117.  
  1118.  
  1119. :HELP_DEL
  1120. echo        DELETE PATH  
  1121. echo        Deletes a given path from the PATH variable.
  1122. echo        will search both user and system path.
  1123. echo        By default will prompt if you want to make a permanaent
  1124. echo        change to the registry. For changes to system paths you
  1125. echo        may require adminstrative privileges.
  1126. echo.
  1127. echo        SYNTAX:     %fileName% /del[ete] [modifiers] [path]
  1128. echo.
  1129. echo        [modifiers] /x Expand all paths before display.
  1130. echo                /r Display paths in reverse order.
  1131. echo                /p Display prompt to make registry change.
  1132. echo                /y Do not ask, just make registry change (batch mode).  
  1133. echo                /n Do not make registry change (batch mode).  
  1134. echo                /v Delete any paths that are not valid (do not need to provide any path).
  1135. echo.
  1136. call :HELP_PATH
  1137. echo.
  1138. echo        For drag and drop operation it is recommended that you create a shortcut file
  1139. echo        and include the following target - pathmgr /delete /y. This will delete the dropped path
  1140. echo        from whatever path it is found in the registry without prompting. However, if the path is in
  1141. echo        the system path you will require administrator privileges. In Vista and Windows7 this means
  1142. echo        that the shortcut needs to be checked as 'run as administrator'.
  1143. echo.
  1144. goto EXIT
  1145.  
  1146. :HELP_ADD
  1147. echo        ADD PATH     
  1148. echo        Will add a given path to the PATH variable.
  1149. echo        By default the user HKCU path variable is used.
  1150. echo        Add is the default action if no action argument is provided
  1151. echo        but a valid path is provided. This makes it possible to have
  1152. echo        drag and drop. If a file path is dropped only the folder path is added.
  1153. echo        By default Add will prompt if you want to make a permanaent
  1154. echo        change to the registry. This behaviour can be changed by applying
  1155. echo        the /y or /n modifiers.
  1156. echo.
  1157. echo        SYNTAX:     %fileName% /add [scope] [modifiers] [path]
  1158. echo.
  1159. echo        [scope]     ^/user^|^/system
  1160. echo.
  1161. echo        [modifiers]  /v By default will validate a path before adding.
  1162. echo                /nv Do not validate any paths before adding them.
  1163. echo                 /p Display prompt to make registry change.
  1164. echo                 /y Do not ask, just make registry change (batch mode).  
  1165. echo                 /n Do not make registry change (batch mode).  
  1166. echo.
  1167. call :HELP_PATH
  1168. echo.
  1169. echo        For drag and drop operation it is recommended that you create a shortcut file
  1170. echo        and include the following target - pathmgr /add /y. This will add the dropped path to
  1171. echo        the user path in the registry without prompting.
  1172. echo.
  1173. goto EXIT
  1174.  
  1175. :HELP_BACKUP
  1176. echo        BACKUP
  1177. echo        Backs up either user or system or both paths.
  1178. echo        By default will backup the user path only.
  1179. echo        Must use the /all scope switch to backup both user and system.
  1180. echo.
  1181. echo        SYNTAX:     %fileName% /backup [scope] [path]
  1182. echo.
  1183. echo        [scope]     ^/user^|^/system^|^/all
  1184. echo.
  1185. call :HELP_PATH
  1186. echo                The path requires an output file path, a folder path will result in an error.
  1187. echo                Each path in the PATH variable is output on its own line in the output file.
  1188. echo                User paths are under heading 'USER PATH' and system paths are under the
  1189. echo                heading 'SYSTEM PATH'.
  1190. goto EXIT
  1191.  
  1192. :HELP_RESTORE
  1193. echo        RESTORE
  1194. echo        Restores paths from a backup text file.
  1195. echo.
  1196. echo        SYNTAX:     %fileName% ^/restore [path]
  1197. echo.
  1198. call :HELP_PATH
  1199. echo                The path requires a valid backup file path.
  1200. echo                restore process is aborted if the first line in backup file is not
  1201. echo                either 'USER PATH' or 'SYSTEM_PATH'.
  1202. echo.              
  1203. goto EXIT
  1204.  
  1205. :HELP_CLEAN
  1206. echo        CLEAN
  1207. echo        Will clean up the PATH variable in the registry by removing
  1208. echo        trailing and leading spaces, remove any enclosing double quotes,
  1209. echo        remove duplicate paths and if /v specified will remove invalid paths.
  1210. echo        The scope indicates wheteher user, system or both paths are cleaned.
  1211. echo        By default will prompt if you want to make a permanaent
  1212. echo        change to the registry. Note that any changes made to the path variable
  1213. echo        not made permanent in the registry will be lost.
  1214. echo        If you do not have administrator privileges you will not be able
  1215. echo        to make changes to the system path.
  1216. echo.
  1217. echo        SYNTAX:     %fileName% /clean [scope] [modifiers]
  1218. echo.
  1219. echo        [scope]     ^/user^|^/system^|^/all
  1220. echo.
  1221. echo        [modifiers]  /v Will also delete any invalid paths
  1222. echo                 /p Display prompt to make registry change.
  1223. echo                 /y Do not ask, just make registry change (batch mode).  
  1224. echo                 /n Do not make registry change (batch mode).  
  1225. echo.
  1226. goto EXIT
  1227.  
  1228. :HELP_REFRESH
  1229. echo        REFRESH
  1230. echo        Will refresh the current environment PATH variable with
  1231. echo        the user and system paths in the registry.Any changes
  1232. echo        made to the path variable made in the registry will be lost.  
  1233. echo.
  1234. echo        SYNTAX:     %fileName% /refresh
  1235. goto EXIT
  1236.  
  1237. :HELP_PATHMGR
  1238. echo        PATH
  1239. echo        Will display the current path of %filename%.
  1240. echo        By using the modifier /+ or /- you can also add or delete
  1241. echo        the %filename% path from the PATH variable.
  1242. echo.
  1243. echo        SYNTAX:     %fileName% /path [scope] [modifiers]
  1244. echo.
  1245. echo        [scope]     ^/user^|^/system^|/all
  1246. echo.
  1247. echo        [modifiers]  /+: add current %filename% to PATH
  1248. echo                 /-: delete current %filename% from PATH
  1249. echo                 /p Display prompt to make registry change.
  1250. echo                 /y Do not ask, just make registry change (batch mode).  
  1251. echo                 /n Do not make registry change (batch mode).  
  1252. echo.
  1253.  
  1254. goto EXIT
  1255.  
  1256.  
  1257.  
  1258. :EXIT
  1259. echo.
  1260. if "%help%" equ "false" ( echo %fileName% %action% action complete.
  1261.               echo. )
  1262. if defined newPath  (endlocal &  set "path=%newPath%" & set "pathmgr_Path=%pathmgrPath%") else (endlocal & set "pathmgr_Path=%pathmgrPath%")
  1263.  
  1264. @echo on
  1265.  
  1266.  
Add Comment
Please, Sign In to add comment