Search computers drives for a Folder Name and make the folders location a var for manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
burgo855
Posts: 1
Joined: 27 Aug 2016 21:18

Search computers drives for a Folder Name and make the folders location a var for manipulation

#1 Post by burgo855 » 27 Aug 2016 21:42

I recently asked this question on StackOverflow here: http://stackoverflow.com/questions/3917 ... ess-the-wo and "foxidrive" explained i should rather ask here then there and it seems "foxidrive" is on this forum too, nice!.


So to cut my question a bit shorter than before, i want to have a batch file on my desktop and from running it from the desktop, i want to search the computer for a Folder named(Visuals2579211). This folder has an app/exe i made inside it along with log, tmp and txt files.

The problem is i have more than one computer running windows, some are running windows: Visa, Win7 Win8 Win10. I use this application a lot on my machines and there are tasks i want to do to the folder the app is in using a BATCH file, tasks such as searching txt files for strings, exporting data, deleting files etc. The folder name never changes on any of the machines(Visuals2579211), however none of my machines really has this app installed in the same location/drive letter some of the drives arent even called C: etc. It is very tiresome having to manually access these files on all of these machines and change them all the time when i could make a batch to do it for me.

So i need help in finding a way to search the PC from the desktop running BATCH to locate the folder name "Visuals2579211" and after finding the folder, if it exists, store the location/address of the folder as a PATH to later use for file manipulation. "foxidrive" had a very helpful solution:

Code: Select all

@echo off
set "folder="
for %%a in (c e f) do if not defined folder (
   pushd "%%a:\"
      for /d /r %%b in (Visua?s2579211) do set "folder=%%b"
   popd
)
if defined folder %comspec% /k pushd "%folder%"


But instead of making the BATCH make this folder the CD, i would rather make it a PATH/String. Can someone please help me in maybe altering foxidrives solution or provide a better one and if possible please add as much comments as you can to the code as i really would like to understand the code more too.

Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Search computers drives for a Folder Name and make the folders location a var for manipulation

#2 Post by foxidrive » 28 Aug 2016 02:58

Code: Select all

@echo off
set "folder="
for %%a in (c e f) do if not defined folder (
   pushd "%%a:\"
      for /d /r %%b in (Visuals2579211?) do set "folder=%%b"
   popd
)

Welcome to dostips.

The code above sets a variable called folder to the location of the folder. The way you use the variable is the factor that needs to be considered.

1) The batch script can write the folder location to a text file on the desktop and which can be re-used any way you like: and also search for the folder if the file doesn't already exist.

2) The batch script itself can be extended to do the tasks you need to do.

3) The batch script could add the folder-location to a key in the registry rather than a text file.

A whole lot depends on what you need to do with the folder location.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: Search computers drives for a Folder Name and make the folders location a var for manipulation

#3 Post by thefeduke » 29 Aug 2016 00:39

burgo855,
I pruned some code that I was working on recently to apply to your search. This can run from anywhere and will set a variable if the folder is found. The subfolder name default can be changed with an argument and an optional second argument can specify the name of the saved variable. A choice to set the CD is offered if the system supports the CHOICE command. Sorry about the lack of comments. There is some related reading in "Post subject: Better way to find a folder and list contents?" at http://www.dostips.com/forum/viewtopic.php?p=48575#p48575.

Code: Select all

@echo off
SetLocal EnableDelayedExpansion
set "Dir=%CD%"
set "FindFolder=Visuals2579211"
If /i "%~1" NEQ "" set "FindFolder=%~1"
set "FolderName=VisualFolder
If /i "%~2" NEQ "" set "FolderName=%~2"

for %%A in (F I) do (
    Set "Dir=%%A:\"
    for /d  /r "%Dir%" %%a in (*) do (
        If /I "%%~nxa" EQU "%FindFolder%" (
            If Not Defined Folder (
                Set "Folder=%%a"
                If Exist "%comspec:cmd=choice%" (
                    Echo/Select Option A to Accept "!Folder!" `
                    Echo/ - - - - - - - - - as the new current directory.
                    Echo/Select Option S to just Set "!Folder!" `
                    Echo/ - - - - - - - - - as the value for the '%FolderName%' variable.
                    Echo/Select Option Q to reject this folder and Quit.
                    CHOICE /C ASQ /N /M [A,S,Q]?
                    Set "Choice=!errorlevel!"
                ) Else Set "Choice=2"
:NoWhere
                If "!Choice!" EQU "3" Exit /B
                Goto :NoMore
            )
        )
    )
)
:NoMore
If Defined Folder (Endlocal & Rem.return value of Folder
    If "%Choice%" EQU "1" cd /d "%Folder%"
    Set "%FolderName%=%Folder%"
) Else Echo.Could not find the "%FindFolder%" subfolder.
Echo/
If /i "%~2" NEQ "" set "%~2" Else Set VisualFolder
Echo/List of active drive Current Directories:
set "" | findstr "^=[A-Z]:="
Exit /B

John A.

Post Reply