Link to home
Start Free TrialLog in
Avatar of ramachandrawithu
ramachandrawithu

asked on

script to delete files...

hi all,

In my organisation, we are deleting files older than 36hrs every time. we are doing it manually. can anyone please help me to build a script which runs in windows server 2012. which i can use in scheduler to run automatically.
Avatar of Professor J
Professor J

you can try with this

Change the sRoot and nMaxFileAge variables to suit your needs.To run the script periodically, save it to a location like C:\Scripts\delete_old_files.vbs and create a scheduled task with a Run setting of "cscript //B C:\Scripts\delete_old_files.vbs"

On Error Resume Next

Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "C:\YourPath To Old Files"			'Path root to look for files
today = Date
nMaxFileAge = 36					'Files older than this (in days) will be deleted

DeleteFiles(sRoot)

Function DeleteFiles(ByVal sFolder)

	Set oFolder = oFileSys.GetFolder(sFolder)
	Set aFiles = oFolder.Files
	Set aSubFolders = oFolder.SubFolders

	For Each file in aFiles
		dFileCreated = FormatDateTime(file.DateCreated, "2")
		If DateDiff("d", dFileCreated, today) > nMaxFileAge Then
			file.Delete(True)
		End If
	Next

	For Each folder in aSubFolders
		DeleteFiles(folder.Path)
	Next

End Function

Open in new window



or if you want a more advanced version, then please refer here
Do you need to process subfolders, or just the files in one folder?

If just one folder my simple approach in VBS would be as follows.

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("c:\temp")
For Each objFile In objFolder.Files
   If DateDiff("h", objFile.DateLastModified, Now) > 36 Then 
      objFile.Delete(True)
   End If
Next

Open in new window

~bp
powershell
function delete-oldfiles {
<#
.Synopsis
   Deletes files starting at a base location with a last modified date older than a Maximum Age
.DESCRIPTION
   Long description
.EXAMPLE
   delete-oldfiles.ps1 -basefolder c:\temp\ -maxage 30
.EXAMPLE
   delete-oldfiles.ps1 will delete files from c:\temp and subfolders with a modified date of more than 3 days ago
.EXAMPLE
delete-oldfiles -BasePath c:\abcd  will delete files from c:\abcd and subfolders with a modified date of more than 3 days ago
.EXAMPLE
delete-oldfiles -Maxage 180 will delete files from c:\temp and subfolders with a modified date of more than 180 days ago
.INPUTS
   Inputs to this cmdlet (if any)
.OUTPUTS
   none
    #>
    [CmdletBinding()]
    Param ( [string] $BaseFolder = 'c:\temp',[int] $MaximumAge = '3' )
    write-Debug ($BaseFolder)
    write-Debug ($MaximumAge)
    $cutoffdate= (get-date).adddays(-1 * $maximumage)
    $files = Get-ChildItem -Path $BaseFolder -Recurse 
    foreach($file in $files){
    if  ($file.LastWriteTime -lt $cutoffdate) 
                { 
#remove -whatif if done testing
                remove-item -Path $file.FullName -whatif
                }
    }
}

Open in new window

@ProfessorJimJam

Did you mean to use 36 days difference as your criterion?
I expected to see this:
If DateDiff("h", dFileCreated, today) > nMaxFileAge Then

Open in new window

Aikimark
Many thanks for spotting that issue, it was incorrect to have d because it was 36 hours not 36 days.

So, my first post code line # 18 "d" to be replaced by "h".

Thanks again
SOLUTION
Avatar of Professor J
Professor J

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ramachandrawithu

How many files are in the directory (tree?) and what percentage of the files are usually eligible for deletion?
Avatar of ramachandrawithu

ASKER

sorry for the late response.

my requirement is, we have a server and we created one folder and gave access to all the users. in that, we need to delete files and folders which are not used more than 2 days (48 hrs - preferably in hours calculation only.). but the script should check that before deleting the folder it should check all the sub-folders and files with in that sub folders.

almost 20 to 25% of files eligible for deletion and that main folder is almost more than 80 GB data is there.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial