Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Filtering Get-ChildItem results with -LIKE? Expand / Collapse
Posted Tuesday, April 8, 2014 11:49 AM



Group: General Forum Members
Last Login: Today @ 11:24 AM
Points: 12,904, Visits: 31,976
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?

#folder contains the following files for example:


#variable definitions
$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;


--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1559622
Posted Tuesday, April 8, 2014 12:52 PM

Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 6:19 AM
Points: 60, Visits: 889
Just a small change.
if ( $item.Name -like ".sqb") 

if ( $item.Name -like "*.sqb")  

if ( $item.Name -like "*.$extension")  

Also, if you want to cut down the files returned to $fl from Get-ChildItem add the -Filter option.
$fl = Get-ChildItem -path "C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup"  -Filter "$Full_LOG_or_DIFF*$extension"

Post #1559663
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse