Page 1 of 1

Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 02:53
by WBC
Hello!

I'm trying to solve this challenge where I search for Specificly name folder (League of Legends) that is install directory of the game. So full path to the folder is something like C:\Riot Games\League of Legends.

My goal is to find that install folder Riot Games\League of Legends on any disk partition as I know some people use C, D, E, F or whatever.

I've tried following but cmd window just closes in the end without giving me a result:

Code: Select all

@echo on
set "folder="
for %%a in (C D E F) do if not defined folder (
   pushd "%%a:\"
      for /d /r %%b in (Riot*Games\League*of*Legends) do set "folder=%%b"
   popd
)
if defined folder %comspec% /k pushd "%folder%"
I'd appreciate any help on this :)

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 10:21
by aGerman
Wildcards are only supported in the last element of a path. However, globbing means that all folders that meet the pattern will be found and you need an additional comparison to be sure you found the folder you're looking for.
You should be aware that recursively running through the drives will take ages.

Code: Select all

set "folder="
for %%a in (C D E F) do if not defined folder (
  2>nul pushd "%%a:\" && (
    for /d /r %%b in ("Riot Games\League of Legends*") do if /i "%%~nxb"=="League of Legends" set "folder=%%b"
    popd
  )
)
Steffen

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 11:52
by WBC
Thanks!

So if you say that's gonna take ages, do you have any other idea how to solve this? I'm more than happy to learn about creative ways of solving it :)

Also, I ran the command you posted - but it also closes cmd in the end :(
Also 2, why my posts have to be approved all the time?

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 12:21
by aGerman
I mean, what do you expect if you're running through the whole C: drive? Explore your user profile, or the Programs folder, or the Windows folder. The sheer amount of subfolders and depth of the folder trees is overwhelming. There might be a possibility to use the WHERE command if you would specify a file rather than a folder to search for. It seems to be noticeably faster which indicates that it uses a multi-threaded search algorithm internally.

Steffen

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 13:02
by WBC
Would you be able to help me with WHERE version of the code - and check for LeagueClient.exe file, please?

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 13:35
by aGerman

Code: Select all

set "file="
for %%a in (C D E F) do if not defined file (
  for /f "delims=" %%b in ('2^>nul where /r "%%a:\\" "LeagueClient.exe"') do set "file=%%b"
)
echo "%file%"
That's about 10% faster on my machine. However, it still takes ages :lol:

Steffen

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 13:45
by WBC
Yeah.. it takes forever, plus it still closes cmd unexpectedly.

Do you have any other ideas how to find specificly named folder in the PC?

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 14:05
by aGerman
You can parse the output of ROBOCOPY. It's known to use multi-threading and you have the possibility to effectively exclude folders. The example below excludes C:\Windows. But that's all I can think of. And I don't believe that it can be significantly improved. Compare it with how long it takes to find a file in an explorer window if you start searching in the root folder of a drive...

Code: Select all

set "file="
for %%a in (C D E F) do if not defined file (
  for /f "tokens=*" %%i in ('robocopy "%%a:\." " nul" "LeagueClient.exe" /xd "C:\Windows" /l /s /xx /r:1 /w:1 /ns /nc /ndl /np /njh /njs') do set "file=%%i"
)
echo "%file%"
Steffen

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 14:18
by WBC
Actually, that is good solution and it was quick enough. I don't need it to be within MS, this process may take up to few seconds. Also it found correct directory.
Now, lastly.. what if I want to stay within a folder where file was found - aka make it my current one in CMD?

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 14:39
by aGerman

Code: Select all

set "folder="
for %%a in (C D E F) do if not defined folder (
  for /f "tokens=*" %%i in ('robocopy "%%a:\." " nul" "LeagueClient.exe" /xd "C:\Windows" /l /s /xx /r:1 /w:1 /ns /nc /ndl /np /njh /njs') do set "folder=%%~dpi"
)
if defined folder %comspec% /k cd /d "%folder%"
The path in %folder% has a trailing backslash which shouldn't cause any problem in your case.

Steffen

Re: Find specificly named folder and make that as current repo

Posted: 15 Jul 2021 16:17
by WBC
Awesome, I very much appreciate your help! :shock: Thanks!