Batch - Open folder with unknown name

2019-09-07 04:28发布

So I want to access this specific path

c:\users\admin\social\profile\KoJumperz\pictures

but my friends name isn't KoJumperz so his path is not the same. His path would be

C:\users\admin\social\profile\JhonSmith\pictures

Is there a way to create a code that will open the first folder in my profile folder?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-07 05:19
  1. Start enumerating the folders and break the loop after the first iteration:

    for /d %%d in (C:\users\admin\social\profile\*) do (set profile=%%d & goto break)
    :break
    echo Using %profile%\pictures
    

    To get the 2nd folder skip 1 entry of dir listing:

    for /f "skip=1 delims=" %%d in ('dir /b /a:d "C:\users\admin\social\profile\*"') do (
        set profile=%%d
        goto break
    )
    :break
    

    Use skip=2 to get the 3rd and so on.

  2. Or find an environment variable that may be set on the user's PC, for example %socialusername% (the exact variable name can be seen by running set in command prompt console):

    echo Using C:\users\admin\social\profile\%socialusername%\pictures
    
查看更多
登录 后发表回答