Filtering Get-ChildItem results with -LIKE?

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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"

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply