Description: |
call:append str file line |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
|
:append str file line -- appends a string to a specific line in a text file
:: -- str [in] - string to be appended
:: -- file [in] - file name to append the string to
:: -- line [in] - line number to append the string to, first line is 1, omit for last line
:$created 20060101 :$changed 20080219 :$categories FileManipulation
:$source https://www.dostips.com
SETLOCAL
set ap=%~1
set f=%~2
set c=%~3
if not defined c (
set c=0
for /f %%a in ('find /v /c ""^<"%f%"') do set c=%%a
)
(for /f "tokens=1,* delims=:" %%a in ('findstr /v /n "$$"^<"%f%"') do (
if "%%a"=="%c%" (echo.%%b%ap%) ELSE echo.%%b
))>"%temp%.\t0815.txt"
move /y "%temp%.\t0815.txt" "%f%"
EXIT /b
|
|
Description: |
call:bannerPingPong varref |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:bannerPingPong varref -- moves text in varref one step left or right and updates title
:: -- varref [in,out] - variable name with banner text, format: "Banner Text------"
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set s=!%~1: =-!
if "!s:~-1!" NEQ "-" if "!s:~-1!" NEQ "+" set s=!s!--------
set d=!s:~-1!
if "!s:~0,1!" NEQ "-" set d=+
if "!s:~-2,1!" NEQ "-" set d=-
if "!d!"=="+" (set s=-!s:~0,-2!+) ELSE (set s=!s:~1,-1!--)
TITLE !s!
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" SET %~1=%s%
)
EXIT /b
|
|
Description: |
call:bannerRotate varref |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:bannerRotate varref -- rotates text in varref one step and updates title
:: -- varref [in,out] - variable name with banner text, format: "Banner Text------"
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set s=!%~1: =-!
set s=!s:~1!!s:~0,1!
TITLE !s!
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" SET %~1=%s%
)
EXIT /b
|
|
Description: |
call:choiceListInput ret list title max |
Script: |
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.
|
:choiceListInput ret list title max -- lets the user choose from list of last entered values
:: -- ret [out] - varref returns input value
:: -- list [in,out] - varref with choice list, returns trimmed reordered choice list
:: -- title [in] - list title
:: -- max [in] - maximum number of list entries
:$created 20060101 :$changed 20080219 :$categories Input
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set l=,!%~2!,& rem.-- get the choice list
set t=%~3& rem.-- get the list title
set c=%~4& rem.-- get the list entry count limit
set m=& rem.-- a message
set l2=,
set v=
:choiceListInput_LOOP1
echo.%t%
set i=0&for /f "delims=" %%a in ('"echo.%l:,=&echo.%"') do (
set /a i+=1
set l2=!l2!!i!;%%~a,
set v=%%~a
echo. !i! - %%~a
)
if "%m%" NEQ "" echo. ^>^> %m%
echo. Make a choice or enter a new value [%v%]
set v1=%v%
set /p v1= :
echo.
set v2=!v1!&set v2=!v2:,=!&set v2=!v2:@=!&set v2=!v2:;=!&set v2=!v2:"=!
rem.--reject entry with illegal character
if "!v1!" NEQ "!v2!" (
set m=Note: ,;@" and empty string not allowed. Try again.
goto:choiceListInput_LOOP1
)
rem.--if first character is minus then remove the entry
set remove=&if "%v1:~0,1%"=="-" set remove=y&set v1=%v1:~1%
set v=%v1%
rem.--if number chosen then find corresponding value
set l3=!l2:,%v%;=,@!
if "%l3%" NEQ "%l2%" (
for /f "delims=@ tokens=2" %%a in ("!l3!") do set l3=%%a
for /f "delims=," %%a in ("!l3!") do set v=%%a
)
rem.--remove value from list if exist
set l3=%l%
set l=!l:,%v%,=,!
if "%remove%"=="y" (
if "%l%"=="%l3%" (set m='%v%' cannot be removed from list
) ELSE (set m='%v%' has been removed from list)
goto:choiceListInput_LOOP1
)
if "%l%"=="%l3%" echo. ^>^>'%v%' has been added to the list
rem.--add to the value to the end
set l=!l:~1!%v%
rem.--enforce the list entry count limit if requested
if "%c%" NEQ "" (
set i=0&for %%a in (%l%) do set /a i+=1
if /i !i! GTR !c! (
for /f "delims=, tokens=1,*" %%a in ("!l!") do (
set l=%%b
echo. ^>^>'%%a' dropped out of the list
)
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%v%) ELSE (echo.%v%)
IF "%~2" NEQ "" (SET %~2=%l%) ELSE (echo.%l%)
)
EXIT /b
|
|
Description: |
call:CmpFTime op file1 file2 attr1 attr2 |
Script: |
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.
|
:CmpFTime op file1 file2 attr1 attr2 -- compares the time of two files, succeeds if condition is met, fails otherwise
:: -- op [in] - compare operator, see 'IF /?', i.e.EQU, NEQ, LSS, LEQ, GTR, GEQ
:: -- fileL [in] - file name, left side of comparisation
:: -- file2 [in] - file name, right side of comparisation
:: -- attrL [in,opt] - time field to be used for fileL, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:: -- attrR [in,opt] - time field to be used for fileR, default is attrL
:$created 20060101 :$changed 20080219 :$categories DateAndTime,FileOperation
:$source https://www.dostips.com
SETLOCAL
set op=%~1
set fileL=%~2
set fileR=%~3
set attrL=%~4
set attrR=%~5
if "%op%"=="" set op===
if "%attrL%"=="" set attrL=/tw
if "%attrR%"=="" set attrR=%attrL%
for /f "tokens=1-6 delims=/: " %%a in ('"dir %attrL% /-c "%fileL%"|findstr "^^[0-1]""') do (
set TL=%%c%%a%%b%%f%%d%%e
)
for /f "tokens=1-6 delims=/: " %%a in ('"dir %attrR% /-c "%fileR%"|findstr "^^[0-1]""') do (
set TR=%%c%%a%%b%%f%%d%%e
)
if "%TL%" %op% "%TR%" (rem.) ELSE set=2>NUL
EXIT /b
|
|
Description: |
call:date2jdate JD YYYY MM DD |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
|
:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
:: -- JD [out] - julian days
:: -- YYYY [in] - gregorian year, i.e. 2006
:: -- MM [in] - gregorian month, i.e. 12 for december
:: -- DD [in] - gregorian day, i.e. 31
:$reference https://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
|
|
Description: |
call:dayOfYear JD DateStr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
|
:dayOfYear JD DateStr -- returns the day of the year, i.e. 1 for 1/1/2008, 266 for 12/31/2008
:: -- day [out,opt] - variable name to store resulting day of the year
:: -- DateStr [in,opt] - date string, e.g. "3/31/2006" or "Fri 03/31/2006" or "31.3.2006", or omit form current date
:$reference https://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20080612 :$changed 20080612 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL
set "DateStr=%~2"&if "%~2"=="" set "DateStr=%date%"
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
set /a "yy=10000%yy% %%10000,mm=1,dd=1"
set /a JD-=-1+dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
|
|
Description: |
call:DeleteIfOld name days tnow |
Dependencies: |
:ftime |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
|
:DeleteIfOld name days tnow -- deletes file or directory if older than given number of days
:: -- name [in] - name of file or directory
:: -- days [in] - number of days to expire
:: -- tnow [in] - today's date in julia days
:$created 20060101 :$changed 20080219 :$categories DateAndTime,FileOperation
:$source www.DosTips.com
SETLOCAL
set "days=%~2"
set "tnow=%~3"
call:ftime tfile "%~1"
set /a "diff=tnow-tfile"
if %diff% LEQ %days% EXIT /b
set "attr=%~a1"
rem ECHO.%attr%, %attr:~0,1%, %~nx1 is %diff% days old
if /i "%attr:~0,1%"=="d" (
rd /Q /S "%~1"
) ELSE (
del /Q "%~1"
)
EXIT /b
|
|
Description: |
call:doProgress |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
|
:doProgress -- displays the next progress tick
:$created 20060101 :$changed 20080327 :$categories Progress
:$source https://www.dostips.com
set /a "ProgressCnt+=1"
SETLOCAL ENABLEDELAYEDEXPANSION
set /a "per100=100*ProgressCnt/ProgressMax"
set /a "per10=per100/10"
set /a "per10m=10-per100/10-1"
set "P=%per100%%%"
set "PP="
for /l %%N in (0,1,%per10%) do call set "PP=%%PP%%*"
for /l %%N in (%per10%,1,9) do call set "PP=%%PP%% "
set "PPP="
for /l %%N in (0,1,%per10m%) do call set "PPP=%%PPP%%*"
set "ProgressFormat=%ProgressFormat:[P]=!P!%"
set "ProgressFormat=%ProgressFormat:[PP]=!PP!%"
set "ProgressFormat=%ProgressFormat:[PPP]=!PPP!%"
title %ProgressFormat%
EXIT /b
|
|
Description: |
call:dumpArr arr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8.
|
:dumpArr arr -- dumps the array content / under construction
:: -- arr [in] - array name
:$created 20060101 :$changed 20080219 :$categories Array
:$source https://www.dostips.com
SETLOCAL
call set i=%%%~1[#]%%
for /l %%n in (1,1,%i%) do call echo."%%%~1[%%n]%~2%%"
EXIT /b
|
|
Description: |
call:echo col txt |
Dependencies: |
:getColorCode |
Script: |
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.
|
:echo col txt -- echoes text in a specific color
:: -- col [in] - color code, append a DOT to omit line break, call 'color /?' for color codes
:: -- txt [in] - text output
:$created 20060101 :$changed 20080219 :$categories Echo,Color
:$source https://www.dostips.com
SETLOCAL
for /f "tokens=1,*" %%a in ("%*") do (
set col=%%a
set txt=%%~b
)
set cr=Y
if "%col:~-1%"=="." (
set cr=N
set col=%col:~0,-1%
)
call:getColorCode "%col%" col
set com=%temp%.\color%col%.com
if not exist "%com%" (
echo.N %COM%
echo.A 100
echo.MOV BL,%col%
echo.MOV BH,0
echo.MOV SI,0082
echo.MOV AL,[SI]
echo.MOV CX,1
echo.MOV AH,09
echo.INT 10
echo.MOV AH,3
echo.INT 10
echo.INC DL
echo.MOV AH,2
echo.INT 10
echo.INC SI
echo.MOV AL,[SI]
echo.CMP AL,0D
echo.JNZ 109
echo.RET
echo.
echo.r cx
echo.22
echo.w
echo.q
)|debug>NUL
"%com%" %txt%
rem del "%com%" /q
if "%cr%"=="Y" echo.
EXIT /b
|
|
Description: |
call:echoLine uniqueStr formatter offset |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
|
:echoLine uniqueStr formatter offset -- outputs a formatted string, substitutes file name and line number
:: -- uniqueStr [in] - a unique string to identify the line
:: -- formatter [in,opt] - a string using __FILE__ and/or __LINE__ to be substituted and echoed
:: -- offset [in,opt] - offset to be added to the line number
:$reference https://www.dostips.com/forum/viewtopic.php?t=369
:$created 20080512 :$changed 20080512 :$categories Echo,Debug
:$source https://www.dostips.com
Setlocal Disabledelayedexpansion
Set "Fmt=%~2"
If Not Defined Fmt Set "Fmt=__FILE__(__LINE__): ERROR"
For /F "Delims=:" %%A In ('"Findstr /N "%~1" "%~f0""') Do Set /A "lineNr=%%A+%~30/10"
Call Set "Fmt=%%Fmt:__LINE__=%lineNr%%%"
Call Echo.%%Fmt:__FILE__=%~nx0%%
EXIT /b
|
|
Description: |
call:extractFileSection StartMark EndMark FileName |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
|
:extractFileSection StartMark EndMark FileName -- extracts a section of file that is defined by a start and end mark
:: -- [IN] StartMark - start mark, use '...:S' mark to allow variable substitution
:: -- [IN,OPT] EndMark - optional end mark, default is first empty line
:: -- [IN,OPT] FileName - optional source file, default is THIS file
:$created 20080219 :$changed 20100205 :$categories FileOperation
:$source https://www.dostips.com
SETLOCAL Disabledelayedexpansion
set "bmk=%~1"
set "emk=%~2"
set "src=%~3"
set "bExtr="
set "bSubs="
if "%src%"=="" set src=%~f0& rem if no source file then assume THIS file
for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do (
if /i "%%B"=="%emk%" set "bExtr="&set "bSubs="
if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B)
if /i "%%B"=="%bmk%" set "bExtr=Y"
if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y"
)
EXIT /b
|
|
Description: |
call:ExtractFunction func |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
|
:ExtractFunction func -- extracts a function by label
:: -- func [in] - name of the function to be extracted
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set func=%~f0
set /a b=2000000000
set /a e=2000000000
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%~1 " "%func%""') do set /a b=%%a
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"EXIT /b" "%func%""') do (
if /i %b% LSS %%a if /i %%a LSS !e! set /a e=%%a
)
SETLOCAL DISABLEDELAYEDEXPANSION& rem --disabling preserves excamation marks in %%b
for /f "tokens=1,* delims=[]" %%a in ('"find /n /v "" "%func%""') do (
if /i %b% LEQ %%a if /i %%a LEQ %e% echo.%%b
)
EXIT /b
|
|
Description: |
call:ExtractSimple StartMark EndMark |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:ExtractSimple StartMark EndMark -- extracts a marked section from batch file, sustitutes variables during extraction
:: -- [IN] StartMark - start mark
:: -- [IN,OPT] EndMark - optional end mark, default is first empty line
:$created 20091113 :$changed 20091113 :$categories FileOperation
:$source https://www.dostips.com
SETLOCAL
set "cpy="
for /f "tokens=1* delims=]" %%A in ('find /v /n "" "%~f0"') do (
if "%%B"=="%~1" set "cpy=Y"
if "%%B"=="%~2" set "cpy="
if defined cpy call echo.%%B
)
EXIT /b
|
|
Description: |
call:false |
Script: |
1. 2. 3. 4. 5.
|
:false -- returns failure
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
set=2>NUL
EXIT /b
|
|
Description: |
call:Format fmt str1 str2 ... |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
|
:Format fmt str1 str2 ... -- outputs columns of strings right or left aligned
:: -- fmt [in] - format string specifying column width and alignment, i.e. "[-10][10][10]"
:$created 20060101 :$changed 20091130 :$categories Echo
:$source https://www.dostips.com
SETLOCAL
set "fmt=%~1"
set "line="
set "spac= "
set "i=1"
for /f "tokens=1,2 delims=[" %%a in ('"echo..%fmt:]=&echo..%"') do (
set /a i+=1
call call set "subst=%%%%~%%i%%%spac%%%%%~%%i%%"
if %%b0 GEQ 0 (call set "subst=%%subst:~0,%%b%%"
) ELSE (call set "subst=%%subst:~%%b%%")
call set "const=%%a"
call set "line=%%line%%%%const:~1%%%%subst%%"
)
echo.%line%
EXIT /b
|
|
Description: |
call:fprop filename prop ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:fprop filename prop ret -- returns a file property
:: -- filename [in] - file name
:: -- prop [in] - property, i.e.: d p n x a f s t z
:: -- ret [out] - return value
:$created 20060101 :$changed 20080219 :$categories FileInfo
:$source https://www.dostips.com
SETLOCAL
set ret=
for %%a in ("%~1") do set ret=%%~%~2a
( ENDLOCAL & REM RETURN VALUES
IF "%~3" NEQ "" (SET %~3=%ret%) ELSE echo.%ret%
)
EXIT /b
|
|
Description: |
call:ftime JD filename attr |
Dependencies: |
:jdate |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
|
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
|
|
Description: |
call:getColorCode col ret |
Script: |
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.
|
:getColorCode col ret -- converts color text to color code
:: -- col [in] - color text BackgroundForeground, i.e.: BlueLYellow for 1E
:: -- ret [out] - return variable to return color code in
:$created 20060101 :$changed 20080219 :$categories Color,Echo
:$source https://www.dostips.com
SETLOCAL
set col=%~1
set col=%col:Gray=8%
set col=%col:LBlue=9%
set col=%col:LGreen=A%
set col=%col:LAqua=B%
set col=%col:LRed=C%
set col=%col:LPurple=D%
set col=%col:LYellow=E%
set col=%col:LWhite=F%
set col=%col:Black=0%
set col=%col:Blue=1%
set col=%col:Green=2%
set col=%col:Aqua=3%
set col=%col:Red=4%
set col=%col:Purple=5%
set col=%col:Yellow=6%
set col=%col:White=7%
ENDLOCAL & IF "%~2" NEQ "" (SET %~2=%col%) ELSE (echo.%col%)
EXIT /b
|
|
Description: |
call:getDriveInfo var |
Script: |
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.
|
:getDriveInfo var -- returns array of fsutil drive information
:: -- var [out] - return variable, as array
:$created 20060101 :$changed 20091115 :$categories DriveInfo,Array
:$source https://www.dostips.com
if "%~1" NEQ "" for /f "delims==" %%A in ('set %~1[ 2^>NUL') do @set "%%A="
SETLOCAL ENABLEDELAYEDEXPANSION
set "data="
set /a n=-1
for /f "tokens=1 delims=" %%A in ('fsutil fsinfo drives^|find "\"') do (
set "dr=%%~A"
set "dr=!dr:~-3,-1!"
set /a n+=1
set "data=!data!&set %~1[!n!].Drive=!dr!"
for /f "tokens=1,* delims=- " %%a in ('"fsutil fsinfo drivetype !dr!"') do set "data=!data!&set %~1[!n!].DriveType=%%b"
for /f "tokens=1,* delims=:" %%a in ('"fsutil volume diskfree !dr!"') do (
set "v= %%a"
set "v=!v: =!"
set "d= %%b"
set "d=!d:~2!"
set "data=!data!&set %~1[!n!].!v!=!d!"
)
)
set "data=rem.%data:)=^)%"
( ENDLOCAL & REM RETURN VALUES
%data%
SET "%~1[#]=%n%"
)
EXIT /b
|
|
Description: |
call:getDrives var |
Dependencies: |
:getDriveInfo |
Script: |
1. 2. 3. 4. 5.
|
:getDrives var -- This function has been replaced by :getDriveInfo
:$created 20060101 :$changed 20091115
:$source https://www.dostips.com
call:getDriveInfo %~1
EXIT /b
|
|
Description: |
call:getFunctions ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:getFunctions ret -- returns a comma separated list of all functions
:: -- ret [out] - reference to return variable
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
set ret=
for /f %%a in ('"findstr "^^:[a-z].*--" "%~f0" "') do call set ret=%%ret%%%%a,
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%ret%) ELSE ECHO.%ret%
)
EXIT /b
|
|
Description: |
call:getHostName ip ret |
Dependencies: |
:getIP |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:getHostName ip ret -- resolves IP address to computer name
:: -- ip [in,opt] - ip, default is THIS computer's IP
:: -- ret [out,opt] - computer name
:$created 20060101 :$changed 20080219 :$categories Network
:$source https://www.dostips.com
SETLOCAL
set ip=%~1
if "%ip%"=="" call:getIP "" ip
set name=
for /f "tokens=2" %%a in ('"ping /a /n 1 %ip%|find "Pinging" 2>NUL"') do set name=%%a
ENDLOCAL & IF "%~2" NEQ "" (SET %~2=%name%) ELSE (echo.%name%)
EXIT /b
|
|
Description: |
call:getIP host ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:getIP host ret -- return THIS computers IP address
:: -- host [in,opt] - host name, default is THIS computer
:: -- ret [out,opt] - IP
:$created 20060101 :$changed 20080219 :$categories Network
:$source https://www.dostips.com
SETLOCAL
set host=%~1
set ip=
if "%host%"=="" ( for /f "tokens=2,* delims=:. " %%a in ('"ipconfig|find "IP Address""') do set ip=%%b
) ELSE ( for /f "tokens=2 delims=[]" %%a in ('"ping /a /n 1 %host%|find "Pinging" 2>NUL"') do set ip=%%a)
ENDLOCAL & IF "%~2" NEQ "" (SET %~2=%ip%) ELSE (echo.%ip%)
EXIT /b
|
|
Description: |
call:getIPConfig arr |
Script: |
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.
|
:getIPConfig arr -- return IPCONFIG /ALL data in array variable
:: -- arr [out] - target array variable for IPCONFIG data
:$created 20091111 :$changed 20091111 :$categories Network,Array
:$source https://www.dostips.com
if "%~1" NEQ "" for /f "delims==" %%A in ('set %~1[ 2^>NUL') do @set "%%A="
SETLOCAL ENABLEDELAYEDEXPANSION
set "data="
set /a n=0
for /f "tokens=1,* delims=:" %%A in ('ipconfig /all^|find ":"') do (
set "v=%%~A "
if "!v:~0,8!" NEQ " " (
rem it's a new section
set /a n+=1
set "data=!data!&set %~1[!n!].DisplayName=%%A"
) ELSE (
set "v=!v:~8!"
set "v=!v:.=!"
set "v=!v: =!"
set "x=%%~B"
set "data=!data!&set %~1[!n!].!v!=!x:~1!"
)
)
set "data=rem.%data:)=^)%"
( ENDLOCAL & REM RETURN VALUES
%data%
SET "%~1[#]=%n%"
)
EXIT /b
|
|
Description: |
call:getNextInList current list |
Script: |
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.
|
:getNextInList current list -- return next value in list
:: -- current [in,out] - iterator variable holding the current value to be replaced with the next value
:: -- list [in] - choice list, must start and end with the delimiter
:$created 20091207 :$changed 20091207 :$categories List,Iterate,String
:$source https://www.dostips.com
Setlocal Enabledelayedexpansion
set "lst=%~2"
set "lst=%lst:~-1%%lst%%lst:~1%"
call set "v=%%lst:*%lst:~-1%!%~1!%lst:~-1%=%%"
set "v=!v:%lst:~-1%=&rem.%!"
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%v%) ELSE (echo.%new%)
)
GOTO:EOF
:count needle haystack ret -- counts the number of occurrences of needle in haystack
:: -- needle [in] - string to find
:: -- haystack [in] - string to search in
:: -- ret [out] - valref for return value
:$created 20060101 :$changed 20090531 :$categories String
:$source https://www.dostips.com
SETLOCAL
set "haystack=%~2"
set /a ret=0
FOR /f %%a in ('"(echo.%%haystack:%~1=&echo.%%)|find /c /v """') DO set /a ret=%%a-1
( ENDLOCAL & REM RETURN VALUES
IF "%~3" NEQ "" (SET %~3=%ret%) ELSE ECHO.%ret%
)
EXIT /b
|
|
Description: |
call:getRandomColor ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:getRandomColor ret -- returns a random color
:: -- ret [out,opt] - return variable to return color code in
:$created 20060101 :$changed 20080219 :$categories Color
:$source https://www.dostips.com
SETLOCAL
set HEX=0123456789ABCDEF
set /a r1=%random% %% 16
set /a r2=%random% %% 16
call set rndcolor=%%HEX:~%r1%,1%%%%HEX:~%r2%,1%%
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%rndcolor%) ELSE (echo.%rndcolor%)
EXIT /b
|
|
Description: |
call:GetRegKeys Key Data Type |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:GetRegKeys Key Data Type -- returns all registry syb keys of a given registry key
:: -- Key [in] - registry key
:: -- Arr [out] - array of sub keys
:$created 20091123 :$changed 20091123 :$categories Registry, Array
:$source https://www.dostips.com
if "%~2" NEQ "" for /f "delims==" %%A in ('set %~2[ 2^>NUL') do set "%%A="
set /a %~2[#]=-1
for /f "tokens=*" %%A in ('reg query "%~1"^|findstr /b /c:"HKEY_"') do (
set /a %~2[#]+=1
call set %~2[%%%~2[#]%%]=%%A
)
EXIT /b
|
|
Description: |
call:GetRegValue Key Value Data Type |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.
|
:GetRegValue Key Value Data Type -- returns a registry value
:: -- Key [in] - registry key
:: -- Value [in] - registry value
:: -- Data [out] - return variable for Data
:: -- Type [out] - return variable for Type, i.e.: REG_SZ, REG_MULTI_SZ, REG_DWORD_BIG_ENDIAN, REG_DWORD, REG_BINARY, REG_DWORD_LITTLE_ENDIAN, REG_NONE, REG_EXPAND_SZ
:$created 20060101 :$changed 20080219 :$categories Registry
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set Key=%~1
set Val=%~2
if "%Val%"=="" (set v=/ve) ELSE set v=/v "%Val%"
set Data=
set Type=
for /f "tokens=2,* delims= " %%a in ('reg query "%Key%" %v%^|findstr /b "....%match%"') do (
set Type=%%a
set Data=%%b
)
( ENDLOCAL & REM RETURN VALUES
IF "%~3" NEQ "" (SET %~3=%Data%) ELSE echo.%Data%
IF "%~4" NEQ "" (SET %~4=%Type%)
)
EXIT /b
|
|
Description: |
call:GetRegValues Key Data Type |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:GetRegValues Key Data Type -- returns all registry values of a given registry key
:: -- Key [in] - registry key
:: -- Stc [out] - struct of registry values
:$created 20091123 :$changed 20091123 :$categories Registry, Array
:$source https://www.dostips.com
if "%~2" NEQ "" for /f "delims==" %%A in ('"set %~2. 2>NUL"') do set "%%A="
for /f "tokens=1,2,* skip=4 delims= " %%A in ('reg query "%~1"') do (
for /f "tokens=*" %%X in ("%%A") do ( rem this removes the leading tab
set "%~2.%%X=%%C"
)
)
EXIT /b
|
|
Description: |
call:getServices var |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:getServices var -- returns array of service information
:: -- var [out] - return variable, as array
:$created 20060101 :$changed 20091113 :$categories ServiceControl,Array
:$source https://www.dostips.com
set /a %~1[#]=-1
for /f "tokens=*" %%x in ('"sc queryex state= all type= service"') do (
for /f "tokens=1,* delims=: " %%a in ("%%x") do (
if "%%a"=="SERVICE_NAME" set /a %~1[#]+=1
call set %~1[%%%~1[#]%%].%%a=%%b
)
)
EXIT /b
|
|
Description: |
call:getVarsByAttr attr ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:getVarsByAttr attr ret -- returns a comma separated list of variables based on an attribute name
:: -- attr [in] - attribute name
:: -- ret [out] - reference to return variable
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
set varattr=%~1
set value=
FOR /F "usebackq tokens=1,2 delims=.=" %%a IN (`set`) DO if "%%a"=="%varattr%" (set value=!value!%%b,)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET %~2=%value%
)
EXIT /b
|
|
Description: |
call:htmlhelp |
Dependencies: |
:ExtractFunction |
Script: |
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.
|
:htmlhelp -- dumps html help to console
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
rem echo.^<html^>^<body^>
echo.^<center^>
echo.^<table border=0^>
for /f "tokens=1,* delims=-" %%b in ('"findstr "^^:[^^:].*--" "%~f0" "') do (
for /f "tokens=1,* delims= " %%m in ("%%b") do (
set func=%%m
set args=%%n
)
set sign=%%b
set desc=%%c
echo.^<tr^>^<td^>^<a href='#_!func:~1!'^>!func!^</a^>^</td^>^<td^> !desc!^</td^>^</tr^>
)
echo.^</table^>
echo.^</center^>
for /f "tokens=*" %%a in ('"findstr "^^:.*-- ^^:\$" "%~f0" "') do (
set line=%%a
if "!line:~1,1!"==":" ( rem process function arguments section
for /f "tokens=1,2,* delims=-[] " %%b in ("!line:*--=!") do (
set pname=%%b
set ptype=%%c
set pdesc=%%d
)
echo.^<tr^>
echo.^<td^>!pname!^</td^>
echo.^<td^>!ptype!^</td^>
echo.^<td^>!pdesc!^</td^>
echo.^</tr^>
) ELSE if "!line:~1,1!"=="$" ( rem process the end section
if "!func!" NEQ "" (
set depe=
for /f "tokens=*" %%F in ('%~f0 :ExtractFunction !func!^|findstr /i "call\:[a-z]"') do (
set line="%%F"
set line="!line:*call:=!
for /f "delims=&|>< " %%G in (!line!) do set depe=!depe!%%G &set line=%%G
rem echo.!func! - !line!>&2
)
rem echo.!func! >&2
rem echo.^<pre^>&call:ExtractFunction !func!&echo.^</pre^>
echo.^</table^>
echo.^<p^>Dependencies:
for %%F in (!depe!) do (echo.^<a href='#_%%F'^>:%%F^</a^> )
if not defined depe echo.none
echo.^</p^>
set func=&rem this makes sure we process the END only once
)
) ELSE ( rem process function title section
for /f "tokens=1,* delims=-" %%b in ("!line!") do (
for /f "tokens=1,* delims= " %%m in ("%%b") do (
set func=%%m
set args=%%n
)
TITLE processing !func!
set sign=%%b
set desc=%%c
)
echo.^<br/^>
echo.^<H2^>^<a name='_!func:~1!'^>!func! -- !desc!^</a^>^</H2^>
echo.^<span style='float:right;'^> ^<a href='#!func:~1!'^>View Script^</a^>^ ^ ^</span^>
echo.^<p^>Call ^<b^>^<a href='#!func:~1!'^>!func!^</a^>^</b^> ^<i^>!args!^</i^>^</p^>
echo.^<table border=1^>^<tr^>^<td^>Parameter^</td^>^<td^>Type^</td^>^<td^>Desc^</td^>^</tr^>
)
)
TITLE processing done
rem echo.^</body^>^</html^>
EXIT /b
|
|
Description: |
call:Init |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:Init -- initializes the environment for this command library
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
set ".=%~f0" & rem set full name, i.e. type %.%
set ":=call "%.%" " & rem set cl call prefix, i.e. %:%:func
set "?=&&echo.TRUE||echo.FALSE" & rem set true/false check, i.e. %:%:true %?%
set "*=%:%:set " & rem set array setter for console output, i.e. %*% d=dir /b
if not exist "%temp%" set "temp=%tmp%"
if not exist "%temp%" set "temp=c:\temp"
if not exist "%temp%" md "%temp%"
EXIT /b
|
|
Description: |
call:initProgress max format |
Dependencies: |
:doProgress |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:initProgress max format -- initializes an internal progress counter and display the progress in percent
:: -- max [in] - progress counter maximum, equal to 100 percent
:: -- format [in,opt] - title string formatter, default is '[P] completed.'
:$created 20060101 :$changed 20080327 :$categories Progress
:$source https://www.dostips.com
set /a "ProgressCnt=-1"
set /a "ProgressMax=%~1"
set "ProgressFormat=%~2"
if not defined ProgressFormat set "ProgressFormat=[PPPP]"
set "ProgressFormat=%ProgressFormat:[PPPP]=[P] completed.%"
call:doProgress
EXIT /b
|
|
Description: |
call:initVarsByAttr |
Dependencies: |
:getVarsByAttr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9.
|
:initVarsByAttr -- restores the values of the persistent variables to defaults
:$created 20060101 :$changed 20080219 :$categories Persistency
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call:getVarsByAttr persist vars
( ENDLOCAL
for %%a in (%vars%) do SET %%a=!persist.%%a!& rem AA=persist.AA
)
EXIT /b
|
|
Description: |
call:IsFileOpen filename |
Script: |
1. 2. 3. 4. 5. 6.
|
:IsFileOpen filename -- succeeds if file in use, fails otherwise
:: -- filename [in] - name of file to be tested
:$created 20060101 :$changed 20080219 :$categories FileOperation
:$source https://www.dostips.com
echo.N|copy /-y NUL "%~1">NUL
EXIT /b
|
|
Description: |
call:IsRegKey Key |
Script: |
1. 2. 3. 4. 5. 6.
|
:IsRegKey Key -- succeeds if Key exists
:: -- Key [in] - registry key
:$created 20060101 :$changed 20080219 :$categories Registry
:$source https://www.dostips.com
reg query "%~1">NUL 2>NUL
EXIT /b
|
|
Description: |
call:IsRegValue Key Value |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9.
|
:IsRegValue Key Value -- succeeds if Key and Value exists
:: -- Key [in] - registry key
:: -- Value [in] - registry value, return default value if omitted
:$created 20060101 :$changed 20080219 :$categories Registry
:$source https://www.dostips.com
SETLOCAL
if "%~2"=="" (set v=/ve) ELSE set v=/v "%~2"
reg query "%~1" %v%>NUL 2>NUL
EXIT /b
|
|
Description: |
call:IsServiceRunning service |
Script: |
1. 2. 3. 4. 5. 6. 7.
|
:IsServiceRunning service -- returns success if service is running, otherwise failure
:: -- service [in] - name of the service to check
:$created 20060101 :$changed 20080219 :$categories ServiceControl
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
sc query "%~1"|findstr "STATE.*:.*4.*RUNNING">NUL
EXIT /b
|
|
Description: |
call:jdate JD DateStr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference https://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20090328 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
|
|
Description: |
call:jdate2date JD YYYY MM DD |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.
|
:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
:: -- JD [in] - julian days
:: -- YYYY [out] - gregorian year, i.e. 2006
:: -- MM [out] - gregorian month, i.e. 12 for december
:: -- DD [out] - gregorian day, i.e. 31
:$reference https://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b
|
|
Description: |
call:kBytesFree drive ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:kBytesFree drive ret -- returns the free space of a drive in kilobytes
:: -- drive [in] - drive letter, default is c:
:: -- ret [out,opt] - varref returns number of bytes free
:$created 20060101 :$changed 20080219 :$categories FileInfo
:$source https://www.dostips.com
SETLOCAL
SET "drive=%~1"
IF "%drive%"=="" SET "drive=C:"
FOR /f "tokens=3 delims= " %%a in ('"dir %drive%\|find "bytes free""') do set free=%%a
SET "free=%free:,=%" &rem eleminate commas in number
SET "free=%free:.=%" &rem eleminate dots in number
SET "free=%free:~0,-3%" &rem divide by 1000 by cutting off the last 3 digits
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET "%~2=%free%") ELSE echo.%free%
)
EXIT /b
|
|
Description: |
call:l2a list arr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:l2a list arr -- converts a list to an array
:: -- list [in] - list name
:: -- arr [in] - array name
:$created 20060101 :$changed 20080219 :$categories Array,List
:$source https://www.dostips.com
call set l=,%%%~1%%,
set /a i=0
for /f "delims=" %%a in ('"echo.%l:,=&echo.%"') do (
call set %~2[%%i%%]=%%a
set /a i+=1
)
set %~2=%i%
EXIT /b
|
|
Description: |
call:loadPersistentVars filename |
Dependencies: |
:initVarsByAttr |
Script: |
1. 2. 3. 4. 5. 6. 7.
|
:loadPersistentVars filename -- loads the values of the persistent variables
:: -- filename [in] - batch file name to restore from
:$created 20060101 :$changed 20080219 :$categories Persistency
:$source https://www.dostips.com
call:initVarsByAttr
if exist "%FilePersist%" call "%FilePersist%"
EXIT /b
|
|
Description: |
call:loadRegVars key |
Dependencies: |
:getVarsByAttr, :GetRegValue |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:loadRegVars key -- loads the values of the persistent variables
:: -- key [in] - registry key to restore from
:$created 20060101 :$changed 20080219 :$categories Registry,Persistency
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call:getVarsByAttr reg vars
( ENDLOCAL
for %%a in (%vars%) do SET %%a=!reg.%%a!& rem AA=reg.AA
for %%a in (%vars%) do call:GetRegValue "%~1" "%%a" %%a
)
EXIT /b
|
|
Description: |
call:lookup key map ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
|
:lookup key map ret -- returns the value associated with a key in a map of key-value pairs
:: -- key [in] - key to lookup
:: -- map [in] - map with key-value pairs, i.e. key1-val1;key2-val2;key3-val3
:: -- ret [out] - valref for return value
:$created 20060101 :$changed 20080219 :$categories Lookup
:$source https://www.dostips.com
SETLOCAL
SET map=%~2
CALL SET ret=%%map:*%~1-=%%
SET ret=%ret:;=&:%
IF "%ret%" NEQ "%ret:-=%" set ret=
( ENDLOCAL & REM RETURN VALUES
IF "%~3" NEQ "" (SET %~3=%ret%) ELSE ECHO.%ret%
)
EXIT /b
|
|
Description: |
call:lTrim string char |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.
|
:lTrim string char -- strips white spaces (or other characters) from the beginning of a string
:: -- string [in,out] - string variable to be trimmed
:: -- char [in,opt] - character to be trimmed, default is space
:$created 20060101 :$changed 20080227 :$categories StringManipulation
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call set "string=%%%~1%%"
set "charlist=%~2"
if not defined charlist set "charlist= "
for /f "tokens=* delims=%charlist%" %%a in ("%string%") do set "string=%%a"
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" SET "%~1=%string%"
)
EXIT /b
|
|
Description: |
call:MakeAbsolute file base |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
|
:MakeAbsolute file base -- makes a file name absolute considering a base path
:: -- file [in,out] - variable with file name to be converted, or file name itself for result in stdout
:: -- base [in,opt] - base path, leave blank for current directory
:$created 20060101 :$changed 20080219 :$categories Path
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set "src=%~1"
if defined %1 set "src=!%~1!"
set "bas=%~2"
if not defined bas set "bas=%cd%"
for /f "tokens=*" %%a in ("%bas%.\%src%") do set "src=%%~fa"
( ENDLOCAL & REM RETURN VALUES
IF defined %1 (SET %~1=%src%) ELSE ECHO.%src%
)
EXIT /b
|
|
Description: |
call:MakeRelative file base |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
|
:MakeRelative file base -- makes a file name relative to a base path
:: -- file [in,out] - variable with file name to be converted, or file name itself for result in stdout
:: -- base [in,opt] - base path, leave blank for current directory
:$created 20060101 :$changed 20080219 :$categories Path
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set src=%~1
if defined %1 set src=!%~1!
set bas=%~2
if not defined bas set bas=%cd%
for /f "tokens=*" %%a in ("%src%") do set src=%%~fa
for /f "tokens=*" %%a in ("%bas%") do set bas=%%~fa
set mat=&rem variable to store matching part of the name
set upp=&rem variable to reference a parent
for /f "tokens=*" %%a in ('echo.%bas:\=^&echo.%') do (
set sub=!sub!%%a\
call set tmp=%%src:!sub!=%%
if "!tmp!" NEQ "!src!" (set mat=!sub!)ELSE (set upp=!upp!..\)
)
set src=%upp%!src:%mat%=!
( ENDLOCAL & REM RETURN VALUES
IF defined %1 (SET %~1=%src%) ELSE ECHO.%src%
)
EXIT /b
|
|
Description: |
call:NetworkDeviceGuid2Name guid name |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:NetworkDeviceGuid2Name guid name -- gets a Network Device Name from its coresponding GUID
:: -- guid [in] - Network Device GUID
:: -- name [out,opt] - valref to return Name in
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
set guid=%~1
set key=HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}
set ret=
for /f "tokens=*" %%k in ('"reg query "%key%" /s|find /I "}\Connection" "') do (
for /f "tokens=2,*" %%m in ('"reg query "%%k" /v "Name"|findstr /b "....Name" "') do (
for /f "tokens=7 delims=\" %%g in ("%%k") do (
if "%%g"=="%guid%" set ret=%%n
)))
ENDLOCAL&if "%~2" NEQ "" (SET %~2=%ret%) ELSE ECHO.%ret%
EXIT /b
|
|
Description: |
call:NetworkDeviceName2Guid name guid |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:NetworkDeviceName2Guid name guid -- gets a Network Device GUID from its corresponding Name
:: -- name [in] - Network Device Name
:: -- guid [out,opt] - valref to return GUID in
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
set name=%~1
set key=HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}
set ret=
for /f "tokens=*" %%k in ('"reg query "%key%" /s|find /I "}\Connection" "') do (
for /f "tokens=2,*" %%m in ('"reg query "%%k" /v "Name"|findstr /b "....Name" "') do (
for /f "tokens=7 delims=\" %%g in ("%%k") do (
if "%%n"=="%name%" set ret=%%g
)))
ENDLOCAL&if "%~2" NEQ "" (SET %~2=%ret%) ELSE ECHO.%ret%
EXIT /b
|
|
Description: |
call:parse fmt str |
Script: |
1. 2. 3. 4. 5. 6. 7. 8.
|
:parse fmt str -- parses a string matching a formatter
:: -- fmt [in] - formatter containing return variable names and delimitters
:: -- str [in] - string to be parsed
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
SETLOCAL
echo...under construction
EXIT /b
|
|
Description: |
call:pwd var title color |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.
|
:pwd var title color -- shows a password dialog box
:: -- var [in] - return variable
:: -- title [in,opt] - dialog title, default is "Password:"
:: -- color [in,opt] - color default is AB (Light Green on Light Aqua)
:$created 20060101 :$changed 20080226 :$categories Input,Password
:$source https://www.dostips.com
SETLOCAL
set "tit=%~2"
set "col=%~3"
set "pwd="
if not defined col set "col=EF"
if not defined tit set "tit=Password:"
set "f=%temp%\%~nx0.tmp~0.tmp"
start "%tit%" /wait cmd /c "mode con cols=24 lines=1&color %col%&set /p "in="&call echo.%%in%%>"%f%""
for /f "usebackq tokens=*" %%a in ("%f%") do set "pwd=%%a"
del /q "%f%"
ENDLOCAL&if "%~1" NEQ "" (SET %~1=%pwd%) ELSE ECHO.%txt%
EXIT /b
|
|
Description: |
call:regedit key |
Script: |
1. 2. 3. 4. 5. 6. 7. 8.
|
:regedit key -- opens regedit at predefined location
:: -- [IN] key - registry key to be shown after opening regedit
:$created 20080314 :$changed 20080314 :$categories Regedit,Registry
:$source https://www.dostips.com
SETLOCAL
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /f /d "%~1">NUL
start /b regedit
EXIT /b
|
|
Description: |
call:removeArr arr |
Script: |
1. 2. 3. 4. 5. 6.
|
:removeArr arr -- removes an array
:: -- arr [in] - array name
:$created 20060101 :$changed 20080219 :$categories Array
:$source https://www.dostips.com
for /f "delims==" %%a in ('"set %~1[ 2>NUL"') do set "%%a="
EXIT /b
|
|
Description: |
call:rTrim string char max |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
|
:rTrim string char max -- strips white spaces (or other characters) from the end of a string
:: -- string [in,out] - string variable to be trimmed
:: -- char [in,opt] - character to be trimmed, default is space
:: -- max [in,opt] - maximum number of characters to be trimmed from the end, default is 32
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call set string=%%%~1%%
set char=%~2
set max=%~3
if "%char%"=="" set char= &rem one space
if "%max%"=="" set max=32
for /l %%a in (1,1,%max%) do if "!string:~-1!"=="%char%" set string=!string:~0,-1!
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" SET %~1=%string%
)
EXIT /b
|
|
Description: |
call:savePersistentVars filename |
Dependencies: |
:getVarsByAttr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9.
|
:savePersistentVars filename -- save values of persistent variables into a file
:: -- filename [in] - file name used for storage
:$created 20060101 :$changed 20080219 :$categories Persistency
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
type nul>"%~1"
call:getVarsByAttr persist vars
for %%a in (%vars%) do (echo.SET %%a=!%%a!>>"%~1")
EXIT /b
|
|
Description: |
call:saveRegVars key |
Dependencies: |
:getVarsByAttr, :SetRegValue |
Script: |
1. 2. 3. 4. 5. 6. 7. 8.
|
:saveRegVars key -- save values of persistent variables into a file
:: -- key [in] - registry key used for storage
:$created 20060101 :$changed 20080219 :$categories Registry,Persistency
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
call:getVarsByAttr reg vars
for %%a in (%vars%) do call:SetRegValue "%~1" "%%a" "!%%a!"
EXIT /b
|
|
Description: |
call:set var=command |
Dependencies: |
:removeArr |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.
|
:set var=command -- sets var[] to output of command / under construction
:: -- var [out] varref of target variable
:: -- command [in] command to be parsed, i.e. ipconfig /all
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
for /f "tokens=1,* delims==" %%x in ("%*") do (
call:removeArr "%%x"
if "%%y" NEQ "" (
for /f "tokens=1,2 delims=]" %%a in ('"%%y|find /n /v """') do (
set "%%x%%a]=%%b"
set "%%x[#]=%%a"
)&&call set "%%x[#]=%%%%x[#]:~1%%"
)
call set "%%x=%%%%x[1]%%"
)
EXIT /b
|
|
Description: |
call:SetRegValue Key Value Data Type |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
|
:SetRegValue Key Value Data Type -- sets a registry value
:: -- Key [in] - registry key
:: -- Value [in] - registry value
:: -- Data [in] - (optional) data
:: -- Type [in] - (optional) data type, i.e.: REG_SZ, REG_MULTI_SZ, REG_DWORD_BIG_ENDIAN, REG_DWORD, REG_BINARY, REG_DWORD_LITTLE_ENDIAN, REG_NONE, REG_EXPAND_SZ
:$created 20060101 :$changed 20080219 :$categories Registry
:$source https://www.dostips.com
SETLOCAL
set Key=%~1
set Val=%~2
set Data=%~3
set Type=%~4
if "%Val%" NEQ "" set Val=/v "%Val%"
if "%Data%" NEQ "" set Data=/d "%Data%"
if "%Type%" NEQ "" set Type=/t %Type%
reg add "%Key%" %Val% %Type% %Data% /f>NUL
EXIT /b
|
|
Description: |
call:sleep seconds |
Script: |
1. 2. 3. 4. 5. 6.
|
:sleep seconds -- waits some seconds before returning
:: -- seconds [in] - number of seconds to wait
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
FOR /l %%a in (%~1,-1,1) do (ping -n 2 -w 1 127.0.0.1>NUL)
EXIT /b
|
|
Description: |
call:StartsWith text string |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:StartsWith text string -- Tests if a text starts with a given string
:: -- [IN] text - text to be searched
:: -- [IN] string - string to be tested for
:$created 20080320 :$changed 20080320 :$categories StringOperation,Condition
:$source https://www.dostips.com
SETLOCAL
set "txt=%~1"
set "str=%~2"
if defined str call set "s=%str%%%txt:*%str%=%%"
if /i "%txt%" NEQ "%s%" set=2>NUL
EXIT /b
|
|
Description: |
call:strLen string len |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.
|
:strLen string len -- returns the length of a string
:: -- string [in] - variable name containing the string being measured for length
:: -- len [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source https://www.dostips.com
( SETLOCAL ENABLEDELAYEDEXPANSION
set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12,-1,0) do (
set /a "len|=1<<%%A"
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b
|
|
Description: |
call:substitute OldStr NewStr File |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.
|
:substitute OldStr NewStr File -- substitutes a string in a text file
:: -- OldStr [in] - string to be replaced
:: -- NewStr [in] - string to replace with
:: -- File [in] - file to be parsed
:$created 20060101 :$changed 20101122 :$categories FileManipulation
:$source https://www.dostips.com
SETLOCAL DISABLEDELAYEDEXPANSION
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
EXIT /b
|
|
Description: |
call:ToANSI src dst |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:ToANSI src dst -- converts a file to ANSI
:: -- src [in] - source file name to be converted
:: -- trg [in,opt] - target file name, leave blank for in-place conversion
:$created 20060101 :$changed 20080219 :$categories Encoding
:$source https://www.dostips.com
SETLOCAL
set src=%~1
set trg=%~2
set tmp=%temp%.\%~nx1
if "%trg%"=="" set trg=%src%
cmd /A /c type "%src%">"%tmp%"
move /y "%tmp%" "%trg%"
EXIT /b
|
|
Description: |
call:toCamelCase str |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22.
|
:toCamelCase str -- converts a string to camel case
:: -- str [in,out] - valref of string variable to be converted
:$created 20080219 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
if not defined %~1 EXIT /b
REM make all lower case
for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i"
"J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r"
"S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z"
"Ä=ä" "Ö=ö" "Ü=ü") do (
call set "%~1=%%%~1:%%~a%%"
)
call set "%~1= %%%~1%%"
REM make first character upper case
for %%a in (" a=A" " b=B" " c=C" " d=D" " e=E" " f=F" " g=G" " h=H" " i=I"
" j=J" " k=K" " l=L" " m=M" " n=N" " o=O" " p=P" " q=Q" " r=R"
" s=S" " t=T" " u=U" " v=V" " w=W" " x=X" " y=Y" " z=Z"
" ä=Ä" " ö=Ö" " ü=Ü") do (
call set "%~1=%%%~1:%%~a%%"
)
call set "%~1=%%%~1: =%%"
EXIT /b
|
|
Description: |
call:toDec hex dec |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:toDec hex dec -- convert a hexadecimal number to decimal
:: -- hex [in] - hexadecimal number to convert
:: -- dec [out,opt] - variable to store the converted decimal number in
:$created 20091203 :$changed 20091203 :$categories Arithmetic,Encoding
:$source https://www.dostips.com
SETLOCAL
set /a dec=0x%~1
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET %~2=%dec%) ELSE ECHO.%dec%
)
EXIT /b
|
|
Description: |
call:toHex dec hex |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
|
:toHex dec hex -- convert a decimal number to hexadecimal, i.e. -20 to FFFFFFEC or 26 to 0000001A
:: -- dec [in] - decimal number to convert
:: -- hex [out,opt] - variable to store the converted hexadecimal number in
::Thanks to 'dbenham' dostips forum users who inspired to improve this function
:$created 20091203 :$changed 20110330 :$categories Arithmetic,Encoding
:$source https://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a dec=%~1
set "hex="
set "map=0123456789ABCDEF"
for /L %%N in (1,1,8) do (
set /a "d=dec&15,dec>>=4"
for %%D in (!d!) do set "hex=!map:~%%D,1!!hex!"
)
rem !!!! REMOVE LEADING ZEROS by activating the next line, e.g. will return 1A instead of 0000001A
rem for /f "tokens=* delims=0" %%A in ("%hex%") do set "hex=%%A"&if not defined hex set "hex=0"
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET %~2=%hex%) ELSE ECHO.%hex%
)
EXIT /b
|
|
Description: |
call:toLower str |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:toLower str -- converts uppercase character to lowercase
:: -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
if not defined %~1 EXIT /b
for %%a in ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i"
"J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r"
"S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z" "Ä=ä"
"Ö=ö" "Ü=ü") do (
call set %~1=%%%~1:%%~a%%
)
EXIT /b
|
|
Description: |
call:ToUNICODE src dst |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.
|
:ToUNICODE src dst -- converts a file to UNICODE
:: -- src [in] - source file name to be converted
:: -- trg [in,opt] - target file name, leave blank for in-place conversion
:$created 20060101 :$changed 20080219 :$categories Encoding
:$source https://www.dostips.com
SETLOCAL
set src=%~1
set trg=%~2
set tmp=%temp%.\%~nx1
if "%trg%"=="" set trg=%src%
cmd /U /c type "%src%">"%tmp%"
move /y "%tmp%" "%trg%"
EXIT /b
|
|
Description: |
call:toUpper str |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:toUpper str -- converts lowercase character to uppercase
:: -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
if not defined %~1 EXIT /b
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
"j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
"s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
"ö=Ö" "ü=Ü") do (
call set %~1=%%%~1:%%~a%%
)
EXIT /b
|
|
Description: |
call:Trim string char max |
Dependencies: |
:lTrim, :rTrim |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9.
|
:Trim string char max -- strip white spaces (or other characters) from the beginning and end of a string
:: -- string [in,out] - string variable to be trimmed
:: -- char [in,opt] - character to be trimmed, default is space
:: -- max [in,opt] - maximum number of characters to be trimmed from the end, default is 32
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
call:lTrim "%~1" "%~2"
call:rTrim "%~1" "%~2" "%~3"
EXIT /b
|
|
Description: |
call:trimSpaces varref |
Dependencies: |
:trimSpaces2 |
Script: |
1. 2. 3. 4. 5. 6.
|
:trimSpaces varref -- trims spaces around string variable
:: -- varref [in,out] - variable to be trimmed
:$created 20060101 :$changed 20080223 :$categories StringManipulation
:$source https://www.dostips.com
call call:trimSpaces2 %~1 %%%~1%%
EXIT /b
|
|
Description: |
call:trimSpaces2 retval string |
Script: |
1. 2. 3. 4. 5. 6. 7.
|
:trimSpaces2 retval string -- trims spaces around string and assigns result to variable
:: -- retvar [out] variable name to store the result in
:: -- string [in] string to trim, must not be in quotes
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source https://www.dostips.com
for /f "tokens=1*" %%A in ("%*") do set "%%A=%%B"
EXIT /b
|
|
Description: |
call:true |
Script: |
1. 2. 3. 4. 5.
|
:true -- returns success
:$created 20060101 :$changed 20080219
:$source https://www.dostips.com
rem
EXIT /b
|
|
Description: |
call:Unique ret |
Script: |
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
|
:Unique ret -- returns a unique string based on a date-time-stamp, YYYYMMDDhhmmsscc
:: -- ret [out,opt] - unique string
:$created 20060101 :$changed 20080219 :$categories StringOperation,DateAndTime
:$source https://www.dostips.com
SETLOCAL
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
for /f "tokens=1-4 delims=:. " %%A in ("%time: =0%") do @set UNIQUE=%yy%%mm%%dd%%%A%%B%%C%%D
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
EXIT /b
|
|
Description: |
The function template can be used as starting point for a new function. Complete the template into a useful function like this:
- Rename the function
- Add proper description for the function and its arguments
- Fill in the function body
- Return desired values
|
Script: |
Download: BatchFunctionTmpl.bat
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.
|
:myFunctionName -- function description here
:: -- %~1: argument description here
SETLOCAL
REM.--function body here
set LocalVar1=...
set LocalVar2=...
(ENDLOCAL & REM -- RETURN VALUES
IF "%~1" NEQ "" SET %~1=%LocalVar1%
IF "%~2" NEQ "" SET %~2=%LocalVar2%
)
GOTO:EOF
|
|