windowsbatch-file

Find a file and change to that directory in Windows batch file


I am creating a batch file for some of my colleagues with a ton of stuff to run when deploying a new virtual machine.

However, one thing I cannot figure out is how to find a file and change to that directory in a Windows Command Prompt.

I am looking to have the batch file use an installed programs setup.exe file to uninstall itself, but depending on the version installed, depends on where the setup.exe file resides.

So essentially, looking for something like this:

find setup.exe
cd /d c:\path\to\file\setup.exe --uninstall --system-level

Solution

  • @Mohi provided the answer I was looking for with two different options.

    for /F "delims=" %%I in ('dir "C:\setup.exe" /A-D-L /B /S 2^>nul') do cd /D "%%~dpI"

    As you can see in the picture below, I started in the root of C: Starting Point To Code Above

    The command returned output as it searched through all the folders under the starting point (only listing a couple in the below screenshot), then changed to the destination directory at the end: Destination Point To Code Above

    for /F "delims=" %%I in ('dir "C:\Program Files (x86)\Microsoft\Edge\Application\setup.exe" /A-D-L /B /S 2^>nul') do cd /D "%%~dpI"

    This is the command that fit my needs perfectly (Thank you @Mohi): Starting Point To Code Above

    This command does not return any output as it searches through the directories, and changes to the destination when complete: Destination Point To Code Above

    Thank you again to @Mohi for the suggestion and code.