none
How can i search for specific files with characters [] in its name ? RRS feed

  • Question

  • Hello,

    I'm trying to sort (and delete afterwards) some files on a directory.

    No problem since using regular file names.

    But the one I want to show contains "[]", and I don't know how to sort those files...

    Example : get-childitem "*Ninja*"

    this shows me all off the files containing Ninja in their names.

    I want the same thing with files names containing [p1] for example

    get-childitem "*[p1]*" is not the answer

    get-childitem "*`[p1`]*" is not working also...

    Which character allow to read the next one as a simple character and not being understood by PS as a special character.

    Thanks for your help !

    Regards

    Guillaume

    Sunday, May 22, 2016 8:17 AM

Answers

All replies

  • It is not a special character in PowerShell.  It is an illegal or, at minimum, a character that should not be used in a file name.


    \_(ツ)_/

    Sunday, May 22, 2016 8:55 AM
  • You can use -match  operator to do the filtering .

    Get-ChildItem c:\ -File | Where-Object {$_.Name -match ".*\[.*\]"}
    Where \ (slash) helps you with reading [ as simple character.


    Sunday, May 22, 2016 8:58 AM
  • Try it like this:

    Get-ChildItem \test3 -filter '*[p1]*'


    \_(ツ)_/

    Sunday, May 22, 2016 8:59 AM
  • In your case (containing [p1]) , it would be 

    Get-ChildItem c:\ -File | Where-Object {$_.Name -match ".*\[p1\]"}
    Substitute c:\ with the required path.

    • Proposed as answer by mukesh-kumar Tuesday, May 24, 2016 6:55 AM
    Sunday, May 22, 2016 9:06 AM
  • In your case (containing [p1]) , it would be 

    Get-ChildItem c:\ -File | Where-Object {$_.Name -match ".*\[p1\]"}
    Substitute c:\ with the required path.


    Use the filter.  It is much faster.

    \_(ツ)_/

    Sunday, May 22, 2016 9:08 AM
  • Yes. Using -Filter will be much faster. 
    Sunday, May 22, 2016 9:10 AM
  • Yes. Using -Filter will be much faster. 

    Both methods are good to know.  I'm just wanted to point out the speed difference.  For small folders it would not be an issue.

    \_(ツ)_/

    Sunday, May 22, 2016 9:18 AM
  • Thank you ! it is working perfectly !
    Sunday, May 22, 2016 10:30 AM
  • Thank you, your solution is also working smoothly :)
    Sunday, May 22, 2016 10:30 AM