cmd.exe library.
@echo off
:: From another file, call functions as follows:
:: call lib.cmd :fnc [param1[, param2[, ... ]]]
call %*
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:get_files
:: Get a string of file paths given a string of file names and a parent
:: directory.
:: %1 Return variable name for file paths.
:: %2 Parent directory path.
:: %3 String of file names.
setlocal enabledelayedexpansion
set dir=%~2
set names=%~3
:: File paths to return.
set files=
:: Build file paths.
for %%g in (%names%) do (
set files=!files! %dir%\%%g
)
:: The first file path, if written, adds a leading space. Remove it.
call :trim_leading_delimiter files "%files%"
( endlocal
set %~1=%files%
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:check_files
:: Report on present and missing files given a list of files.
:: %1 Return variable for missing files.
:: %2 Return variable for present files.
:: %3 Files.
setlocal enabledelayedexpansion
set files=%~3
:: Missing files.
set missing=
:: Present files.
set present=
:: Build lists of missing and present files.
for %%g in (%files%) do (
if exist %%g (
set present=!present! %%g
) else (
set missing=!missing! %%g
)
)
:: The first file names, if written, add a leading space. Remove them.
call :trim_leading_delimiter missing"%missing%"
call :trim_leading_delimiter present"%present%"
( endlocal
set %~1=%missing%
set %~2=%present%
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:trim_leading_delimiter
:: Remove a leading delimiter from strings built with get_files or
:: check_files.
:: %1 Return variable name.
:: %2 The string.
setlocal
set str=%~2
if defined str set str=%str:~1%
( endlocal
set %~1=%str%
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:get_tmp_file_name
:: Attempts to return a unique file name based on system time. For almost any
:: reasonable situation, this should work. Not locale independent.
:: http://blogs.msdn.com/b/myocom/archive/2005/06/03/creating-unique-filenames-in-a-batch-file.aspx?
:: %1 Return variable name.
:: %2 Base name. May be unspecified or zero-length.
setlocal
set base_name=%~2
for /f "delims=/ tokens=1-3" %%a in ("%date:~4%") do (
for /f "delims=:. tokens=1-4" %%m in ("%time: =0%") do (
set name=%%c-%%a-%%b-%%m%%n%%o%%p
)
)
if defined base_name set name=%base_name%-%name%
( endlocal
set %~1=%name%
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:empty_dir
:: Quietly empty a directory
:: %1 Path to the directory to empty. Must be specified. If the current
:: directory is the target, specify '.'.
setlocal
set dir=%~1
if not defined dir (
echo.error lib.cmd :empty_dir: Path to directory to be emptied must be
echo.specified. If current directory is target, specify '.'.
exit /b 1
)
del /f /q "%dir%\*"
for /d %%g in ("%dir%\*") do rmdir /q /s "%%g"
endlocal
goto :eof