Variable in the file’s name filter.

This topic contains 4 replies, has 3 voices, and was last updated by  Stan Gor 3 years, 8 months ago.

  • Author
    Posts
  • #13154

    Stan Gor
    Participant

    Hello!

    I am new to PowerShell scripting your help will be really appreciated!

    The following works for me by running it as .ps1 script:

    $source="C:\Backup\Full\"
    Get-ChildItem $source -Recurse -filter "aaa_bbb_client_FULL.txt"
    

    The following does not work for me by running it as .ps1 script:

    $client="sclient"
    $source="C:\Backup\Full\"
    Get-ChildItem $source -Recurse -filter "aaa_bbb_" + $client + "_FULL.txt"
    

    error I am getting:
    Get-ChildItem : A positional parameter cannot be found that accepts argument '+
    '.

    Please help me to understand what I am missing.

    Regards

  • #13155

    Don Jones
    Keymaster

    well, you've put "sclient" into the second example, whereas the first used "client". But, you don't need to concatenate:


    Get-ChildItem $source -Recurse -filter “aaa_bbb_$($client)_FULL.txt”

    Ought to give the same result.

  • #13156

    Richard Siddaway
    Moderator

    Try
    Get-ChildItem $source -Recurse -filter “aaa_bbb_$client_FULL.txt”

    or

    Get-ChildItem $source -Recurse -filter (“aaa_bbb_” + $client + “_FULL.txt”)

    Filter expects a single string to define the filter criteria. Using + (concatenation) it only looks at the first string and treats the rest as different parameters

  • #13157

    Stan Gor
    Participant

    Thank you so much for your help!!!! It worked.

    PS

    Sorry for typo

  • #13159

    Stan Gor
    Participant

    The following worked!!!

    Get-ChildItem $source -Recurse -filter “aaa_bbb_$($client)_FULL.txt” 

You must be logged in to reply to this topic.