-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathSanitizer.bat
More file actions
79 lines (61 loc) · 2.89 KB
/
pathSanitizer.bat
File metadata and controls
79 lines (61 loc) · 2.89 KB
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
@echo off
REM Windows 7 Weather Gadget Repair Tool
REM https://www.github.com/kloverde/Win7WeatherGadgetRepairTool
REM Copyright (c) 2015 Kurtis LoVerde
REM All rights reserved.
REM
REM See LICENSE for this software's licensing terms.
REM This script trims spaces from a path and replaces double backslashes
REM with a single backslash. Windows is inconsistent with how it copes
REM with concurrent backslashes and with how it reports paths. This
REM causes chaos. For example:
REM
REM * Windows Explorer doesn't forgive double backslashes. Try it:
REM type C:\\Windows into an Explorer window's path textbox. You'll
REM get an error.
REM
REM * CMD.exe does forgive double backslashes. Try it: type
REM "cd C:\\Windows" into a prompt; it will work.
REM
REM * Environment variables like %USERPROFILE% are returned without a
REM trailing backslash. Try it: type "echo %userprofile%" into a
REM command prompt.
REM
REM * CMD.exe-generated variables, such as the directory of the
REM currently-running script, are returned WITH a trailing
REM backslash. Try it: put "echo %~dp0" in a script.
REM
REM In the case of Windows 7 Weather Gadget Repair Tool's installer,
REM %~dp0 is used to construct a path which is saved in the registry.
REM Constructing the path without double backslashes would look like:
REM %~dp0TheRestOfThePath. This would look like a mistake to anyone
REM reading the code; you'd expect to see %~dp0\TheRestOfThePath.
REM That, however, results in a double backslash, which Windows can't
REM read. It seems that only CMD.exe can cope with it.
REM
REM Given the inconsistency with how paths are reported and handled,
REM and my complete lack of confidence that the inconsistency will
REM always be consistent (how's that for a play on words), I chose to
REM write correct-looking code and create this sanitizing script to
REM fix any potential problems with the paths it generates.
REM
REM This script stores the sanitized path in %sanitized_path%.
set str=%1
REM echo Received : [%str%]
REM Step 1: Remove leading and trailing quotes.
REM
REM If a path containing spaces was passed in, quotes would need to be used to ensure the
REM string is interpreted as a single argument. This has the side effect of the quotes
REM becoming part of the argument.
for /f "useback tokens=*" %%a in ( '%str%' ) do set str=%%~a
REM echo Trim quotes : [%str%]
REM Step 2: Trim leading spaces
for /f "tokens=* delims= " %%a in ( "%str%" ) do set str=%%a
REM echo Trim leading spaces : [%str%]
REM Step 3: Trim trailing spaces
for /l %%a in ( 1, 1, 1024 ) do if "!str:~-1!"==" " set str=!str:~0,-1!
REM echo Trim trailing spaces : [%str%]
REM Step 4: Convert double backslashes to single backslashes
set str=%str:\\=\%
REM echo Fix double backslash : [%str%]
set sanitized_path=%str%