Code: Select all
<#
.NOTES
===========================================================================
SYNOPSIS : Unstructured Data Automation
Created with: Powershell ISE
Created on: 08-Dec-2024
Adjusted on : 09/Dec/2024
Created by: Name
Organization: Company
Filename: ClearFiles.ps1 ( Use Ps File Name here )
Application Name: Unstructured Data Automation
Additions:
============================================================================================
.DESCRIPTION
A description of the file.
#>
#==========================================================================================
$col = @()
$Comp = Get-Content env:computername
$Date = (Get-Date -format "MM-dd-yyyy")
$datetoday = (get-date)
#[Date]$Time = Get-Date -format hh:mm:ss
$CurrUser = GC env:UserName
$AppName = "Unstructured Data Automation"
$Logname1 = "UnstructuredDataLog_"+$Date
$logFilePath = "C:\West\log\$Logname1.log"
#Add-Content $logfile $Comp $Date > $appname - is now created -PassThru
function Write-ToLog {
param (
[string]$message
)
# Step 3: Prepare the log entry with timestamp
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$logEntry = "$timestamp - $message"
# Step 4: Check if the log file exists
if (Test-Path -Path $logFilePath) {
# If the file exists, append to it
$logEntry | Add-Content -Path $logFilePath
}
else {
# If the file does not exist, create it and write the entry
$logEntry | Set-Content -Path $logFilePath
}
}
Write-ToLog -message "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"
Write-ToLog -message "///////////////////-------------------------$AppName - PS Script has started----------------------------------////////////////////////"
Write-ToLog -message "///////////////// --------------------------The current user is ----- $currUser -----------------------------------/////////////////"
$col = @()
$jobstatus = (Get-VBRjob -Name FileShare3_UDataTest).GetLastResult()
if ($jobstatus -Like "Success") {
Write-ToLog -message "Last Backup job is Successful.. ";
Write-ToLog -message "Proceeding with the old files deletion based on the age criteria provided"
## List and capture file information in the share
$files = Get-ChildItem "\\lnvmon03\ECS\Batches\2242048521" -File -Recurse
$flcount = $files.count
$limit = [datetime]::Now.AddDays(-15)
foreach($file in $files) {
$row= "" | Select Folder, File, LastModified
if ($file.LastWriteTime -lt $limit) {
$file
$fldr = $file.Directory.Name
$row.Folder = $fldr
$row.File = $file.Name
$row.LastModified = Get-Date $file.LastWriteTime -Format MM-dd-yyyy
#Remove-item -Path "\\lnvmon03\ecs\Batches\"
Write-ToLog -message "The file '$($file)' has been deleted from the folder '$($fldr)'"
$col+=$row
}
}
Write-ToLog -message "There are '$($flcount)' files found in the folder '$($fldr)'"
$col | Export-csv -NoTypeInformation C:\West\Log\FilesPartofDeletion.txt
}
[Moderator] Added code block for the script