Jump to content

Recommended Posts

Posted

Hi all,

I'm glad I found this forum. I was looking for help with this issue but the deployment discussions are great.

I made a simple bat file to clear out temp files for users but I'd like to add a bit of information output to it.

I made it for both Win7 and XP, if the directory doesn't exist it doesn't do anything so I just added both OS directories. If anyone has any other directories to suggest to clean out please let me know. I've read a bit about prefetch not needing to be cleaned but a peer likes to empty it so I added it.

I'd like to be able to count the files (and files in subfolders) in each temp folder and display a total number of files cleaned once the script is completed. (Or display the number of files before deletion if that's tricky)

Here is what I put together, it's really basic. Any ideas on how to add a total number of files cleaned output to it?

Thanks for any help.


del /s /q "c:\windows\prefetch\*.*"
del /s /q "c:\windows\temp\*.*"
del /s /q "%homepath%\Appdata\Local\Temp\*.*"
del /s /q "c:\temp\*.*"
del /s /q "%homepath%\Local Settings\Temporary Internet Files\*.*"
del /s /q "%homepath%\Local Settings\Temp\*.*


Posted

Something like:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A Counter=0
FOR %%A IN (
"c:\windows\prefetch\*.*"
"c:\windows\temp\*.*"
"%homepath%\Appdata\Local\Temp\*.*"
"c:\temp\*.*"
"%homepath%\Local Settings\Temporary Internet Files\*.*"
"%homepath%\Local Settings\Temp\*.*
) DO (
CALL :do_count %%A
CALL :do_del %%A
)

GOTO :EOF

:do_count
FOR /F %%B IN ('dir /b %1') DO (
SET /A Counter+=1
ECHO !Counter!
)
GOTO :EOF

:do_del
ECHO del %1
GOTO :EOF

:unsure:

jaclaz

Posted

Thank you for your response.

I spent a bunch of time trying to figure this out too. It seems like too much of a hassle just to show a number and show the bat is worthwhile to a user though. If they see a performance increase that's enough for me.

Posted

Here's a script which should do at least what you require!

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR %%! IN (
"X:\Path To\Some Directory"
"X:\Same Path To\Another Directory"
"X:\Different Path To\A Folder"
) DO (
CALL :CN "%%~f!\*" #
CALL ECHO=Of %%#%% file(s^) located in %%~f!
DEL/Q "%%~f!\*.*"
CALL :CN "%%~f!\*" _
CALL SET/A £=%%#%% - %%_%%
CALL ECHO=%%£%% were successfully deleted!
CALL SET/A T +=%%£%%
)
ECHO=
ECHO=%T% files were removed during the cleanup process.
ECHO=
PAUSE
GOTO :EOF
:CN
SET "%2=0"
FOR %%$ IN (%1) DO SET/A %2 +=1

All you need to do is change the example paths I've provided, (lines 4-6), to the ones you're cleaning.

Posted

Here a VBS script

1:\ Run Csript

2:\ Ask if you want to continue or quit

3:\ If Continue will attempt to delete all files in the listed Dir Array

4:\ Show The Total Amount Of Files Deleted And The Amont Deleted In Mb Or Kb

CleanUp.vbs


'-> Objects To Use uring Runtime
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Pathway Varibles
Dim Win :Win = Act.ExpandEnvironmentStrings("%Windir%")
Dim UsD :UsD = Act.ExpandEnvironmentStrings("%UserProfile%")
'-> Array For Directories
Dim Dir :Dir = Array( _
Win & "\prefetch", _
Win & "\temp", _
UsD & "\Appdata\Local\Temp",_
"c:\temp", _
UsD & "\Local Settings\Temporary Internet Files", _
UsD & "\Local Settings\Temp")
'-> Varibles To Be Used During Runtime
Dim C1, C2, File, Folder, Lne, Obj, Reply
Lne = " -------------------------------------------------------------- "
'-> To Make Sure This Runs In Cscript Only
If InStr(1,WScript.FullName,"cscript",1) Then
YesNo()
ElseIf InStr(1,WScript.FullName,"wscript",1) Then
MsgBox vbTab & "Error Wrong Scripting Engine" & vbCrLf & _
"To Use This Script It Must Be Run Under Cscript.exe" & vbCrLf & _
"Right Click This Script And Select Command Prompt" & vbCrLf & _
"Option, To Open This Script Correctly",4128,"Error Wrong Engine"
End If
'-> Yes Or No Function
Function YesNo()
'-> Show List Of Folder To Delete Files
WScript.StdOut.WriteLine " Directories To Clean Up" & vbCrLf & Lne
For Each Obj In Dir
If Fso.FolderExists(Obj) Then
WScript.StdOut.WriteLine " " & Obj
End If
Next
WScript.StdOut.WriteBlankLines 1
Do While Len(Reply) < 5
WScript.StdOut.WriteLine Lne
WScript.StdOut.WriteLine " Do You Want To Continue With Deleting All"
WScript.StdOut.WriteLine " Above Listed Directories Files. This Script"
WScript.StdOut.WriteLine " Will Attempt To Delete All Files In All Sub"
WScript.StdOut.WriteLine " Directories. Type Yes To Continue And No To"
WScript.StdOut.WriteLine " Exit And Do Nothing"
WScript.StdOut.WriteLine Lne
WScript.StdOut.WriteBlankLines 1
Reply = Wscript.StdIn.ReadLine
'-> User Confirms
If InStr(1,Reply, "yes",1) Then
For Each Obj In Dir
If Fso.FolderExists(Obj) Then
Recursive Fso.GetFolder(Obj)
End If
Next
'-> Converts Number To Either Mb Or Kb
If C2 > 1048576 Then
C2 = FormatNumber(C2 / 1048576,2)
C2 = C2 & " Mb"
Else
C2 = FormatNumber(C2 / 1024,2)
C2 = C2 & " Kb"
End If
'-> End Of Script
WScript.StdOut.WriteLine " Total Files Deleted : " & C1
WScript.StdOut.WriteLine " Total Sizes Deleted : " & C2
WScript.StdOut.WriteLine " Press Enter To Exit"
Do While WScript.StdIn.AtEndOfLine
WScript.Quit()
Loop
'-> User Cancel
ElseIf InStr(1,Reply, "no",1) Then
WScript.Quit
End If
Loop
End Function
'-> Loop Threw All Sub Folders
Function Recursive(Loc)
WScript.StdOut.WriteLine Lne & vbCrLf & _
" Procesing Folder : " & Loc.Name & vbCrLf & lne
On Error Resume Next
For Each File In Loc.Files
C1 = C1 + 1
C2 = Int(C2) + Int(File.Size)
WScript.StdOut.WriteLine File.Name
Fso.DeleteFile(File.Path),True
Next
For Each Folder In Loc.subFolders
Recursive Folder
Next
End Function

Rename CleanUp.vbs.txt to CleanUp.vbs to make active

CleanUp.vbs.txt

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...