воскресенье, 26 января 2014 г.

Как удалить временные файлы во всех профилях Windows с помощью скрипта.

Предлагаю вам скрипт для удаления временных файлов во всех профилях Windows:

Весьма удобно при использовании программ, поддерживающих удаленное выполнение скриптов.





Set fileObject = CreateObject("Scripting.FileSystemObject")

'Used to get a list of files in a folder
Function getFiles(folderName)
 ReDim tempFileList(-1)
 if (fileObject.folderExists(folderName)) then
  set folderObject = fileObject.getFolder(folderName)
  set files = folderObject.Files
  m = 0
  For Each file in files
   ReDim Preserve tempFileList(UBound(tempFileList) + 1)
   tempFileList(m)=file
   m=m+1
  Next
 End if
 getFiles = tempFileList
End Function

'Used to get a list of folders in a folder
Function getSubFolders(folderName)
 ReDim tempList(-1)
 if (fileObject.folderExists(folderName)) then
  set folderObject = fileObject.getFolder(folderName)
  set subFolders = folderObject.subFolders

  Dim i

  i = 0
  for each folder in subFolders
   ReDim Preserve tempList(UBound(tempList) + 1)
   tempList(i) =  folder
   i=i+1
  next
 end if
 getSubFolders = tempList
End Function

'We need to detect the correct folder for user profiles
'since newer versions of windows place it under c:\users
Function getUserFolder()
 if (fileObject.folderExists("C:\Users")) then
  getUserFolder = "C:\Users"
 else
  getUserFolder = "C:\Documents and Settings"
 end if
End Function

Function getTempFolders()
 Dim profileList
 Dim i

 'Get our list of profile folders
 profileList = getSubFolders(getUserFolder())

 'Update them to point to the temp folder
 for i=0 to UBound(profileList)
  profileList(i)=profileList(i) & "\AppData\Local\Temp\"
 next

 getTempFolders = profileList
End Function

Dim profileList
ReDim folderList(-1)
ReDim fileList(-1)

Dim k

'Get our list of temp folders
profileList = getTempFolders()

'Need k as our index for our final list
k=0

'Go through each folder and get a list of subfolders
for i=0 to UBound(profileList)
 Dim tempList
 tempList = getSubFolders(profileList(i))
 for j=0 to UBound(tempList)
  ReDim Preserve folderList(UBound(folderList) + 1)
  folderList(k)=tempList(j)
  k=k+1
 next
next

k=0
'Now we add the root files too
for i=0 to UBound(profileList)
 Dim tempFileList
 tempFileList = getFiles(profileList(i))
 for j=0 to UBound(tempFileList)
  ReDim Preserve fileList(UBound(fileList) + 1)
  fileList(k)=tempFileList(j)
  k=k+1
 next
next


On Error Resume Next

'Delete our folders
for i=0 to UBound(folderList)
 Err.Clear
 fileObject.DeleteFolder folderList(i),true
 if (Err.Number<>0) then
  WScript.Echo "Error deleting: " & folderList(i) & " (" & Err.Description  & ")"
 else
  WScript.echo("Deleted: " & folderList(i))
 end if
next

'Delete our files
for i=0 to UBound(fileList)
 Err.Clear
 fileObject.DeleteFile fileList(i),true
 if (Err.Number<>0) then
  WScript.Echo "Error deleting: " & fileList(i) & " (" & Err.Description  & ")"
 else
  WScript.echo("Deleted: " & fileList(i))
 end if
next


Весьма удобно при использовании программ, поддерживающих удаленное выполнение скриптов.

Комментариев нет:

Отправить комментарий