Search for a file with wildcards in the path using Windows command line

10

3

How can I search for files in the Windows command line, using wildcards like this?

C:\Users\*\AppData\Local\*.txt

Sakher

Posted 2012-12-26T16:53:11.570

Reputation: 233

2For what are you trying to use the wildcard search? A command line program, .bat file? For a .bat file scenario, just use nested batch for loops. for /d %%D in (C:\Users\*) do for %%F in (%%~fD\AppData\Local\*.txt) do echo %%~fF As for command line programs, the wildchar handling is up to the authors of that specific program. – David Ruhmann – 2012-12-26T17:11:36.323

I am trying to search in some path with wildcards in the path, not only in the file name, I need some way using existing windows tools (cmd.exe), or powershell, or some external free tool! – Sakher – 2012-12-26T17:18:02.113

Answers

15

Windows PowerShell can do this easily.

Get-ChildItem C:\Users\*\AppData\Local\*.txt

If you want to include subfolders:

Get-ChildItem C:\Users\*\AppData\Local\*.txt -recurse

Regarding Hidden (etc.) files, Powershell's help Get-ChildItem says:

By default, Get-ChildItem gets non-hidden items, but you can use the Directory, File, Hidden, ReadOnly, and System parameters to get only items with these attributes. To create a complex attribute search, use the Attributes parameter. If you use these parameters, Get-ChildItem gets only the items that meet all search conditions, as though the parameters were connected by an AND operator.

aphoria

Posted 2012-12-26T16:53:11.570

Reputation: 898

How to include hidden and system dirs and files? – Sakher – 2012-12-26T17:39:00.770

@MohamedSakherSawan check out the edit I added. – Ƭᴇcʜιᴇ007 – 2012-12-26T18:01:17.273

5

Any good Unix shell for Windows will do this.

For example, here's a screenshot of my Hamilton C shell doing your example (sorry, no matches) and a couple others that do produce matches. The ... wildcard used in the third example matches zero or more directory levels, whatever it takes for the rest of the pattern to match.

Wildcarding using Hamilton C shell on Windows 7 x64

And here's how you might do it with Cygwin bash. A few differences here because Cygwin follows Unix conventions: Paths are usually typed with forward slashes and the backslash is the escape character. Since Unix doesn't understand drive letters, they've introduced a /cydrive/c notation to refer to the C: drive. As on Unix, wildcarding is case-sensitive. And while it supports the usual multiple directory level wildcarding you'd like, it doesn't include the ... I added to my shell. (Instead, you'd probably use find.)

Wildcarding using Cygwin bash on Windows 7 x64

Nicole Hamilton

Posted 2012-12-26T16:53:11.570

Reputation: 8 987

1

In cmd, navigate to C:\Users, and run

dir /s /b | findstr AppData\Local | findstr txt

Explanation: dir /s lists all files recursively, /b displays full path, and within all these child files, use pipeline operator to search both the string 'AppData\Local' and 'txt'.

wile the coyote

Posted 2012-12-26T16:53:11.570

Reputation: 33