How do I navigate to a subdirectory named with a s

2019-07-29 14:59发布

问题:

Suppose I need to navigate to a sub-folder using the wildcard character *. What is the command in cmd/batch script?

Example:

My current directory is c:\Users\Test

I have only one sub-folder as 3

I want to navigate to c:\Users\Test\3

cd * //doesn't work

cd *. * //doesn't work

What is the command to navigate?

回答1:

You could choose one random/the first directory found by a FOR-Loop

for /d %%A in (*) do cd %%A


回答2:

Are you sure? This is working in my environment:

PS C:\users\myuser\test> ls


    Directory: C:\users\myuser\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         9/18/2013   9:56 AM            3


PS C:\users\myuser\test> cd *
PS C:\users\myuser\test\3>

This would not work if you try to use it in a directory that has multiple sub-directories. For example, my "Documents" folder has multiple subdirectories:

PS C:\users\myUser\documents> cd *
Set-Location : Cannot set the location because path '*' resolved to multiple
containers. You can only the set location to a single container at a time.

Here's what happens when I try to use the wildcard to match for certain incomplete phrases that are specific enough to return a single match:

PS C:\users\myUser\documents> cd boo*
PS C:\users\myUser\documents\Books>
PS C:\users\myUser\documents> cd *ooks
PS C:\users\myUser\documents\Books>