Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Filtering Get-ChildItem results with -LIKE?

$
0
0
i cannot for the life of me pinpoint my error here.this should be fairly simple: find matching files, and print em out before i explicitly delete them.i'm trying to test each file in a folder to match certain criteria to determine whether i want to delete it or not.eventually it will be four items total,including "older than x days, but i cannot even get the first item to filter and prove it was found.i'm trying to find something that matches a database name between underscores, and starts with "FULL_" and also the "right" extension.As far as i know, it's never even entering /testing my if statement.i thought maybe the underscores were part of the problem with a -LIKE statement, but even removing them doesn't get me to return anything inside the write-host commands.I've tried -like, startswith, and lots of other variations, but i'm obviously stuck on something super basic that my programming skillset assumes or takes for granted.can anyone see an obvious mistake here?[code="plain"]#folder contains the following files for example:#FULL_(local)_CommonSecurity_20140407_141304.sqb#FULL_(local)_dbWarden_20140407_141304.sqb#FULL_(local)_master_20140407_122503.sqb#FULL_(local)_model_20140407_122505.sqb#FULL_(local)_msdb_20140407_122505.sqb#FULL_(local)_ReportServerTempDB_20140407_141305.sqb#variable definitions$dbname="CommonSecurity";$Full_LOG_or_DIFF = "FULL";$extension = ".sqb";$limit = (Get-Date).AddDays(-7)$fl = Get-ChildItem -path "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup" ;foreach($item in $fl) { Write-Host $item.Name; if ( $item.Name -like ".sqb") { Write-Host $item.Name; Write-Host $item.FullName; } #if ( $item.Name -like "_${dbName}_" -and $item.Name.StartsWith("${Full_LOG_or_DIFF}_") -and $item.Name -like $extension -and $_.CreationTime -lt $limit) if ( $item.Name -like $extension ) { Remove-Item $item.FullName -WhatIf; } }[/code]

Viewing all articles
Browse latest Browse all 467

Trending Articles