Link to home
Create AccountLog in
Avatar of marexit
marexitFlag for United Kingdom of Great Britain and Northern Ireland

asked on

compare lastwritetime to lastaccesstime

hi

am trying to compare 500+ files in a directory: say the files were last written to at 0700, but only about 200 have been accessed since (we download all these files everyday and am trying to weed out the ones nobody uses - but of course no one can give me a concrete answer).

so what i want to do is compare the write time and access time so that i can see which files have been accessed.
what i have so far is this (not really querying C:\ - just an example):
get-ChildItem "C:\" | ForEach-Object {
$test = New-TimeSpan ($_.Lastaccesstime) $($_.lastwritetime)
if ($test.minutes -gt '5'){
echo $_.name
}}

...but this doesnt work. please help!
thanks
Avatar of chrismerritt
chrismerritt

Does this help? I had to workaround the fact that even if it was only milliseconds out it didn't match the times together, so now it matches as long as the times are within the same second at least.

I don't know what you want to do with the logic of this but hopefully you can see how it works?

$Directory = "C:\TEMP"
[array]$Files = gci $Directory -Recurse | ? {$_.PSIsContainer -eq $False}

if ($Files.Count -gt 0)
{
	foreach ($File in $Files)
	{
		$LastWriteTime = $File.LastWriteTime.DateTime
		$LastAccessTime = $File.LastAccessTime.DateTime

		if ($LastWriteTime -gt $LastAccessTime)
		{
			"{0, -20} {1} {2, -10}" -f "File", ":", $File.FullName
			"{0, -20} {1} {2, -10} {3}" -f "Most Recent", ":", $File.LastWriteTime, " - Last Write Time"
		}
		elseif ($LastWriteTime -eq $LastAccessTime)
		{
			"{0, -20} {1} {2, -10}" -f "File", ":", $File.FullName
			"{0, -20} {1} {2, -10} {3}" -f "Most Recent", ":", $File.LastWriteTime, " - Times Match"
		}	
		else
		{
			"{0, -20} {1} {2, -10}" -f "File", ":", $File.FullName
			"{0, -20} {1} {2, -10} {3}" -f "Most Recent", ":", $File.LastAccessTime, " - Last Access Time"
		}
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of marexit

ASKER

hey thanks for your input. think learnctx is closer to what i need.

not looked at this for ages, so sorry its taken so long to get back to you. just realised the lastwritetime wasnt modified at the time of download (we download these 500 files daily). so i'm wondering if i can change the lastwritetime to an actual time like 0600? we download these files and i want to know if they are being accessed since they were downloaded.
tried setting the end time with get-date but not certain i have the right idea...
any thoughts?
Avatar of marexit

ASKER

Actually, i think i got it:

GCI "c:\test" | % {   if ((New-TimeSpan $('06:00') $_.LastAccessTime).TotalMinutes -ge 455 {$_.Name }}

points to learnctx for pointing me in the right direction

thanks!! :)
You can alter the LastWriteTime attribute.
Get-ChildItem C:\Temp | Foreach-Object {
   $_.LastWriteTime = (Get-Date).date.addhours(6)
}

Open in new window

This will take every file in that directory and set the LastWriteTime attribute to 6AM on the day you're running it.