summaryrefslogtreecommitdiff
path: root/win32/rm.bat
blob: c41ebfa5ee0177e993ad24b3d942fbe62e5c3530 (plain)
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
@echo off
@setlocal EnableExtensions DisableDelayedExpansion || exit /b -1

set prog=%~n0
set dryrun=
set recursive=
set debug=
set error=0
set parent=

:optloop
if "%1" == "-f" shift
if "%1" == "-n" (shift & set "dryrun=%1" & goto :optloop)
if "%1" == "-r" (shift & set "recursive=%1" & goto :optloop)
if "%1" == "--debug" (shift & set "debug=%1" & set PROMPT=$E[34m+$E[m$S & echo on & goto :optloop)
:begin
if "%1" == "" goto :EOF
  set p=%1
  shift
  set p=%p:/=\%
  call :remove %p%
goto :begin

:remove
setlocal

::- Split %1 by '?' and '*', wildcard characters
for /f "usebackq delims=?* tokens=1*" %%I in ('%1') do (set "par=%%I" & set "sub=%%J")
if "%sub%" == "" goto :remove_plain
if "%sub:\=%" == "%sub%" goto :remove_plain
    ::- Extract the first wildcard
    set "q=%1"
    call set "q=%%q:%par%=%%"
    set q=%q:~0,1%

    ::- `delims` chars at the beginning are removed in `for`
    if "%sub:~0,1%" == "\" (
        set "sub=%sub:~1%"
        set "par=%par%%q%"
    ) else (
        for /f "usebackq delims=\\ tokens=1*" %%I in ('%sub%') do (set "par=%par%%q%%%I" & set "sub=%%J")
    )

    ::- Recursive search
    for /d %%D in (%par%) do (
        call :remove %sub% %2%%D\
    )
goto :remove_end
:remove_plain
    set p=%2%1
    if not exist "%1" goto :remove_end
    if not "%dryrun%" == "" (
        echo Removing %p:\=/%
        goto :remove_end
    )
    ::- Try `rd` first for symlink to a directory; `del` attemps to remove all
    ::- files under the target directory, instead of the symlink itself.
    (rd /q "%p%" || del /q "%p%") 2> nul && goto :remove_end

    if "%recursive%" == "-r" for /D %%I in (%p%) do (
        rd /s /q %%I || call set error=%%ERRORLEVEL%%
    )
:remove_end
endlocal & set "error=%error%" & goto :EOF