npocmaka
npocmaka

Reputation: 57252

Is there a way to workaround this pushd bug?

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.

Upvotes: 3

Views: 477

Answers (1)

Aacini
Aacini

Reputation: 67216

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<"

Upvotes: 3

Related Questions