-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnityLibrariesCleaner.bat
More file actions
289 lines (267 loc) · 8.64 KB
/
UnityLibrariesCleaner.bat
File metadata and controls
289 lines (267 loc) · 8.64 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@echo OFF
rem VERSION INFO
set "Version=V1.1.0"
set "Date=2026-03-01"
rem About this version:
rem - Optimized scanning speed (replaced batch file counting with native directory checks).
rem - Optimized deletion speed (removed redundant file verification).
rem - Improved Unity project detection using Assets/ProjectSettings structure.
rem Save the original encoding:
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
rem Change the encoding (for accented characters):
chcp 1252>nul
rem Enable additional features:
setlocal enableextensions enabledelayedexpansion
rem Adjust the current directory (useful in admin context):
cd /d "%~dp0"
rem Variables for text formatting:
set "Bold=[1m"
set "Normal=[0m"
set "Red=[91m"
set "Yellow=[93m"
set "Green=[92m"
set "RedBackground=[1;41m"
set "YellowBackground=[1;43m"
set "GreenBackground=[42;1m"
set "Separator=************************************************************************************************************************"
set "GreenSeparator=%GreenBackground%%Separator%%Normal%"
set "RedSeparator=%RedBackground%%Separator%%Normal%"
set "YellowSeparator=%YellowBackground%%Separator%%Normal%"
set /a maxPathLength=256
set /a maxTotalPathLength=8192
rem Variables for finding Library folders:
set "SceneFile=LastSceneManagerSetup.txt"
set "BuildSettingsFile=EditorUserBuildSettings.asset"
set "LibraryFolder=Library"
rem Spinner for progress indication:
set "spinChars=|/-\"
set /a spinIdx=0
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
echo %GreenSeparator%
echo.
echo %Bold% UNITY LIBRARIES CLEANER %Normal%- %Version% (%Date%)
echo https://github.com/JonathanTremblay/UnityLibrariesCleaner (CC0 license)
echo.
echo %GreenSeparator%
echo.
echo This script frees up disk space by emptying the Library folders of your Unity projects.
echo (When you open a project, Unity can rebuild the content of the Library folder.)
echo.
echo Usage and warnings:
echo - The .bat file must be placed in a folder that contains one or more Unity projects.
echo - The script first finds all valid Library folders and then offers Manual or Automatic mode.
echo - The folder contents are permanently deleted (not placed in the recycle bin).
echo - The "Library/LastSceneManagerSetup.txt" and "Library/EditorUserBuildSettings.asset" files are preserved
echo (these files are keeping the last opened scene of projects and their BuildSettings).
echo - The script cannot process paths if they contain exclamation marks.
echo.
echo %GreenSeparator%
echo.
echo Searching for folders that can be %Bold%emptied%Normal%:
rem Find Library folders containing files to delete:
set /a counter=0
rem Find Library folders with invalid paths:
set /a impossibleCounter=0
rem Create an array named "folders" to store the folders to process:
set "folders="
set "impossibleFolders="
set "lastFolder=###"
set /a lastLength=0
set /a currentTotalLength=0
for /f "tokens=* delims=" %%d in ('dir /ad /b /s "%LibraryFolder%" 2^>nul') do (
set "currentFolder=%%~d"
set /a "spinIdx=(spinIdx+1) %% 4"
for %%i in (!spinIdx!) do set "spinChar=!spinChars:~%%i,1!"
<nul set /p "=!spinChar!!BS!"
call set "currentFolderSub=%%currentFolder:~0,!lastLength!%%
if "!currentFolderSub!" neq "!lastFolder!" (
rem Calculate path length:
set "s=#%%~d"
set "currentLength=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "currentLength+=%%N"
set "s=!s:~%%N!"
)
)
rem Adding characters to take into account a space and quotes:
set /a "currentLength+=3"
rem Verify Unity project structure (parent must contain Assets and ProjectSettings):
set "parentDir=%%~dpd"
set "parentDir=!parentDir:~0,-1!"
if exist "!parentDir!\Assets" if exist "!parentDir!\ProjectSettings" (
rem Fast content check: subdirectories or extra files beyond preserved ones?
set "hasContent=0"
for /f "delims=" %%x in ('dir /ad /b "%%~d" 2^>nul') do set "hasContent=1"
if !hasContent! equ 0 (
for /f "delims=" %%x in ('dir /a-d /b "%%~d" 2^>nul ^| findstr /v /i /c:"%SceneFile%" /c:"%BuildSettingsFile%"') do set "hasContent=1"
)
if !hasContent! equ 1 (
if !currentLength! gtr !maxPathLength! (
set "impossibleFolders=!impossibleFolders! "%%~d""
set /a impossibleCounter+=1
) else (
set /a currentTotalLength+=currentLength
if !currentTotalLength! gtr !maxTotalPathLength! (
echo %Red% There is at least one more folder, but the maximum total path length has been reached.
echo %Yellow% Please delete the identified folders, and rerun the script to delete the remaining ones. %Normal%
goto :QUESTION
) else (
set "lastFolder=%%~d"
set /a lastLength=currentLength-3
set /a counter+=1
echo !counter!: %Yellow%%%~d%Normal%
rem Add currentFolder to the folders array:
set "folders=!folders! "%%~d""
)
)
)
)
)
)
<nul set /p "=!BS! "
:QUESTION
set "foldersToDelete=!folders!"
cd /d "%~dp0"
echo.
if %counter% == 0 goto :NOTHING
echo %Normal%%GreenSeparator%
echo.
set "plural="
set "pronoun=this"
if %counter% geq 2 (
set "plural=s"
set "pronoun=these %counter%"
)
echo AVAILABLE MODES:
echo %Bold%[1]%Normal% - MANUAL (choose for each folder, one by one)
echo 2 - AUTOMATIC (delete all folders)
echo 3 - CANCEL (do not delete anything)
echo.
set /p mode= Which mode do you want to use to delete the contents of %pronoun% folder%plural%? (%Bold%[1]%Normal%, 2, or 3)
if "%mode%" == "2" (
echo 2: AUTOMATIC MODE
echo.
goto :AUTOMATIC
)
if "%mode%" == "3" (
echo 3: CANCEL MODE
echo.
goto :STOP
)
rem Default mode:
echo 1: MANUAL MODE
echo.
goto :CHOOSE
:CHOOSE
rem For loop to prompt the user for each folder:
set "foldersToDelete="
set /a total=counter
set /a counter=0
set /a displayCounter=0
for %%d in (%folders%) do (
set "DeleteFolder=N"
set /a displayCounter+=1
set /p "DeleteFolder= !displayCounter!/%total% Delete the folder %Yellow%%%~d%Normal% ? (Y/%Bold%[N]%Normal%) "
if /i "!DeleteFolder!"=="Y" (
rem echo %%~d will be deleted
set "foldersToDelete=!foldersToDelete! "%%~d""
set /a counter+=1
)
)
echo.
set "plural="
if %counter% geq 2 set "plural=s"
set "number=%counter%"
if %counter% geq 1 goto :AUTOMATIC
:NOTHING
echo %GreenSeparator%
echo.
echo %Bold%NOTHING TO CLEAN ^^! %Normal%
echo No Library folder to empty, so the script is done.
echo.
echo %GreenSeparator%
goto :ENDCHECK
:STOP
echo %YellowSeparator%
echo.
echo %Bold%INTERRUPTION ^^! %Normal%
echo Script stopped. No files deleted.
echo.
echo %YellowSeparator%
goto :ENDCHECK
:AUTOMATIC
echo %GreenSeparator%
echo.
echo %counter% deletion%plural% to perform (each deletion may take a few seconds...)
echo.
set /a total=counter
set /a counter=0
rem Clean the Library folders:
for %%d in (%foldersToDelete%) do (
if exist "%%~d" (
set /a "spinIdx=(spinIdx+1) %% 4"
for %%i in (!spinIdx!) do set "spinChar=!spinChars:~%%i,1!"
<nul set /p "=!spinChar!!BS!"
rem Backup preserved files to a temporary folder:
if exist "%%~d_T" rd /s /q "%%~d_T"
mkdir "%%~d_T"
if exist "%%~d\%SceneFile%" move "%%~d\%SceneFile%" "%%~d_T" >nul
if exist "%%~d\%BuildSettingsFile%" move "%%~d\%BuildSettingsFile%" "%%~d_T" >nul
rem Delete subfolders individually to update spinner:
for /d %%k in ("%%~d\*") do (
set /a "spinIdx=(spinIdx+1) %% 4"
for %%i in (!spinIdx!) do set "spinChar=!spinChars:~%%i,1!"
<nul set /p "=!spinChar!!BS!"
rd /s /q "%%k"
)
rem Delete the Library folder root and remaining files:
rd /s /q "%%~d"
rem Restore preserved files (rename temp folder or move files back):
if exist "%%~d_T" (
if exist "%%~d" (
for %%f in ("%%~d_T\*.*") do move "%%f" "%%~d" >nul
rd /s /q "%%~d_T"
) else ( ren "%%~d_T" %LibraryFolder%)
)
set /a counter+=1
echo !counter!/%total% %Yellow% %%~d%Normal% has been emptied
)
)
echo.
echo %GreenSeparator%
echo.
set "plural="
if %counter% geq 2 set "plural=S"
echo %Bold%SUCCESS: %counter% FOLDER%plural% EMPTIED %Normal%
echo Thank you for using Unity Libraries Cleaner ^^!
echo.
echo %GreenSeparator%
:ENDCHECK
if %impossibleCounter% equ 0 goto :END
timeout 2
set "plural="
set "pronoun=it"
set "pronoun2=it"
set "verb=has"
set "verb2=is"
if %impossibleCounter% geq 2 (
set "plural=s"
set "pronoun=they"
set "pronoun2=them"
set "verb=have"
set "verb2=are"
)
echo.
echo %RedSeparator%
echo.
echo %Bold%WARNING ^^! %Normal%
echo The following folder path%plural% %verb% been detected but %verb2% exceeding %maxPathLength% characters, so %pronoun% cannot be deleted.
echo To resolve this issue, move or rename %pronoun2%. Then run the script again^^!
for %%d in (%impossibleFolders%) do echo � %Red%%%~d%Normal%
echo.
echo %RedSeparator%
:END
echo.
chcp %cp%>nul
pause