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
Author
Message
Posted Tuesday, April 8, 2014 11:49 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 12,890, Visits: 31,853
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:

#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;
}
}


Lowell

--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: Tuesday, July 29, 2014 6:25 AM
Points: 60, Visits: 883
Just a small change.
from
if ( $item.Name -like ".sqb") 

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

and
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