Recursive file search using PowerShell

后端 未结 8 1492
情话喂你
情话喂你 2020-11-28 01:50

I am searching for a file in all the folders.

Copyforbuild.bat is available in many places, and I would like to search recursively.

$Fil         


        
相关标签:
8条回答
  • 2020-11-28 02:33
    Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat
    

    Will also work

    0 讨论(0)
  • 2020-11-28 02:37

    Filter using wildcards:

    Get-ChildItem -Filter CopyForBuild* -Include *.bat,*.cmd -Exclude *.old.cmd,*.old.bat -Recurse
    

    Filtering using a regular expression:

    Get-ChildItem -Path "V:\Myfolder" -Recurse
    | Where-Object { $_.Name -match '\ACopyForBuild\.[(bat)|(cmd)]\Z' }
    
    0 讨论(0)
  • 2020-11-28 02:42

    Here is the method that I finally came up with after struggling:

    Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*
    

    To make the output cleaner (only path), use:

    (Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname
    

    To get only the first result, use:

    (Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname | Select -First 1
    

    Now for the important stuff:

    To search only for files/directories do not use -File or -Directory (see below why). Instead use this for files:

    Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}
    

    and remove the -eq $false for directories. Do not leave a trailing wildcard like bin/*.

    Why not use the built in switches? They are terrible and remove features randomly. For example, in order to use -Include with a file, you must end the path with a wildcard. However, this disables the -Recurse switch without telling you:

    Get-ChildItem -File -Recurse -Path ./bin/* -Include *.lib
    

    You'd think that would give you all *.libs in all subdirectories, but it only will search top level of bin.

    In order to search for directories, you can use -Directory, but then you must remove the trailing wildcard. For whatever reason, this will not deactivate -Recurse. It is for these reasons that I recommend not using the builtin flags.

    You can shorten this command considerably:

    Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}
    

    becomes

    gci './path*/' -s -Include 'name*' | where {$_.PSIsContainer -eq $false}
    
    • Get-ChildItem is aliased to gci
    • -Path is default to position 0, so you can just make first argument path
    • -Recurse is aliased to -s
    • -Include does not have a shorthand
    • Use single quotes for spaces in names/paths, so that you can surround the whole command with double quotes and use it in Command Prompt. Doing it the other way around (surround with single quotes) causes errors
    0 讨论(0)
  • 2020-11-28 02:47

    Use the Get-ChildItem cmdlet with the -Recurse switch:

    Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force
    
    0 讨论(0)
  • 2020-11-28 02:47

    Try this:

    Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}
    
    0 讨论(0)
  • 2020-11-28 02:47

    To add to @user3303020 answer and output the search results into a file, you can run

    Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat > path_to_results_filename.txt
    

    It may be easier to search for the correct file that way.

    0 讨论(0)
提交回复
热议问题