Powershell code to selectively delete files

Binky

Diamond Member
Oct 9, 1999
4,046
4
81
I need a script to delete files of certain extensions if a similarly-named file doesnt exist in either of two other folders? Any windows script language is fine, powershell would be best since I already have a powershell script doing other things daily.

Example:

Main file = tv_show_5-5-12.wtv
log file = tv_show_5-5-12.log

There are several log, xml, and other file formats that are left scattered around and need to be cleaned up. They are always the same name with different extension, so I assume this is easy.

How can I scan certain folders full of these log files, and delete any that don't have an equivalent WTV file in one of two source folders?
 

wayliff

Lifer
Nov 28, 2002
11,720
11
81
have you tried to look anything up? tried to write something yet?
Or do you expect someone to do everything for you?
 

Binky

Diamond Member
Oct 9, 1999
4,046
4
81
I'm researching, but this is not my area of expertise so it takes me a long ass time to accomplish something simple. I'm assuming this is easy for others. Given this relatively simple task somebody could probably lead me in the right direction with almost no effort on their part. I've looked into the following but I haven't assembled anything useful yet.

I can get a list of the existing delete-candidate log/xml/other files in all folders into several variables with get-childitem
I can get a list of the check files (don't delete list) with get-childitem
I can make a loop that can look at each item in the three variables
I can test if a file exists with test-path

I just haven't developed way to test then delete. I should have something that looks pretty ugly and almost works by tomorrow....but I was hoping for a head start.
 

Binky

Diamond Member
Oct 9, 1999
4,046
4
81
I got something working. Not pretty. This is the loop that checks for log files. I have similar loops for other types. It runs a bit slow but at least it works.

1. Create array of WTV files to test against
2. For each log file in the folder, see if a wtv file exists. If not, delete log file to recylce bin.
3. Log any deletions.


Code:
$tv_folder1 = "D:\Recorded TV"
$tv_folder2 = "\\server\Recorded TV"
$xml_folder = "C:\Users\Public\DvrmsToolbox\CommercialsXml"
$File ="D:\TVcleanup.txt"
Add-Type -AssemblyName Microsoft.VisualBasic

Get-Date | Out-File $File -append

$list = Get-ChildItem -Path $tv_folder1 -Filter *.wtv
$list += Get-ChildItem -Path $tv_folder2 -Filter *.wtv
$log_files1 = Get-ChildItem -Path $tv_folder1 -Filter *.log

foreach ($i in $log_files1)
    {
    $log_filename = $i.FullName 
    $log_filename_wtv = split-path $log_filename -leaf
    $log_filename_wtv = $log_filename_wtv.replace(".log", ".wtv")

    $files = $list | Where {$_.Name -match $log_filename_wtv} 
    if(!$files) 
    {
       [Microsoft.VisualBasic.FileIO.Filesystem]::DeleteFile($log_filename,'OnlyErrorDialogs','SendToRecycleBin')
    write-output ("$log_filename has been deleted to the recylce bin") | out-file $file -append
    }
    }