Avatar of ethar turky
ethar turky
Flag for Saudi Arabia asked on

Help to write a script

Dear all,
Need a help to complate the following script to delete all files in a specific folder and subfolders if specific cretiria are met ( file name is "xxxx.yyy" and file size <= 1kb)
also to print out on command windows the deleted files with its full path
thanks,
'' /// Setup environment declarations and object references ///
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell = CreateObject("Wscript.Shell")
Dim objFolder:Set objFolder = objFSO.GetFolder("D:\webspace")


CDPath="D:\webspace"
' /// Run the main routine, ProcessFolder, a recursive search through the files/folders found in the objFolder reference ///
  ProcessFolder (objFolder)

' /// Destroy object references, finalizing the script ///
Set objFolder=Nothing
Set objShell=Nothing
Set objFSO=Nothing
wscript.quit

' /// Main routine to scan current folder and recursively call against sub folders ///
Sub ProcessFolder(fld)
  Set subFld = objFSO.GetFolder(fld) ' Set object reference against current folder
  ProcessFile (subFld) 'Call Processfile routine against current folder
  For Each fld In subFld.SubFolders 'loop through all subfolders, recalling the same routine
    ProcessFolder (fld) 
  Next
End Sub


' /// Routine to process all container files within the passed folder reference
Sub ProcessFile(fld)




  Set objFld = objFSO.GetFolder(fld) 'create object reference to the current folder
  For Each file In objFld.Files 'loop through all files found
  If objFSO.GetExtensionName(file) = "php" Then 
  

		 objFSO.Delete

    Set objFile = Nothing 'destory object reference
  End If
Next


End Sub

Open in new window

VB Script

Avatar of undefined
Last Comment
ethar turky

8/22/2022 - Mon
RobSampson

Hi, try this version.  I have added strExtension for you to specify the extension name, and also intSize to specify a size threshold.

Regards,

Rob.

'' /// Setup environment declarations and object references ///
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell = CreateObject("Wscript.Shell")

CDPath="D:\webspace"
strExtension = "php"
' size in bytes to delete files if they are less than this
intSize = 1024

' /// Run the main routine, ProcessFolder, a recursive search through the files/folders found in the objFolder reference ///
ProcessFolder (CDPath)

' /// Destroy object references, finalizing the script ///
Set objFolder=Nothing
Set objShell=Nothing
Set objFSO=Nothing
wscript.quit

' /// Main routine to scan current folder and recursively call against sub folders ///
Sub ProcessFolder(fld)
	For Each objFile In objFSO.GetFolder(fld).Files 'loop through all files found
		If objFSO.GetExtensionName(objFile.Path) = "php" And objFile.Size < intSize Then 
			WScript.Echo "Deleting " & objFile.Path
			objFile.Delete True
		End If
	Next
	For Each objSubFld In objFSO.GetFolder(fld).SubFolders 'loop through all subfolders, recalling the same routine
		ProcessFolder (objSubFld.Path)
	Next
End Sub

Open in new window

ethar turky

ASKER
Thanks dear RobSampson
but I am looking to delete complate file name not only extention.
also to make output to command windows not a message box.

the file size in bytes?
ASKER CERTIFIED SOLUTION
RobSampson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ethar turky

ASKER
thanks
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23