ChatGPT解决这个技术问题 Extra ChatGPT

Recursive file search using PowerShell

I am searching for a file in all the folders.

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

$File = "V:\Myfolder\**\*.CopyForbuild.bat"

How can I do it in PowerShell?


s
software is fun

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

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force

Seems to have an issue that if it runs into a directory you don't have permission to access, the entire search is aborted because the process exits. Is there a way around that?
Try to set the ErrorAction parameter to Continue or SilentlyContinue (in case its value is not as mentioned).
A shorter way of doing exactly the same thing: cd V:\MyFolder followed by ls -r -inc CopyForBuild.bat
So what the commenters above mean is... ls -r -ea silentlycontinue -fo -inc "filename.txt" | % { $_.fullname }
To add to the comments: The diff between filter and inc is explained here. -fo could be force. % is an alias for foreach-object
P
Peter Mortensen

I use this to find files and then have PowerShell display the entire path of the results:

dir -Path C:\FolderName -Filter FileName.fileExtension -Recurse | %{$_.FullName}

You can always use the wildcard * in the FolderName and/or FileName.fileExtension. For example:

dir -Path C:\Folder* -Filter File*.file* -Recurse | %{$_.FullName}

The above example will search any folder in the C:\ drive beginning with the word Folder. So if you have a folder named FolderFoo and FolderBar PowerShell will show results from both of those folders.

The same goes for the file name and file extension. If you want to search for a file with a certain extension, but don't know the name of the file, you can use:

dir -Path C:\FolderName -Filter *.fileExtension -Recurse | %{$_.FullName}

Or vice versa:

dir -Path C:\FolderName -Filter FileName.* -Recurse | %{$_.FullName}

I find this answer extremely useful because it addresses a particular use case - actually wanting to use the file that you are looking for. The | to the give you the full name of the file is something missing in the other answers.
I have to agree with @Sanity1123 here, if you actually want to use the file, you will need the full path to the file. IMHO this should be the accepted answer.
As a beginner, I am having troubles understanding what is going on after the pipe. I've opened a question about it.
As other say, this is the best answer when you need to store the return value. In my case, I had to find a file to remove it, and this made it way easier.
P
Peter Mortensen

When searching folders where you might get an error based on security (e.g. C:\Users), use the following command:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force

r
rink.attendant.6
Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat

Will also work


And if you want to output results to a file, you can add ` > path_to_filename.txt` at the end of the command.
for current directory Get-ChildItem . -name -recurse *.bat worked for me
A
Asad-ullah Khan

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


Z
ZoogieZork

Try this:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}

You can also use | Where-Object { !$_PSIsContainer } to exclude folders
P
Peter Mortensen

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' }

Shouldn't it be '\ACopyForBuild\.(bat|cmd)\Z'?
G
Gwen Au

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.


H
Hector Abreu

to search for all files with ext "py", starting from / type: dir -r *.py or dir *.py -r