Help to write a script

ethar1
ethar1 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
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?
Most Valuable Expert 2012
Top Expert 2014
Commented:
OK, I've replaced strExtension with strFileName to specify a file name.

Also, if you run the script from a command prompt using
cscript C:\Scripts\DeleteFiles.vbs

it will output to the command windows.

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"
strFileName = "myfile.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 LCase(objFile.Name) = LCase(strFileName) 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

Author

Commented:
thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial