Link to home
Start Free TrialLog in
Avatar of Danny Kon
Danny KonFlag for Netherlands

asked on

Extend script to find 2 more files and execute all

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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

Avatar of Danny Kon

ASKER

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
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?
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
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
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

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
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.
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
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

Qlemo

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

Danny
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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