<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: .Bat file to run multiple .Py scripts in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/1155919#M64119</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am looking to do something very similar here. I have 6 individual scripts that I would like to have a .bat file kick off. However, my current .bat file is just executing the first script and not the others. The scripts are NOT dependent on each other, but it would be nice to record any script that fails.&lt;/P&gt;&lt;P&gt;Here is what I have in my .bat file:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Richidt_Corey_0-1647900001476.png" style="width: 679px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36908iFCB632C3322DEED6/image-dimensions/679x148?v=v2" width="679" height="148" role="button" title="Richidt_Corey_0-1647900001476.png" alt="Richidt_Corey_0-1647900001476.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 21 Mar 2022 22:01:23 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-03-21T22:01:23Z</dc:date>
    <item>
      <title>.Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325319#M25302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am looking for info on how to properly write a .bat file that will run multiple python scripts sequentially.&lt;/P&gt;&lt;P&gt;If one of those scripts fails then I need the process to stop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BUT i need then to run in sequence and wait before starting the next.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think this is close but nothing in there that aborts if fails.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;@echo off &lt;BR /&gt;start /wait&amp;nbsp;D://PythonScripts/Script1&lt;BR /&gt;&lt;SPAN&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;D://PythonScripts/Script2&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;D://PythonScripts/Script3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;D://PythonScripts/Script4&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:01:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325319#M25302</guid>
      <dc:creator>jaykapalczynski</dc:creator>
      <dc:date>2017-04-19T15:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325320#M25303</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Looks like you can do something with &lt;A href="http://stackoverflow.com/questions/1491796/get-the-exit-code-for-python-program"&gt;sys.exit()&lt;/A&gt; or &lt;A href="http://stackoverflow.com/questions/1013246/call-python-from-bat-file-and-get-return-code"&gt;exit()&lt;/A&gt; to return a failure to the command prompt, (exit code 1), and then add some logic within the .bat to &lt;A href="http://stackoverflow.com/questions/734598/how-do-i-make-a-batch-file-terminate-upon-encountering-an-error"&gt;check the exit code.&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:47:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325320#M25303</guid>
      <dc:creator>JonathanQuinn</dc:creator>
      <dc:date>2017-04-19T15:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325321#M25304</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In my organization's bat file, we check for the existence of a fail log that is generated in any of the python scripts that are part of the bat file. &amp;nbsp;If the fail log does exist (It is renamed at the beginning of each run) at any point between python calls, then the bat file exits gracefully without running the remainder of the python scripts.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So you would need to write to an error log in your python code and then check for this file in your bat file&lt;/P&gt;&lt;P&gt;if exist fail.log (goto :BAIL) else echo fail.log not found&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;:BAIL&lt;/P&gt;&lt;P&gt;goto :EXIT&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;:EXIT&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 15:48:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325321#M25304</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2017-04-19T15:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325322#M25305</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If I run as below will this work? &amp;nbsp;Just wont have the fail safe of whether or not one of the scripts failed?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff;"&gt;@echo off &lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff;"&gt;start /wait&amp;nbsp;D://PythonScripts/Script1&lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;D://PythonScripts/Script2&lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;D://PythonScripts/Script3&lt;/SPAN&gt;&lt;BR style="background-color: #ffffff;" /&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;start /wait&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="background-color: #ffffff; border: 0px;"&gt;D://PythonScripts/Script4&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:45:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325322#M25305</guid>
      <dc:creator>jaykapalczynski</dc:creator>
      <dc:date>2017-04-19T16:45:03Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325323#M25306</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;With your current bat file, there is no way for it to stop running a successive&amp;nbsp;python script if it fails on the current python script because it will just exit the current python script with an error and move on to the next python script. &amp;nbsp;You need to explicitly tell the bat file to not run successive&amp;nbsp;python scripts if the current script fails and that is what the fail log is for.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now if python scripts are not dependent on each other for working properly, then your current setup could work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my scenario, the python scripts are dependent on each other, so if 1 python script fails I don't want to run the successive scripts.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:52:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325323#M25306</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2017-04-19T16:52:45Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325324#M25307</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yea that part I understand...just want to confirm...just gonna give this a test...and add the Fail&amp;nbsp;safe another day.&lt;/P&gt;&lt;P&gt;Thanks for your thoughts.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any examples of the code to do the testing? &amp;nbsp;Creating the log file and then testing if it exists? &amp;nbsp;I assume I do this in the python script and not in the Bat file.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 16:55:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325324#M25307</guid>
      <dc:creator>jaykapalczynski</dc:creator>
      <dc:date>2017-04-19T16:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325325#M25308</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I create a list to store the errors in near beginning of python script&lt;/P&gt;&lt;P&gt;ErrList = []&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the except portion of your python scripts (trapping the error) you need to put in the following code to trap the error messages and write them to a log file:&lt;/P&gt;&lt;P&gt;except Exception as e:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;ErrList.append(e.message)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FailLog = open("path to log file"/FailLogFile, 'w');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FailLog.write("The following error(s) has been encountered:")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;FailLog.write(ErrList[0])&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;FailLog.close()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;In the bat file I check for existence of a FailLog file by name as I rename a previous fail log at the beginning of the bat file to ensure no FailLog file by that specific name exists at the start of the calls to the python scripts.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if exist FailLog (goto :BAIL)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You could probably simplify this part and just have if&amp;nbsp;exist FailLog (goto :EXIT)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;:EXIT&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Apr 2017 16:14:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325325#M25308</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2017-04-20T16:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325326#M25309</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bat can handle this using shell conditional execution, without clumsy if statements on %errorlevel%.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;amp;&amp;amp; means to execute the next command as long as there is no error returned from the previously run tool. PS has a similar operator "-and"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;@echo off 
pushd D:\PythonScripts
Script1.py &amp;amp;&amp;amp; Script2.py &amp;amp;&amp;amp; Script3.py &amp;amp;&amp;amp; Script4.py‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;A class="link-titled" href="http://www.robvanderwoude.com/condexec.php" title="http://www.robvanderwoude.com/condexec.php" rel="nofollow noopener noreferrer" target="_blank"&gt;Batch files - Conditional execution&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="http://stackoverflow.com/questions/563600/can-i-get-to-work-in-powershell" title="http://stackoverflow.com/questions/563600/can-i-get-to-work-in-powershell" rel="nofollow noopener noreferrer" target="_blank"&gt;syntax - Can I get &amp;amp;&amp;amp; to work in Powershell? - Stack Overflow&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This requires that your python script will return error condition to the shell, this will happen if python fails with an error dump, but if you are trapping with try/except you will have to leave your script with sys.exit(1) to return an error condition to the shell (sys.exit() won't do it, you need to supply the 1).&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:25:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/325326#M25309</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T15:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/1155919#M64119</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am looking to do something very similar here. I have 6 individual scripts that I would like to have a .bat file kick off. However, my current .bat file is just executing the first script and not the others. The scripts are NOT dependent on each other, but it would be nice to record any script that fails.&lt;/P&gt;&lt;P&gt;Here is what I have in my .bat file:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Richidt_Corey_0-1647900001476.png" style="width: 679px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/36908iFCB632C3322DEED6/image-dimensions/679x148?v=v2" width="679" height="148" role="button" title="Richidt_Corey_0-1647900001476.png" alt="Richidt_Corey_0-1647900001476.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Mar 2022 22:01:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/1155919#M64119</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-03-21T22:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: .Bat file to run multiple .Py scripts</title>
      <link>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/1158002#M64163</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;SPAN&gt;jaykapalczynski any thoughts on this? Wondering if you found a solution that worked for your original post? Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2022 19:34:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/bat-file-to-run-multiple-py-scripts/m-p/1158002#M64163</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-03-25T19:34:07Z</dc:date>
    </item>
  </channel>
</rss>

