Link to home
Get AccessLog in
Avatar of ethar turky
ethar turkyFlag 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

Avatar of RobSampson
RobSampson
Flag of Australia image

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

Avatar of 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
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
thanks