Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Search drive for exe and run if found  (Read 2849 times)

0 Members and 1 Guest are viewing this topic.

mickster9

    Topic Starter


    Rookie

    Search drive for exe and run if found
    « on: November 12, 2007, 03:44:17 PM »
    Hi I was wondering how to go about making a batch file to search the C: for a particular .exe file, and to start it if it is found.
    It needs to work nomatter what directory the .exe is in.
    Any ideas?

    diablo416

    • Guest
    Re: Search drive for exe and run if found
    « Reply #1 on: November 13, 2007, 07:08:14 AM »
    @echo off
    set av1=%cd:~0,1%
    for /f "tokens=1*" %%a in ('dir %av1%:\yourexefile.exe /a /b /s') do start %%a



    first line turns echo off
    second line sets the drive letter as %av1%
    third line searches from drive %av1%:\ for yourexefile.exe , /b for bare format /s to include all sub directorys, so if you start in root "%av1%:\" it searches the entire drive , /a includes all hidden files & folders

    mickster9

      Topic Starter


      Rookie

      Re: Search drive for exe and run if found
      « Reply #2 on: November 13, 2007, 10:57:50 AM »
      Works great apart from one thing. If the directory has a space in it eg. 'Program Files' it comes up with an error saying Windows cannot find 'C:\Program'.
      Is there a way to get it to work for directories with spaces by putting quotes around the file path or something?

      tommy gusack



        Intermediate
        Re: Search drive for exe and run if found
        « Reply #3 on: November 13, 2007, 10:59:27 AM »
        Diablo your video scare me :D
        Theres always.....always something to grab a hold on.

        yperron



          Rookie
          Re: Search drive for exe and run if found
          « Reply #4 on: November 13, 2007, 11:17:37 AM »
          you have to add double quotes:

          start "%%a"

          yp
          yp

          diablo416

          • Guest
          Re: Search drive for exe and run if found
          « Reply #5 on: November 13, 2007, 01:37:05 PM »
          using double qutoes in start will make it open cmd.exe again with title "%%a"

          use this instead

          for /f "tokens=1*" %%a in ('findstr /I %au1a% yourfile.exe') do RunDll32.exe shell32.dll,ShellExec_RunDLL "%%a"

          mickster9

            Topic Starter


            Rookie

            Re: Search drive for exe and run if found
            « Reply #6 on: November 13, 2007, 02:40:52 PM »
            Cheers

            for /f "tokens=1*" %%a in ('dir %av1%:\yourexefile.exe /a /b /s') do RunDll32.exe shell32.dll,ShellExec_RunDLL "%%a"

            Works for me