Home » Questions » Computers [ Ask a new question ]

Windows equivalent of ls * (asterisk) directory wildcarding?

Windows equivalent of ls * (asterisk) directory wildcarding?

Another question from an long-time Unix/Linux user who finds himself in a Windows world. First let me explain exactly what I'm trying to do. I'm using the Windows cmd.exe shell, and I want to list all directories below the current directory that contain a bin\Debug hierarchy, and determine if they contain any files (e.g.: trying to figure out if my NAnt clean target is working properly). My hierarchy might look something like this:

Asked by: Guest | Views: 56
Total answers/comments: 3
Guest [Entry]

"Something like this should do what you want. At the root directory of the project type:

c:\path\to\slndir>dir *.dll *.exe /s /b | findstr /i ""bin\debug""

Dir arguments: /s - recursive, /b - ""bare"", no extra info
findstr arguments: /i - ignore case

In general, 'help command' will show help for the shell commands. If that doesn't work, then'command /?' will generally show help."
Guest [Entry]

"@for /D /r %P in (.) do @for %Q in (""%~fP\bin\Debug\*"") do @echo %~ftzaQ

Not nice, but should work. You may also put into a .cmd file (then replace every single % into double %%). Or define a macro using doskey (yes, it still exists).

Ensure that cmd extensions are enabled.

EDIT: How it works:

The first for loop iterates through all subdirectories (/D and /r switch) iteratively. %~fP expands to the full path of that directory. Then, it appends \bin\Debug* to that path. The second for loop enumerates all files that match. %~ftzaQ is expanded to every file match and printed to the screen by echo. The strange %~ftzaQ formats it like a dir output. The @ character avoids that the commands itself are printed on the screen.

For more, see

Guest [Entry]

"In the Windows world cmd.exe does not expand wildcards - they get passed as-is to the program in its command line or argv array. Therefore each program parses and expands them differently, and some of the more advanced uses (like you're looking for) are generally not supported.

timgilbert's suggestion of using Cygwin is a good one, also the GNUWin32 package might have something that'll help you out."