Script for deleting certain types of files and disk space reports with VBS _VBS

Source: Internet
Author: User
Tags chr

Option Explicit
Dim serverpath,onlyone,notfinddel,workpath,arrworwpath,strworkpath,fso,wsh,wshnetwork,cname,exectime,fnum,donum , Totalsize,t1,t2,tm
Fnum=0
Donum=0
Totalsize=0
Workpath = "f:\| d:\| C:\Download\ "' Here Sets the path (absolute path) that needs to be scanned, using the ' | ' To separate multiple paths
Serverpath = "\\server\log$\" Set up a shared folder (writable share) on the server to store the delete log
Onlyone = "Yes" to set whether to scan only once, if you want to run each time scan, please change to "no".
Notfinddel = "Yes" If this run does not find anything to delete, delete the log file (you want to keep the log every time, please change to "no")

If Right (serverpath,1) <> "\" Then serverpath = serverpath& "\"
Arrworwpath = Split (Workpath, "|")
T1 = timer ()
Set WSH = WScript.CreateObject ("Wscript.Shell")
Wsh.run "regsvr32/s Scrrun.dll", 0,true
Set fso = CreateObject ("Scripting.FileSystemObject")
Set WSHNetwork = WScript.CreateObject ("Wscript.Network")
CName = Wshnetwork.computername
Exectime = Now ()
Dim Dicdrv,logfile,logfilepath,objdrv,drvtotalsize,drvfreespace
LogFilePath = Replace (serverpath&cname& ". txt", ":", "-")
If not FSO. FolderExists (serverpath&cname) Then onlyone = "No"
If Onlyone = "No" Then
Set Dicdrv = CreateObject ("Scripting.Dictionary")
Set logfile = fso. OpenTextFile (Logfilepath,8,true)
LogFile. WriteBlankLines (1)
LogFile. WriteLine "#####################################################"
LogFile. WriteLine "Start Scan--" &now ()
LogFile. WriteBlankLines (1)
For each strworkpath in Arrworwpath
If Right (strworkpath,1) <> "\" Then strworkpath = strworkpath& "\"
Scan (Strworkpath)
If not dicdrv. Exists (UCase (left (strworkpath,1)) & "T") Then
Set objdrv = fso. Getdrive (FSO. GetDriveName (Left (strworkpath,2))
Dicdrv.add UCase (Left (strworkpath,1)) & "T", FormatNumber (objdrv.totalsize/1048576, 0)
Dicdrv.add UCase (Left (strworkpath,1)) & "F", FormatNumber (objdrv.freespace/1048576, 0)
End If
Next
T2 = Timer ()
TM=CSTR (int ((T2-T1) *10000) +0.5)/10)
LogFile. WriteBlankLines (1)
LogFile. WriteLine "Complete Scan, check" &fnum& "files, delete" &donum& "files, Count" &formatnumber (totalsize,0) & "Kb"
Dim Drvkey,i
Drvkey = Dicdrv. Keys
For i = 0 to Dicdrv. Count-1 Step 2
LogFile. WriteLine Left (Drvkey (i), 1) & Disk: Total disk space &dicdrv. Item (Drvkey (i)) & "M, remaining disk space" &dicdrv. Item (Drvkey (i+1)) & "M"
Next
LogFile. WriteLine "Time Consuming" & TM & "milliseconds," &now ()
LogFile. WriteLine "#####################################################"
LogFile. WriteBlankLines (1)
Logfile.close
If Notfinddel = "Yes" Then
If donum = 0 Then fso. DeleteFile Logfilepath,true
End If
If not FSO. FolderExists (serverpath&cname) Then fso. CreateFolder (Serverpath&cname)
MsgBox "Find" &fnum& "&AMP;CHR" (a) & "deleted" &donum& "&AMP;CHR" (a) & "time-consuming" & TM & "milliseconds"
' Do not need to display the execution result on the client, comment out the line above
End If
Wsh.run "regsvr32/u/S Scrrun.dll", 0,true
Set WSHNetwork = Nothing
Set wsh=nothing
Set fso=nothing
Wscript.Quit

Sub Scan (Strfolder_)
Dim Folder_,files,file,ext,subfolders,subfolder
' On Error Resume Next
Set Folder_=fso.getfolder (Strfolder_)
Set Files=folder_.files
For each file in files
Fnum = fnum+1
Ext=fso. Getextensionname (file)
Ext=lcase (EXT)
Select Case Ext
Case "rm", "RMVB", "mpg", "wmv", "MPEG", "3gp", "mp4" ' here is the type of file you want to delete, of course, can also be as "TD", "Pdown" and other ^ ^ files
Doit (file)
End Select
Next
Set Subfolders=folder_.subfolders
For each subfolder in subfolders
If subfolder.name <> "System Volume Information" and Subfolder.name <> "RECYCLER" Then
Scan (subfolder)
End If
Next
End Sub

Sub doit (file)
Dim Strtemp,lngsize,strsizev
strtemp = File.path
Lngsize = CLng (file.size/1024)
Donum=donum+1
TotalSize = totalsize + lngsize
' FSO. DeleteFile file,true ' If you just want to see what the customer has done, comment it out (hehe, you want to see it too)
LogFile. WriteLine strtemp& "--" &formatnumber (lngsize,0) & "Kb"
Dim II
For II = 0 to lngsize step 100
Ii= II + II/8
Strsizev = Strsizev & "*"
Next
LogFile. WriteLine "---" &strsizev
End Sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.