Setting the file. One moment. Library · Batch Files · github/awesome-copilot · Skills Docs(opens in a new tab)
assets/library.txt
Text·188 lines·7 KB
19
set "_helpLinesMyLib=17"
20
21:: ========================================================================
22:: TEMPLATE INSTRUCTIONS
23:: 1. Find/Replace "myLib" with your library name (camelCase).
24:: 2. Find/Replace "MyLib" with your library name (PascalCase).
25:: 3. Add your own :_funcNameMyLib labels below.
26:: 4. Update the help block above (lines 2-17) for your library.
27:: 5. Add any new variables to :_removeBatchVariablesMyLib.
28:: ========================================================================
29
30:: Route to the requested function.
31set "_funcMyLib=%~1"
32set "_argOneMyLib=%~2"
33set "_argTwoMyLib=%~3"
34set "_argThreeMyLib=%~4"
35
36if "%_funcMyLib%"=="/?" call :_showHelpMyLib & goto _removeBatchVariablesMyLib
37if /i "%_funcMyLib%"=="-h" call :_showHelpMyLib & goto _removeBatchVariablesMyLib
38if /i "%_funcMyLib%"=="--help" call :_showHelpMyLib & goto _removeBatchVariablesMyLib
39
40if /i "%_funcMyLib%"=="trimWhitespace" call :_trimWhitespaceMyLib & goto _removeBatchVariablesMyLib
41if /i "%_funcMyLib%"=="toLower" call :_toLowerMyLib & goto _removeBatchVariablesMyLib
42if /i "%_funcMyLib%"=="getTimestamp" call :_getTimestampMyLib & goto _removeBatchVariablesMyLib
43if /i "%_funcMyLib%"=="logMessage" call :_logMessageMyLib & goto _removeBatchVariablesMyLib
44if /i "%_funcMyLib%"=="padRight" call :_padRightMyLib & goto _removeBatchVariablesMyLib
45
46echo ERROR: Unknown function "%_funcMyLib%". Run "%~n0 /?" for usage.
47goto _removeBatchVariablesMyLib
48
49:: ========================================================================
50:: LIBRARY FUNCTIONS
51:: ========================================================================
52
53:_trimWhitespaceMyLib
54 REM Trim leading and trailing spaces from a variable.
55 REM %_argOneMyLib% = name of the variable to trim (passed by name).
56 if not defined _argOneMyLib goto :eof
57 setlocal EnableDelayedExpansion
58 set "_valMyLib=!%_argOneMyLib%!"
59 REM Trim leading spaces.
60 for /f "tokens=* delims= " %%a in ("!_valMyLib!") do set "_valMyLib=%%a"
61 REM Trim trailing spaces.
62 :_trimTrailingMyLib
63 if "!_valMyLib:~-1!"==" " (
64 set "_valMyLib=!_valMyLib:~0,-1!"
65 goto _trimTrailingMyLib
66 )
67 endlocal & set "%_argOneMyLib%=%_valMyLib%"
68goto :eof
69
70:_toLowerMyLib
71 REM Convert a variable's value to lowercase.
72 REM %_argOneMyLib% = name of the variable to convert (passed by name).
73 if not defined _argOneMyLib goto :eof
74 setlocal EnableDelayedExpansion
75 set "_valMyLib=!%_argOneMyLib%!"
76 set "_valMyLib=!_valMyLib:A=a!"
77 set "_valMyLib=!_valMyLib:B=b!"
78 set "_valMyLib=!_valMyLib:C=c!"
79 set "_valMyLib=!_valMyLib:D=d!"
80 set "_valMyLib=!_valMyLib:E=e!"
81 set "_valMyLib=!_valMyLib:F=f!"
82 set "_valMyLib=!_valMyLib:G=g!"
83 set "_valMyLib=!_valMyLib:H=h!"
84 set "_valMyLib=!_valMyLib:I=i!"
85 set "_valMyLib=!_valMyLib:J=j!"
86 set "_valMyLib=!_valMyLib:K=k!"
87 set "_valMyLib=!_valMyLib:L=l!"
88 set "_valMyLib=!_valMyLib:M=m!"
89 set "_valMyLib=!_valMyLib:N=n!"
90 set "_valMyLib=!_valMyLib:O=o!"
91 set "_valMyLib=!_valMyLib:P=p!"
92 set "_valMyLib=!_valMyLib:Q=q!"
93 set "_valMyLib=!_valMyLib:R=r!"
94 set "_valMyLib=!_valMyLib:S=s!"
95 set "_valMyLib=!_valMyLib:T=t!"
96 set "_valMyLib=!_valMyLib:U=u!"
97 set "_valMyLib=!_valMyLib:V=v!"
98 set "_valMyLib=!_valMyLib:W=w!"
99 set "_valMyLib=!_valMyLib:X=x!"
100 set "_valMyLib=!_valMyLib:Y=y!"
101 set "_valMyLib=!_valMyLib:Z=z!"
102 endlocal & set "%_argOneMyLib%=%_valMyLib%"
103goto :eof
104
105:_getTimestampMyLib
106 REM Write a YYYY-MM-DD_HH-MM-SS timestamp into the named variable.
107 REM %_argOneMyLib% = name of the output variable.
108 REM NOTE: Uses %DATE% and %TIME% which are locale-dependent. The parsing
109 REM below assumes US-style format (e.g., "Fri 04/18/2026" or "04/18/2026").
110 REM Adjust the substring offsets for your locale, or use PowerShell for
111 REM a locale-independent alternative:
112 REM for /f %%a in ('powershell -nop -c "Get-Date -F yyyy-MM-dd_HH-mm-ss"') do set "var=%%a"
113 if not defined _argOneMyLib goto :eof
114 setlocal EnableDelayedExpansion
115 REM Parse date — strip leading day name if present (e.g., "Fri ").
116 set "_dtMyLib=%DATE%"
117 if "!_dtMyLib:~3,1!"==" " set "_dtMyLib=!_dtMyLib:~4!"
118 set "_stampMyLib=!_dtMyLib:~6,4!-!_dtMyLib:~0,2!-!_dtMyLib:~3,2!"
119 REM Parse time — replace leading space with 0 for single-digit hours.
120 set "_tmMyLib=%TIME: =0%"
121 set "_stampMyLib=!_stampMyLib!_!_tmMyLib:~0,2!-!_tmMyLib:~3,2!-!_tmMyLib:~6,2!"
122 endlocal & set "%_argOneMyLib%=%_stampMyLib%"
123goto :eof
124
125:_logMessageMyLib
126 REM Write a timestamped log line to stdout.
127 REM %_argOneMyLib% = level (INFO, WARN, ERROR)
128 REM %_argTwoMyLib% = message text
129 REM NOTE: Uses %DATE% and %TIME% (locale-dependent). See :_getTimestampMyLib.
130 setlocal EnableDelayedExpansion
131 set "_dtMyLib=%DATE%"
132 if "!_dtMyLib:~3,1!"==" " set "_dtMyLib=!_dtMyLib:~4!"
133 set "_tmMyLib=%TIME: =0%"
134 set "_tsMyLib=!_dtMyLib:~6,4!-!_dtMyLib:~0,2!-!_dtMyLib:~3,2! !_tmMyLib:~0,2!:!_tmMyLib:~3,2!:!_tmMyLib:~6,2!"
135 echo [!_tsMyLib!] [%_argOneMyLib%] %_argTwoMyLib%
136 endlocal
137goto :eof
138
139:_padRightMyLib
140 REM Pad a string to a given width with trailing spaces.
141 REM %_argOneMyLib% = the string to pad
142 REM %_argTwoMyLib% = desired total width
143 if not defined _argOneMyLib goto :eof
144 if not defined _argTwoMyLib goto :eof
145 setlocal EnableDelayedExpansion
146 set "_valMyLib=%_argOneMyLib%"
147 set "_padMyLib=%_valMyLib% "
148 set "_padMyLib=!_padMyLib:~0,%_argTwoMyLib%!"
149 echo !_padMyLib!
150 endlocal
151goto :eof
152
153:: ========================================================================
154:: HELP
155:: ========================================================================
156
157:_showHelpMyLib
158 echo:
159 for /f "skip=1 delims=" %%a in ('findstr /n "^" "%~f0"') do (
160 set "_line=%%a"
161 setlocal EnableDelayedExpansion
162 for /f "delims=:" %%n in ("!_line!") do set "_lineNum=%%n"
163 if !_lineNum! GTR %_helpLinesMyLib% (
164 endlocal
165 goto :eof
166 )
167 set "_text=!_line:*:=!"
168 if defined _text (
169 echo !_text:~4!
170 ) else (
171 echo:
172 )
173 endlocal
174 )
175goto :eof
176
177:: ========================================================================
178:: CLEANUP — Remove all batch variables.
179:: ========================================================================
180:_removeBatchVariablesMyLib
181 set _helpLinesMyLib=
182 set _funcMyLib=
183 set _argOneMyLib=
184 set _argTwoMyLib=
185 set _argThreeMyLib=
186 set _valMyLib=
187 REM Append new variables above this line.
188 exit /b