Friday, September 11, 2009

Powershell Deleting files By Date

When dealing with a computer, one must always be aware of proper syntax. For you could have a problem like Geordi and Data when trying to play Holmes on Next Gen.

For the last few days I have been working on a Powershell script to delete files on a server that are older than X days. Truely harder than I ever thought.

Why you ask, syntax of the code. "Give me an adversary capable of defeating Data" goes the line. This is where my troubles started. I was able to find many different scripts on the internet that claimed to do what I wanted. Logically it was: Delete all the files under this directory (and Sub Directories) that are older than X; leaving newer files.

Now one never wants to practice scripts on live servers, so I copied a number of files over to my workstation and started from there. The really sharp people will know what happened here.. dont worry if you missed the problem, I did too. Moving right along. I used several of the script I read on the net with different degrees of success and failor. It was at this point I desided to write the script on my own, pull from different education sourses and other scripts to accomplish what I needed.

Here is the file script before I knew the problem. First I use alot of variables in my scripts, only because it helps me keep track of things. I am Admin not a programer:

$a = Get-ChildItem C:\Purgetest\test -Recurse
foreach($x in $a)
{
$y = ((Get-Date) - $x.CreationTime).Days
if ($y -gt 7 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}


Real easy.

Now the Break down:

$a = Get-ChildItem C:\Purgetest\test -Recurse
Varible "$a" will equal all the files and folders under "C:\Purgetest".
Next we move into the Loop.
foreach($x in $a)
For every file (called $x) in directory ($a) do this and the loop starts.
Now we are in the loop.
$y = ((Get-Date) - $x.CreationTime).Days
For the loop I used a varible ($y) to get a result of how old a file was. Basically the math goes: Give me todays date (GET-DATE) and subtract the date of creation of the file ($x.CreationTime).days. The .DAYS extension tells the script to round the result to the nearest Day, I didnt need hours, minutes or seconds. So now I have an even number of days for how old the file is.
if ($y -gt 7 -and $x.PsISContainer -ne $True)
{$x.Delete()}
The If statement performs several equations on the file. First it checks if the result I got from $y which is the age of the file is GREATER THAN the number "7" for 7 days. At the same time, it confirms that the "file" I am looking at is actually a file and not a folder. That's what the "AND" statement is doing in the script. The math is: If the item is not a folder and its older than 7 days delete it.

Nifty, sure is. However it didnt work. Every file in my test failed over and over. It didnt deleted the files...... why? Remember Geordi from that episode of Next Gen?? I made the same mistake. The script worked perfectly, just not the outcome I wanted. Syntex Syntext syntex.

Let me explain. If you are looking at the files in detailed mode in Windows, by default you will see a date. By all accounts this is the age of the file.... the last time it was MODIFIED. Seeing the issue now. Nope, ok I shall continue. In my test directory, the files have the same modify date as the original files. However and here comes the source of the mistake, these are copies! So the CREATION date on my workstation would be today and not the same as the original.

Lighting struck. That was the problem. A quickfix on paper, but long time to troubleshoot. To come to that conclusion I moved files I didnt want to my Purgetest directory and noticed they were being deleted. I also tested my code line by line in Powershell. They all worked independently, but not in the loop.

Soooooo, what is the fix. I need to remove the files in my test folder by the last time they were writen too. In Powershell the line goes from this:
$y = ((Get-Date) - $x.CreationTime).Days

to this:

$y = ((Get-Date) - $x.LastWriteTime).Days
Now it works perfectly on my test server.

On a side note. What helped me greatly on this script in Powershell is PowerGUI Script Editor. Its a app that will walk you through the code of your script in an interface much like Microsoft Visual Editors. I HIGHLY recommend downloading it. Its free to use. http://powergui.org/index.jspa

-Boston Tech Out

No comments:

Post a Comment