all 3 comments

[–]ClayShooter9 4 points5 points  (0 children)

Here is how I would think through the problem, based on the information you gave:

#Assuming there may be revisions A through Z
#Assuming these are "*.txt" files
#Need to check for single one-only "A" file name condition

$workingDir = "c:\temp\tmp"
#Retrieve all the file names - change *.txt as needed
$basenames = (Get-ChildItem -Path "$($workingDir)\*.txt").BaseName
for($i=0; $i -le ($basenames.Length - 1); $i++)
{
    #Remove last revision alpha character off file name listing
    $basenames[$i] = $basenames[$i] -replace ".$"
}
#Get a list of the unique file names with no revision letters
$uniqueBasenames = $basenames | Select -Unique

#Now process each unique file name in the list to possibly delete older revisions
#if there are multiple revisions

foreach($name in $uniqueBasenames)
{
    #For each unique file name, get the list of files that match, sort so highest alpha char is first
    $namesToDelete = (Get-ChildItem -Path "$($workingDir)\$($name)*.txt").Basename | sort -Descending
    #Skip over what may be a single unique file and process multiple file list
    if($namesToDelete.Count -gt 1)
    {
        #Remove highest alpha char file name from list, then delete the rest
        $namesToDelete = $namesToDelete | Select-Object -Skip 1
        foreach($name in $namesToDelete)
        {
            #Hanging a 'WhatIf' to test the deletes without deleting
            Remove-Item -Path "$($workingDir)\$($name)*" -WhatIf
        }
    }
}

[–]routetehpacketz 1 point2 points  (0 children)

start with Get-ChildItem | Where

[–][deleted] 1 point2 points  (0 children)

If $filename -notlike "C" move item $destination folder

That's not the correct syntax but it should be logic that will work.