-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteFilesLT.bat
146 lines (122 loc) · 3.03 KB
/
deleteFilesLT.bat
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
echo off
setlocal
set my_name=%~n0%~x0
set my_dir="%~dp0"
set "my_dir=%my_dir:~1,-2%"
set /a verbose=0
set /a CHECK_FLAG=1
set dir=
set type=*
set /a min=0
SET /a recursive=0
SET /a flags=0
GOTO :ParseParams
:ParseParams
if [%1]==[/?] goto help
if /i [%1]==[/h] goto help
if /i [%1]==[/help] goto help
IF /i "%~1"=="/c" (
SET /a "flags=%flags%|CHECK_FLAG"
goto reParseParams
)
IF /i "%~1"=="/d" (
SET "dir=%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/m" (
SET /a min=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/r" (
SET /a recursive=1
goto reParseParams
)
IF /i "%~1"=="/t" (
SET "type=%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/v" (
SET verbose=1
goto reParseParams
) else (
echo Skipping unknown option "%~1"
goto reParseParams
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
if %min% EQU 0 goto usage
if ["%dir%"] == [] goto usage
if ["%dir%"] == [""] goto usage
call :isDir "%dir%"
if [%errorlevel%] == [0] (
echo %in% is not a directory!
goto usage
)
set /a "check=%flags%&CHECK_FLAG"
if %verbose% EQU 1 (
echo dir=%dir%
echo min=%min%
echo type = %type%
echo recursive = %recursive%
echo check = %check%
echo.
)
if %check% NEQ 0 echo [i] Running in check mode
if %recursive% == 0 (
if %verbose% EQU 1 echo iterating %dir%
for %%p in ("%dir%\*.%type%") do (
if %verbose% EQU 1 echo %%p %%~zp
If %%~zp LSS %min% (
if %check% NEQ 0 (
echo del "%%p"
) else (
if %verbose% EQU 1 echo deleting %%p
del "%%p"
)
)
)
) else (
for /r "%dir%" %%p in (*.%type%) do (
if %verbose% EQU 1 echo %%p %%~zp
If %%~zp LSS %min% (
if %check% NEQ 0 (
echo del "%%p"
) else (
if %verbose% EQU 1 echo deleting %%p
del "%%p"
)
)
)
)
exit /B 0
:isDir
setlocal
set "v=%~1"
if ["%v:~0,-1%\"] == ["%v%"] (
if exist "%v%" exit /b 1
) else (
if exist "%v%\" exit /b 1
)
exit /b 0
endlocal
:usage
echo Usage: %prog_name% /d ^<targetPath^> /m ^<minSize^> /t ^<fileType^> [/r] [/c] [/v] [/h]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /d Target directory.
echo /m Minimum file size. All files below the minimum file size will be deleted.
echo /t File type filter. I.e. "log"
echo /r Iterate dir recursively.
echo /c Checking mode. Don't delete files, just print, what would be deleted, if this flag would not have been set.
echo /v Verbose mode.
echo /h Print this.
exit /B 0