Extend script to find 2 more files and execute all

Danny Kon
Danny Kon used Ask the Experts™
on
Can somebody help me to change the underneath CMD script from oBdA to find 3 programs and run 2nd  command when the first is finished and the 3rd when the 2nd is finished (a 10 second delay between commands is also good)

Thanks Danny
_____________________________________________
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
      set /p FileName=Give the filename:
      if "!FileName!"=="" goto :eof
) else (
      set FileName=%~1
)
call :Where Folder "%FileName%"
if "%Folder%"=="" (
      echo %FileName% not found.
      exit /b 1
)
echo Folder: %Folder%

REM ========== Only function after this line! ==========
goto :eof
:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa&goto :eof)
set %~1=
goto :eof
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
The script only searches for the program you supplied, and displays the folder it is in. It does not execute anything.
To modify it to search and execute N programs, I recommend to use following script. But now you will have to provide all programs to execute in the commandline, like with (calling the script SearchAndExec.cmd):
searchAndExec Prog1.exe Prog2.exe Prog3.exe

Open in new window

@echo off
setlocal enabledelayedexpansion
if "%~1"=="" echo Please supply executable names with the command& pause & exit /b 1

for %%F in (%*) do (
  call :Where FullPath "%%~F"
  if [%FullPath%]==[] (
      echo %%F not found.
      exit /b 1
  )
  call %FullPath%
)
exit /b

REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa& exit /b)
set %~1=
exit /b 1

Open in new window

Author

Commented:
Qlemo,

You are right but i changed the script from oDbA where it says
echo Folder: %Folder%
to
"%Folder%%FileName%" and then it will execute the program
It would be much appriciated if it will work in the same way so it will first ask for the 3 programs and then excecute 1, wait execute 2 wait and execute 3

Thanks

Danny
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Very inconvenient to get asked for 3 programs! Why would you like to do that? One single typo, and it does not work at all.
I would recommend to allow for a single prompt asking for all programs to execute.

Further, do you want to have all programs checked first, so executing only starts if all have been found?
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Author

Commented:
Qlemo
 
It doesnt has to do ask for the program but is it possible that i can change this in the batch
as an example i want to find the file sqldump.exe
sometimes it will be in C:\Program Files\MySQL\MySQL Server 5.7\bin
but on other SQL version or in a other computer it will be in a different directory  

something like:
___________
@echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
Program 1
Program 2
program 3
      if "!FileName!"=="" goto :eof
) else (
      set FileName=%~1
)
call :Where Folder "%FileName%"
if "%Folder%"=="" (
      echo %FileName% not found.
      exit /b 1
)
execute 1
wait
execute 2
wait
%mysqldump% -u root -ppassword databasename > %databasename%

REM ========== Only function after this line! ==========
goto :eof
:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa&goto :eof)
set %~1=
goto :eof
__________
Thanks Danny

Author

Commented:
Qlemo,

I overlooked your last comment yes it has to say something like "this script will not work" if i doesnt find all the programs

Danny
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
This allows for an arbitrary count of progs to be provided in the first FOR. Only if all of them are found execution starts in sequence as given.
@echo off
setlocal enabledelayedexpansion

for %%F in (Prog1.exe Prog2.exe Prog3.exe) do (
  call :Where FullPath "%%~F"
  if [!FullPath!]==[] (
      echo %%F not found -- Abort
      exit /b 1
  )
  set FullPaths=!FullPaths! "!FullPath!"
)
for %%F in (%FullPaths%) call %%F
exit /b

REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa& exit /b 0)
set %~1=
exit /b 1

Open in new window

Author

Commented:
Qlemo,

I dont understand how i can use this in my question  
As you see in my example i need to give additional parameters to the programs, how i can do this in your solution?

for %%F in (Prog1.exe Prog2.exe mysqldump.exe)

Where i can put underneath ?

Prog 1
wait
Prog 2
wait
%mysqldump% -u root -ppassword databasename > databasename

Thanks for your help

Danny
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That is more complex, and you should have told from start. It now results in a more cumbersome script:
@echo off
setlocal enabledelayedexpansion

call :Where FullPath1 Prog1.exe && ^
call :Where FullPath2 Prog2.exe && ^
call :Where FullPath3 mysqldump.exe && (
  !FullPath1!
  !FullPath2!
  !FullPath3! -u root -ppassword databasename > databasename
  exit /b 0
)
echo *** ABORT ***
exit /b 1


REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa& exit /b 0)
echo '*** %~2 not found anyhwere'
set %~1=
exit /b 1

Open in new window

Feel free to name FullPath1 etc. different and more specific. Those names are arbitrary.

Author

Commented:
Qlemo,

You are right i had to be more clear in the question.
I used wordpad.exe mspaint.exe and calc.exe for testing the script,  it doesnt work as expected wordpad is not finding the file

Danny

-----test file--------
@echo off
setlocal enabledelayedexpansion

call :Where FullPath1 wordpad.exe && ^
call :Where FullPath2 mspaint.exe && ^
call :Where FullPath3 calc.exe && (
 
!FullPath1!wordpad.exe "C:\Users\POS\Desktop\FILE.TXT"
!FullPath2!mspaint.exe
!FullPath3!calc.exe
 
exit /b 0
)
echo *** ABORT ***
exit /b 1


REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa& exit /b 0)
echo '*** %~2 not found anyhwere'
set %~1=
exit /b 1
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
No, you do NOT repeat the application names in the actual call. You need those only in the :Where check.
@echo off
setlocal enabledelayedexpansion

call :Where FullPath1 wordpad.exe && ^
call :Where FullPath2 mspaint.exe && ^
call :Where FullPath3 calc.exe && (
  !FullPath1! "C:\Users\POS\Desktop\FILE.TXT"
  !FullPath2!
  !FullPath3!
  exit /b 0
)
echo *** ABORT ***
exit /b 1


REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~dp$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~dpa& exit /b 0)
echo '*** %~2 not found anyhwere'
set %~1=
exit /b 1

Open in new window

Author

Commented:
Qlemo

Then i only get the path
'C:\Program Files\Windows NT\Accessories\' is not recognized as an internal or external command,

Danny
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Sorry, my fault. Forgot to adapt that.
@echo off
setlocal enabledelayedexpansion

call :Where FullPath1 wordpad.exe && ^
call :Where FullPath2 mspaint.exe && ^
call :Where FullPath3 calc.exe && (
  !FullPath1! "C:\Users\POS\Desktop\FILE.TXT"
  !FullPath2!
  !FullPath3!
  exit /b 0
)
echo *** ABORT ***
exit /b 1


REM ========== Only function after this line! ==========

:Where <Variable> <File>
set %~1=%~F$PATH:2
if not "!%~1!"=="" goto :eof
echo '%~2' not found in path; searching C: ...
for /f "delims=" %%a in ('dir /a:-d /s /b "C:\%~2"') do (set %~1=%%~Fa& exit /b 0)
echo '*** %~2 not found anyhwere'
set %~1=
exit /b 1

Open in new window

Author

Commented:
Qlemo

Sorry that this give you so much trouble,
It start wordpad but it does not find the file 100% sure that the file is there very strange

Danny

Author

Commented:
Qlemo

I think i found the problem, if i put the
 !FullPath1!
  !FullPath2!
  !FullPath3!

like this it will start also the file
 
 "!FullPath1!"
  "!FullPath2!"
  "!FullPath3!"

Thanks so much for your help

Danny

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial