<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Issues Running Python Scripts in Batch File]]></title><description><![CDATA[<p dir="auto">Hello Community,</p>
<p dir="auto">I am currently facing some challenges running Python scripts through a batch file, and I would greatly appreciate your assistance in resolving the issue. Here are the details:</p>
<p dir="auto"><strong>Problem Description:</strong><br />
I have a batch file that I believe is supposed to run multiple Python scripts on files that I drag and drop on to the batch file. However, I am encountering difficulties, and I suspect there might be an issue with the way it’s set up.</p>
<p dir="auto"><strong>Batch File Content:</strong></p>
<pre><code>@echo off

rem List of Python scripts to run
set "scripts=BrakesDelayTimes.py CriticalDamageThresholdTimes.py DamageCapacityTimes.py DamagedConsumptionModifierTimes.py DamagedMaxTorqueMultiplierTimes.py DamagedMinTorqueMultiplierTimes.py EngineResponsivenessTimes.py FuelConsumptionTimes.py TorqueTimes.py"

rem Path to your Python executable
set "python_executable=C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.496.0_x64__qbz5n2kfra8p0\python3.12.exe"

rem Loop through the scripts and run them
for %%i in (%scripts%) do (
    echo Running script: %%i
    "%python_executable%" "%%i"
)
</code></pre>
<p dir="auto"><strong>Additional Information:</strong></p>
<ul>
<li>Example of one of the python scripts</li>
</ul>
<pre><code># encoding=utf-8
from Npp import editor

def multiply_by_2(m):
    return 'BrakesDelay="' + str(float(m.group(1))*1.08) + '"'
 

editor.rereplace(r'BrakesDelay="(\d*\.?\d*.\d+)"', multiply_by_2)
</code></pre>
<ul>
<li>Operating System: [Windows 11]</li>
<li>Python Version: [Python 3.12]</li>
</ul>
<p dir="auto"><strong>Request for Assistance:</strong><br />
If anyone has experience with running Python scripts through a batch file, could you please review my batch file and paste the new batch file. Thank you in advance for your time and assistance!</p>
<p dir="auto">Best regards,<br />
ImJohnFolks</p>
]]></description><link>https://community.notepad-plus-plus.org/topic/25429/issues-running-python-scripts-in-batch-file</link><generator>RSS for Node</generator><lastBuildDate>Mon, 15 Jun 2026 13:53:34 GMT</lastBuildDate><atom:link href="https://community.notepad-plus-plus.org/topic/25429.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Jan 2024 07:16:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Wed, 31 Jan 2024 14:27:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> said in <a href="/post/92522">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto">With your help, (its took me around 4 hours of just F’ing around) I’ve managed to make some progress on my PythonScript within Notepad++.</p>
</blockquote>
<p dir="auto">If you mean you spent a total of 4 hours on all the aspects of the script, including the research and starting from the example, and incorporating our feedback : that’s actually pretty good, considering how new you are to Python and coding in general – congratulations, such effort is to be commended.  (Put it in perspective: universities offer semester-long “introduction to Python” classes. In such a class, you might not even have defined one function yet in the first week, let alone the first four hours of class and homework, because they’re still working on variables and for-loops for the first few classes.  So you’re doing great)</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92529</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92529</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Wed, 31 Jan 2024 14:27:18 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Wed, 31 Jan 2024 06:41:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> said in <a href="/post/92514">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> said in Issues Running Python Scripts in Batch File:</p>
<p dir="auto">I am really new to Python, scripting and Notepad ++ and I am having difficulty understanding the concept you mentioned in your previous response. Specifically, you suggested defining my own function that calls ‘editor.rereplace’ and then using that function in place of the ‘printFilename’ called within the ‘forEachFile(…)’.</p>
<p dir="auto">This is not a Python help forum. Defining a function is very much generic Python programming.</p>
<p dir="auto">But in brief, it’s the def XXXXX(arg):<br />
So you would need to do something like the following (untested) to define your function:</p>
<p dir="auto">def doMyReplacement():<br />
editor.rereplace(r’BrakesDelay=“(\d*.?\d*.\d+)”', multiply_by_2)<br />
Then the forEachFile line should be:</p>
<p dir="auto">forEachFile(doMyReplacement)<br />
If you are going to continue to develop in PythonScript, you will need to learn the fundamentals of Python – and we cannot teach those to you. You will have to find some other tutorial on the web to learn the essentials of Python.</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a></p>
<p dir="auto">I appreciate your guidance and clarification. Thank you for sharing the snippet for defining a function in Python, and I kind of understand your point about the forum’s focus and apologies for any confusion caused by my assumption about the nature of this forum. I did notice the “Help wanted” tag and thought it implied assistance could be sought here. Regardless, I appreciate your feedback.</p>
<p dir="auto">With your help, (its took me around 4 hours of just F’ing around) I’ve managed to make some progress on my PythonScript within Notepad++. For the benefit of others facing a similar issue, here’s the code that worked for me:</p>
<pre><code># encoding=utf-8
from Npp import editor, notepad

def multiply_by_2(m):
    return 'BrakesDelay="' + str(float(m.group(1))*1.08) + '"'

# Define the replacement function
def doMyReplacement():
    editor.rereplace(r'BrakesDelay="(\d*\.?\d*.\d+)"', multiply_by_2)

def forEachFile(doMyReplacement):
    for file in notepad.getFiles():
        notepad.activateBufferID(file[1])
        doMyReplacement()

def printFilename():
    print(notepad.getCurrentFilename())

# Store the current buffer id
currentBufferID = notepad.getCurrentBufferID()

# Run our printFilename function on each open file
forEachFile(doMyReplacement)

# Restore the current buffer from the stored buffer id
notepad.activateBufferID(currentBufferID)
</code></pre>
<p dir="auto">Thanks again for your help and clear guidance.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92522</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92522</guid><dc:creator><![CDATA[ImJohnFolks]]></dc:creator><pubDate>Wed, 31 Jan 2024 06:41:18 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Tue, 30 Jan 2024 22:13:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> said in <a href="/post/92512">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto">I am really new to Python, scripting and Notepad ++ and I am having difficulty understanding the concept you mentioned in your previous response. Specifically, you suggested defining my own function that calls <strong>‘editor.rereplace’</strong> and then using that function in place of the <strong>‘printFilename’</strong> called within the <strong>‘forEachFile(…)’</strong>.</p>
</blockquote>
<p dir="auto">This is not a Python help forum.  Defining a function is very much generic Python programming.</p>
<p dir="auto">But in brief, it’s the <code>def XXXXX(arg):</code><br />
So you would need to do something like the following (untested) to define <em>your</em> function:</p>
<pre><code>def doMyReplacement():
    editor.rereplace(r'BrakesDelay="(\d*\.?\d*.\d+)"', multiply_by_2)
</code></pre>
<p dir="auto">Then the <code>forEachFile</code> line should be:</p>
<pre><code>forEachFile(doMyReplacement)
</code></pre>
<p dir="auto">If you are going to continue to develop in PythonScript, you will need to learn the fundamentals of Python – and we cannot teach those to you.  You will have to find some other tutorial on the web to learn the essentials of Python.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92514</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92514</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 30 Jan 2024 22:13:03 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Tue, 30 Jan 2024 19:57:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a> said in <a href="/post/92509">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> ,</p>
<p dir="auto">The example you were copying from was just using <code>printFilename()</code> as an example of something you could do on all files: it was a dummy/placeholder, not the end goal.</p>
<p dir="auto">What you really need to do is to define your own function, which calls the <code>editor.rereplace</code> .  You would then call <em>that</em> function instead of calling <code>printFilename</code> in the <code>forEachFile(...)</code> call.</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/peterjones" aria-label="Profile: PeterJones">@<bdi>PeterJones</bdi></a>,</p>
<p dir="auto">I appreciate your assistance thus far.</p>
<p dir="auto">I am really new to Python, scripting and Notepad ++ and I am having difficulty understanding the concept you mentioned in your previous response. Specifically, you suggested defining my own function that calls <strong>‘editor.rereplace’</strong> and then using that function in place of the <strong>‘printFilename’</strong> called within the <strong>‘forEachFile(…)’</strong>.</p>
<p dir="auto">Below is my attempt at implementing what I believe you were suggesting:</p>
<pre><code># encoding=utf-8
from Npp import editor

def forEachFile(func):
    for file in notepad.getFiles():
        notepad.activateBufferID(file[1])
        func()

def multiply_by_2(m):
    return 'BrakesDelay="' + str(float(m.group(1))*1.08) + '"'

def printFilename():
    print(notepad.getCurrentFilename())

# Store the current buffer id
currentBufferID = notepad.getCurrentBufferID()

# Run our printFilename function on each open file
forEachFile(editor.rereplace(r'BrakesDelay="(\d*\.?\d*.\d+)"', multiply_by_2))

# Restore the current buffer from the stored buffer id
notepad.activateBufferID(currentBufferID)
</code></pre>
<p dir="auto">It seems like I am missing something, could you kindly provide further clarification or perhaps an illustrative example to help me better understand the process?</p>
<p dir="auto">Thank you in advance for your time and assistance.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92512</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92512</guid><dc:creator><![CDATA[ImJohnFolks]]></dc:creator><pubDate>Tue, 30 Jan 2024 19:57:26 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Tue, 30 Jan 2024 19:24:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> ,</p>
<p dir="auto">The example you were copying from was just using <code>printFilename()</code> as an example of something you could do on all files: it was a dummy/placeholder, not the end goal.</p>
<p dir="auto">What you really need to do is to define your own function, which calls the <code>editor.rereplace</code> .  You would then call <em>that</em> function instead of calling <code>printFilename</code> in the <code>forEachFile(...)</code> call.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92509</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92509</guid><dc:creator><![CDATA[PeterJones]]></dc:creator><pubDate>Tue, 30 Jan 2024 19:24:51 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Tue, 30 Jan 2024 19:09:45 GMT]]></title><description><![CDATA[<p dir="auto">Okay, for the past few hours, I’ve been unsuccessful in trying to get this working in the default Python environment.</p>
<p dir="auto">So, I attempted to find a solution within Notepad++ and came across this post [Hmm it appears I can’t post links]. However, the code in that post isn’t working as intended either.</p>
<p dir="auto"><strong>Here’s the code I’m using from that post:</strong></p>
<pre><code>from Npp import *

def forEachFile(func):
    for file in notepad.getFiles():
        notepad.activateBufferID(file[1])
        func()

def printFilename():
    print(notepad.getCurrentFilename())

# store the current buffer id
currentBufferID = notepad.getCurrentBufferID()
# run our printFilename function on each open file
forEachFile(printFilename)
# restore the current buffer from the stored buffer id.
notepad.activateBufferID(currentBufferID)
</code></pre>
<p dir="auto">When I place this in my Script py, I get the following output in the Python console:</p>
<pre><code>C:\Program Files\Notepad++\plugins\PythonScript\scripts\SnowRunnerPythonScripts\Engines\EnginesBrakesDelayTimes.py
C:\Users\johnr\Desktop\[media]\classes\engines\e_us_scout_old.xml
C:\Users\johnr\Desktop\[media]\classes\engines\e_ru_scout_modern.xml
</code></pre>
<p dir="auto"><strong>My Script py:</strong></p>
<pre><code># encoding=utf-8
from Npp import editor

def forEachFile(func):
    for file in notepad.getFiles():
        notepad.activateBufferID(file[1])
        func()

def printFilename():
    print(notepad.getCurrentFilename())

# store the current buffer id
currentBufferID = notepad.getCurrentBufferID()
# run our printFilename function on each open file
forEachFile(printFilename)
# restore the current buffer from the stored buffer id.
notepad.activateBufferID(currentBufferID)

def multiply_by_2(m):
    return 'BrakesDelay="' + str(float(m.group(1))*1.08) + '"'

editor.rereplace(r'BrakesDelay="(\d*\.?\d*.\d+)"', multiply_by_2)
</code></pre>
<p dir="auto">However, the output files aren’t changed at all. I assume I’m doing something wrong in the <strong>‘func()’</strong> part where I’m supposed to insert something??? Could you please help me figure this out? Thank you.</p>
<p dir="auto">PS Im new so be kind I have no experince in python or notepad etc</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92508</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92508</guid><dc:creator><![CDATA[ImJohnFolks]]></dc:creator><pubDate>Tue, 30 Jan 2024 19:09:45 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Mon, 29 Jan 2024 14:05:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a></p>
<p dir="auto">IMO the best idea here is to forget Notepad++ being involved in the solution, and using only a Python solution.  You obviously already have a standalone Python on your system, know something about programming in Python, etc.</p>
<p dir="auto">So you just have to figure out how to do the operations you’d do using <code>editor.xxx()</code> and/or <code>notepad.xxx()</code> PythonScript functions a different way.  Doing it in standalone Python is only slightly harder than with a PythonScript assist.  From your example script, instead of <code>editor.rereplace()</code>, I’d suggest <code>re.sub()</code>.</p>
<p dir="auto">And that’s as far as I’ll go because as soon as Notepad++ gets chopped out of the discussion, this all becomes off-topic for this forum.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92482</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92482</guid><dc:creator><![CDATA[Alan Kilborn]]></dc:creator><pubDate>Mon, 29 Jan 2024 14:05:19 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Mon, 29 Jan 2024 11:30:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ekopalypse" aria-label="Profile: Ekopalypse">@<bdi>Ekopalypse</bdi></a> said in <a href="/post/92476">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto">They import objects that are exposed by the PythonScript plugin and are only available through it. This means that such scripts only run in an instance of Npp in which this plugin is loaded, and NEVER outside of Npp, i.e. within a cmd shell</p>
</blockquote>
<p dir="auto">Thank you for your swift response regarding the limitations of trying to use batch files to run multiple scripts at once.</p>
<p dir="auto">Given this information, I’m curious to know if there’s a recommended way to achieve a similar batch-like execution of multiple Python scripts directly within Notepad++. Is there an alternative approach? Or is it not feasible to achieve this within Notepad++?</p>
<p dir="auto">Thank you for your assistance!</p>
<p dir="auto">Here is a quick overview<br />
<img src="/assets/uploads/files/1706527807014-untitledvideo-madewithclipchamp-ezgif.com-video-to-gif-converter.gif" alt="Untitledvideo-MadewithClipchamp-ezgif.com-video-to-gif-converter.gif" class=" img-fluid img-markdown" /></p>
]]></description><link>https://community.notepad-plus-plus.org/post/92480</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92480</guid><dc:creator><![CDATA[ImJohnFolks]]></dc:creator><pubDate>Mon, 29 Jan 2024 11:30:11 GMT</pubDate></item><item><title><![CDATA[Reply to Issues Running Python Scripts in Batch File on Mon, 29 Jan 2024 07:43:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/imjohnfolks" aria-label="Profile: ImJohnFolks">@<bdi>ImJohnFolks</bdi></a> said in <a href="/post/92475">Issues Running Python Scripts in Batch File</a>:</p>
<blockquote>
<p dir="auto">from Npp import editor</p>
</blockquote>
<p dir="auto">They import objects that are exposed by the PythonScript plugin and are only available through it. This means that such scripts only run in an instance of Npp in which this plugin is loaded, and NEVER outside of Npp, i.e. within a cmd shell.</p>
]]></description><link>https://community.notepad-plus-plus.org/post/92476</link><guid isPermaLink="true">https://community.notepad-plus-plus.org/post/92476</guid><dc:creator><![CDATA[Ekopalypse]]></dc:creator><pubDate>Mon, 29 Jan 2024 07:43:49 GMT</pubDate></item></channel></rss>