Find specificly named folder and make that as current repo

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
WBC
Posts: 6
Joined: 15 Jul 2021 02:38

Find specificly named folder and make that as current repo

#1 Post by WBC » 15 Jul 2021 02:53

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 :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 15 Jul 2021 10:21

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

WBC
Posts: 6
Joined: 15 Jul 2021 02:38

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

#3 Post by WBC » 15 Jul 2021 11:52

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#4 Post by aGerman » 15 Jul 2021 12:21

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

WBC
Posts: 6
Joined: 15 Jul 2021 02:38

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

#5 Post by WBC » 15 Jul 2021 13:02

Would you be able to help me with WHERE version of the code - and check for LeagueClient.exe file, please?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#6 Post by aGerman » 15 Jul 2021 13:35

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

WBC
Posts: 6
Joined: 15 Jul 2021 02:38

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

#7 Post by WBC » 15 Jul 2021 13:45

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 15 Jul 2021 14:05

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

WBC
Posts: 6
Joined: 15 Jul 2021 02:38

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

#9 Post by WBC » 15 Jul 2021 14:18

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#10 Post by aGerman » 15 Jul 2021 14:39

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

WBC
Posts: 6
Joined: 15 Jul 2021 02:38

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

#11 Post by WBC » 15 Jul 2021 16:17

Awesome, I very much appreciate your help! :shock: Thanks!

Post Reply