3

Despite it is not documented pushd accepts wildcards (when command extensions are turned on). But does not work as I expect and seems buggy. When a wildcard expression is passed pushd gets all files(!) and folders that apply the pattern in alphabetical order and tries to enter the first item in the list - without checking if it is folder or file:

C:>break>a1

C:>md a2

C:>pushd "a*"
The directory name is invalid.

C:>md b1

C:>pushd "b>"

C:\b1>

Is there way to force pushd to enter the first directory that is like the passed the wildcard expression.Things like b*\ or b*\nul does not work. Looks like the only way is to list directories with DIR command, to get the first one and then pass it to the pushd:

@echo off

set "mask=b?"

for /f "tokens=*" %%# in ('dir /b /a:d /o:-n "%mask%"') do @set the_dir=%%#

pushd %the_dir%

But this does not look convenient for usage from command line.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 3
    I'm pretty sure this "feature" was accidental. I doubt there's any way to make it work the way you want. This functionality is deep in the parsing guts of cmd.exe, and MSFT won't want to go into the legacy code to fix something like that. – Ryan Bemrose Feb 01 '16 at 10:29
  • 1
    [Pushd](https://technet.microsoft.com/en-us/library/bb490978.aspx) does not support wildcards. You are essentially asking, how to use a command outside its specification. The answer to that is simple: Don't. – IInspectable Feb 01 '16 at 10:55
  • 1
    @IInspectable - the most powerful batch techniques use things not covered by MS documentation (you can consider them as 'hacks'). I don't see why can't abuse `pushd` if it can be useful. – npocmaka Feb 01 '16 at 11:02
  • 1
    I don't consider a tool that comes with a disclaimer along the lines of *"Can stop working at any time - we don't know when"* particularly useful. – IInspectable Feb 01 '16 at 11:08
  • @npocmaka: it could be argued that the most powerful batch technique is to forget batch and use a real language if you need to do anything more complex than launching a few executables in sequence. – Matteo Italia Feb 01 '16 at 12:54
  • 2
    Me: "Doctor, it hurts when I use wildcards with PUSHD." Doctor: "Don't do that." – lit Feb 01 '16 at 15:43
  • 3
    Rest of the world: "Don't use Batch files! Use any _real programming language_". We Batch file adventurers: "You have not idea of the huge amount of fun you are loosing that you'll never get from any other better defined language; for example: [macros with parameters](http://www.dostips.com/forum/viewtopic.php?f=3&t=4083), [output color text](http://www.dostips.com/forum/viewtopic.php?f=3&t=4881), animated games like [Snake](http://www.dostips.com/forum/viewtopic.php?f=3&t=4741), [Tetris](http://www.dostips.com/forum/viewtopic.php?f=3&t=6812), and a long et cetera plus all future discoveries". – Aacini Feb 01 '16 at 21:02
  • The `for /F` solution - or rather, `for /D` - is fine for batch files. If you're on the command line, just press TAB and the wildcard will be expanded to the first matching directory. If that's not the one you want, press TAB again and you'll get the next matching directory. (For built-in commands, tab completion knows whether to look for a file or directory.) – Harry Johnston Feb 02 '16 at 01:08

1 Answers1

3

Interesting! I just tested cd command and it works in the same way. There is no way to "sort" names so folder names comes before file names, but if all your folders have not extensions and all your files does have they, you may use the undocumented "<" wild-card to do so:

break > a1.txt
md a2
pushd "a<"
Aacini
  • 65,180
  • 12
  • 72
  • 108