** Thanks in advance to anyone that contributes knowledge. Note: I am still new to PowerShell. **
The goal of the script that I need help with does the following:
- Keeps a count of the total number of files in a specific folder called "Futronics Images".
- Keeps a count of the number of files in this folder that are older than 1 hour.
- Any/All files older than 1 hour ... should be deleted using sDelete. (Note: all files NOT older than 1 hour shouldn't be deleted and should stay in this directory).
Start of Current Code:
#Setting source folder path
$SourceFolder = "C:\Users\KDB8916\Pictures\Futronics Images"
#This code counts the total number of files in source folder (i.e., "C:\Users\kdb8916\Pictures\Futronics Images") for logging purposes
$Count1 = @(Get-ChildItem $SourceFolder).Count
$Count1 = "Total number of files in source folder = " + $Count1
echo $Count1
$Count2 = 0
#If file(s) are older than 1 hour ... call sDelete bat file and securely delete file(s)
if(Get-ChildItem $SourceFolder | Where-Object {$_.LastWriteTime -le (Get-Date).AddMinutes(-1)} | ForEach-Object {$Count2++; $_.fullName})
{
#Calling the sDelete batch script twice for Futronics device due to it saving bitmap image in subfolder by default
./sDelete_Futronics_GOOD_2.bat
./sDelete_Futronics_GOOD.bat
$Count2 = "Total number of files in source folder older than 1 hour that have been deleted = " + $Count2
echo $Count2
}
End of Current Code
'./sDelete_Futronics_GOOD.bat' is being called in the IF statement as you can see.
The contents of 'sDelete_Futronics_GOOD.bat' is a simple one liner as seen below:
sdelete -p 5 -s "C:\Users\KDB8916\Pictures\Futronics Images\*"
Note: The way images are being saved in "Futronics Images" is in a subfolder. So when an image is taken with this Futronics device ... it creates a subfolder within the "Futronics Images" parent folder ... and places a Bitmap file in it. Futronics Images >> Subfolder >> Bitmap
The only solution I can find in order to delete both subfolder and bitmap (that are older than 1 hour) is to call sDelete twice. Furthermore, I found that I need to end the sDelete command using '\*' as you can see above. This prevents sDelete from deleting the parent folder --> "Futronics Images" when there is no subfolder & bitmap older than 1 hour to delete.
Issue: When both of the ./sDelete_Futronics_GOOD.bat lines of code are commented out this works as I want it to. It keeps an accurate count of the total number of files in the "Futronics Images" directory, as well as a count of the bitmap files (not subfolders and bitmap files) that are older than 1 hour ... to be deleted by sDelete.
When I uncomment both of the ./sDelete_Futronics_GOOD.bat lines of code ... I still get accurate counts, HOWEVER, sDelete deletes all files and subfolders regardless of whether or not they are older than 1 hour.
(Once this code works it will be run every 15 minutes as a scheduled task with elevated permissions due to using sDelete.exe)
Any suggestions would be greatly appreciated. Thanks.