Use VBS to report some types of disk space and delete files

Time:2020-10-29

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 \”‘set the path to be scanned (absolute path), and use “|” to separate multiple paths
Serverpath = “\ \ server / log $\”‘set the shared folder (writable share) on the server to store the deletion log
Onlyone = “yes” sets whether to scan only once. If you want to scan every time, please change to “no”.
Notfinddel: “yes”‘If you don’t find anything to delete, delete the log file. (if 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 only “no” then
     Set dicdrv = CreateObject(“Scripting.Dictionary”)
     Set logfile = fso.OpenTextFile(logfilepath,8,True)
     logfile.WriteBlankLines(1)
     logfile.WriteLine “#####################################################”
      logfile.WriteLine “Start scanning –” & 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 “Scan completed, check” & FNum & “files, deleted” & donum & files, total “& 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 & MS, “& 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 “found” & FNum & “files” & Chr (10) & “deleted” & donnum & “time consuming” & TM & “Ms”
‘if you don’t need to display the execution results 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 are the file types you want to delete. Of course, they can also be ^ ^ files such as “TD”, “pdown”
             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 If you just want to see what customers have ordered, please comment out this line
     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