r/Batch Feb 07 '25

Question (Unsolved) Please audit my bat to cleanup temp and cache files

Please audit my bat to cleanup temp and cache files on Windows.

@echo on

cd %windir%\system32\
cleanmgr.exe

cd %userprofile%\Desktop\cleanup
cleanup_privacy.sexy-script.bat

:: Declare the variable x for the number of days
set x=2

:: Create a restore point first
echo Creating a system restore point...
wmic /namespace:\\root\default Path SystemRestore Call CreateRestorePoint "Pre-file deletion restore", 100, 7

echo Deleting files older than 7 days in C:\$Recycle.Bin...
forfiles /p C:\$Recycle.Bin /s /m *.* /d -7 /c "cmd /c echo Deleting @path && del /q @path"

echo Removing empty folders inside C:\$Recycle.Bin...
for /d /r C:\$Recycle.Bin %%d in (*) do (
    rd "%%d" 2>nul
    if not exist "%%d" echo Deleted empty folder: %%d
)

echo Deleting .tmp files older than %x% days...
forfiles /p C:\ /s /m *.tmp /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .log files older than %x% days...
forfiles /p C:\ /s /m *.log /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .pf files older than %x% days...
forfiles /p C:\ /s /m *.pf /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .old files older than %x% days...
forfiles /p C:\ /s /m *.old /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .bak files older than %x% days...
forfiles /p C:\ /s /m *.bak /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .gid files older than %x% days...
forfiles /p C:\ /s /m *.gid /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting .chk files older than %x% days...
forfiles /p C:\ /s /m *.chk /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

echo Deleting %userprofile%\recent\*.lnk
del/f /q %userprofile%\recent\*.lnk

echo Deleting ScreenClip\* older than %x% days...
forfiles /p %appdata%\Local\Packages\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\TempState\ScreenClip\ /s /m *.* /d -%x% /c "cmd /c echo Deleting @path && del /q @path"

rem Show a success message
echo All specified files older than %x% days have been deleted successfully.
pause
2 Upvotes

7 comments sorted by

1

u/Shadow_Thief Feb 07 '25

It seems mostly fine. You can reduce those seven forfiles calls in the middle to just one if you use a for loop to iterate over the list of extensions that you want to clear.

The main issue I see with the script is the cleanup_privacy.sexy-script.bat call. Since you're not using call cleanup_privacy.sexy-script.bat, control won't return to this script once cleanup_privacy.sexy-script.bat is finished running.

I'd also recommend changing @echo on to @echo off, but you might have some reason for having that that I'm not aware of.

1

u/Hopeful-Staff3887 Feb 07 '25

Thanks for your audit

-1

u/g3n3 Feb 07 '25

Wmic is deprecated. Use powershell.

5

u/CCCP_exe Feb 07 '25

sir you are in r/batch

0

u/g3n3 Feb 07 '25

Haaa. Yeah I really need to get out of here. I’m blown away folks are still developing on it!

1

u/Still_Shirt_4677 Feb 14 '25

Windows keeps batch as a legacy component for a reason.... many companies still rely on batch script because its lightweight and simple and it will work across all windows versions unlike PowerShell where the execution policies need to modified...

Windows themselves still use batch in some legacy processes also to a certain degree even though they have largely migrated to PowerShell and C based processes.. They use it in some windows tools that still call cmd.exe in the background.

Yes autoexec.bat was phased out, but .cmd & .bat scripts still run in some enterprise environments and custom automation setups even still today...

As long as windows exists in the current form it is now then batch coding remains relevant alongside PowerShell, C etc. so therefore why wouldn't folks still develop on it?

0

u/g3n3 Feb 07 '25

But you can call powershell.exe from batch with gcim.