Delete file by previous month with VBScript

VBScript ("Microsoft Visual Basic Scripting Edition")
Script delete file by previous month with VBScript
Set shell = CreateObject("WScript.Shell")	'Set for call batch file
shell.CurrentDirectory = "c:\Testscript"	'Location batch file
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "d:\FileBackup"		'Folder to start deleting (subfolders will also be cleaned)
PrevYear = Year(DateAdd("yyyy", -1, Date))
PrevMonth = Right("0" & Month(Date),2)
PrevDay = Right("0" & day(Date),2)
OlderThanDate = PrevMonth & PrevYear
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName, BeforeDate)
	Dim folder, file, fileCollection, folderCollection, subFolder	
	Set folder = fso.GetFolder(folderName)
	Set fileCollection = folder.Files
	'wscript.echo fso.GetFileName(file.Path)		'Test show file path
	if folder.Files.Count > 0 then
	  For Each file In fileCollection		
		FullDate = Right("0" & Month(file.DateLastModified),2) & Year(file.DateLastModified)		'Format month
		if FullDate = BeforeDate then
			'wscript.echo fso.GetFileName(file.Path)		'Test show file path
			fso.DeleteFile(file.Path)	' Delete previous file name			
			shell.Run "Batch file.bat"	'Can run batch file move data and log this here
		else
			shell.Run "Batch file.bat"	'Can run batch file move data and log this here
		end if	
	  Next	
	else
		shell.Run "Batch file.bat"	'Can run batch file move data and log this here
	end if	
End Function
Save type file is .vbs
Run vbscript.vbs file with Batch file.bat
Call c:\vbscript.vbs

 

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *