So I have myself a PowerShell script that after much headaches I got working. Ii deletes files I no longer want and everything is great. Only one issue, it will delete a file whether it is open by another program or not, which is bad. My code is as follows:
# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = [int]30 # 30 days
foreach ($path in Get-Content "pathList.txt") {
# Write information of what it is about to do
Write-Host "Trying to delete files older than $oldTime days, in the folder $path" -ForegroundColor Green
# deleting the old files
Get-ChildItem $path -Recurse -filter "*EDI*" | WHERE {$_.LastWriteTime -le $(Get-Date).AddDays(-$oldTime)} | Remove-Item -Force
I just need a way for the script to see that a file is open, skip said file and move on. I'm running PowerShell 2.0 on Windows 7 SP1.