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

Author Topic: batch for find and start batch where it is located  (Read 3591 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    batch for find and start batch where it is located
    « on: April 30, 2017, 04:02:45 PM »
    I have fev batch files in differend folders and it works as they should when I run it manually.
    But I like to create batch to find and start those batch files where they are.
    I tried this script but problem is because it finds batch files and it execute in the same folder as this script, but that is wrong.
    Batch which are find must be executed where they are.
    Can someone help me with this please?

    Code: [Select]
    for /f "delims=|" %%a in ('dir /B /S new_sendmail1.bat') do call "%%a"

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: batch for find and start batch where it is located
      « Reply #1 on: April 30, 2017, 05:28:55 PM »
      I forget to write that batch files which must be executedn by this master batch have relative paths, because of idfferend names of some folders where they are stored, but last folder where they are stored is the same for all batch files, like this. I didn't use full paths because folders like that are all together around 100.
      d:\users\george\files\logs\new_sendmail1.bat
      d:\users\mike\files\logs\new_sendmail1.bat
      d:\users\joe\files\logs\new_sendmail1.bat

      Salmon Trout

      • Guest
      Re: batch for find and start batch where it is located
      « Reply #2 on: May 01, 2017, 12:56:15 AM »
      The CD command and the %cd% variables are useful things to know about. In the FOR command you can use the CD command to make each folder where a batch was found, in turn, the current directory, and thus execute from there. You can get the batch path by using the standard FOR variable modifers ~d and ~p.

      Here is the master.bat file that runs the individual user batch scripts
      I put it in d:\users


      @echo off
      echo Starting master.bat
      Echo I am master.bat
      echo I am stored in:    %~dp0
      echo I am running from: %cd%
      echo Running individual user batches...
      set thisfolder=%cd%
      for /f "delims=|" %%a in ('dir /B /S new_sendmail1.bat') do cd "%%~dpa" & call "%%a"
      cd "%thisfolder%"
      echo Back in master.bat
      echo Back in folder:    %cd%
      echo Ended master.bat
      pause


      This is my dummy new_sendmail1.bat file
      identical copies stored in d:\users\george, d:\users\mike, and d:\users\joe


      @echo off
      echo This is new_sendmail1.bat
      echo I am stored in:    %~dp0
      echo I am running from: %cd%
      echo Here's the proof...
      dir | find "Directory of"
      echo exiting...


      This is the output I got

      Starting master.bat
      I am master.bat
      I am stored in:    D:\users\
      I am running from: D:\users
      Running individual user batches...
      This is new_sendmail1.bat
      I am stored in:    D:\users\george\
      I am running from: D:\users\george
      Here's the proof...
       Directory of D:\users\george
      exiting...
      This is new_sendmail1.bat
      I am stored in:    D:\users\joe\
      I am running from: D:\users\joe
      Here's the proof...
       Directory of D:\users\joe
      exiting...
      This is new_sendmail1.bat
      I am stored in:    D:\users\mike\
      I am running from: D:\users\mike
      Here's the proof...
       Directory of D:\users\mike
      exiting...
      Back in master.bat
      Back in folder:    D:\users
      Ended master.bat
      Press any key to continue . . .






      Blisk

        Topic Starter


        Intermediate

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: batch for find and start batch where it is located
        « Reply #3 on: May 09, 2017, 11:20:55 AM »
        Thank you!