6

There are a number of examples on the web of how to run a file from the Notepad Plus Plus (NPP). But they all fail to account for the fact that the current working directory is the location of the NPP's executable, and not the location of the file.
Usually they go something like this:

cmd /K "$(FULL_CURRENT_PATH)" 

Consider the following Python script:

with open('somefile.txt', 'a') as file:
    file.write('Hello there.\n')

This file will be created in the NPP folder, which is not at all what most people would expect. Most people would want it in the same location as the Python file.

You could also do something like this, and it works as expected, but this limits you to Python files only:

<Command name="Run This Python File" Ctrl="no" Alt="no" Shift="yes" Key="116">cmd /K python &quot;$(FULL_CURRENT_PATH)&quot;</Command>

I would not want to add extra code to the Python script to change the current working directory, as normally this would not be needed.

I have been trying to solve this and came up with the following. This line goes in "shortcuts.xml" in the NPP folder.

<Command name="Run This File" Ctrl="yes" Alt="no" Shift="no" Key="116">cmd /K &quot;cd &quot;$(CURRENT_DIRECTORY)&quot; &amp;&amp; &quot;$(FULL_CURRENT_PATH)&quot;&quot;</Command>

So you shut down the NPP, edit the "shortcuts.xml" by adding this line, using another editor, then launch the NPP. To run the file, use Ctrl+F5 key combination.

This works in Windows 10, but fails in Windows XP.

How can I tweak it to work in Windows XP?

mcu
  • 3,302
  • 8
  • 38
  • 64

4 Answers4

6

Try this:

cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
Jackssn
  • 1,346
  • 1
  • 14
  • 16
  • I am highlighting the `\d` option in `cd`. It is essential to change the DRIVE in windows at the same time as directory. Each drive has their own current directory. For example, if you are in `C:\tmp` and say `cd V:\tmp` your current working directory is still `C:\tmp`. In contrast, if you say `cd /d V:\tmp` your current working directory will be `V:\tmp`. – Juha Dec 17 '19 at 13:25
1

My guess would be that the problem is the improperly nested quotes in the command; I'm not sure exactly why it would work on later Windows' while failing on XP.

The command

cmd /K &quot;cd &quot;$(CURRENT_DIRECTORY)&quot; &amp;&amp; &quot;$(FULL_CURRENT_PATH)&quot;&quot;

represents

cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""

Even from the syntax highlighting you can see that the quotes are not quoting what you expect.

To get the desired effect, you can use this command:

cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
:: XML-ified:
cmd /K cd &quot;$(CURRENT_DIRECTORY)&quot; ^&amp;^&amp; &quot;$(FULL_CURRENT_PATH)&quot;
Cauterite
  • 1,637
  • 17
  • 24
0

I run several Windows .bat files from Notepad++. To achieve this one of the entries in the <UserDefinedCommands> section of the file C:\Users\AdrianHHH\AppData\Roaming\Notepad++\shortcuts.xml is:

<Command name="CD and run file" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C &quot;cd /d $(CURRENT_DIRECTORY) &amp;&amp; $(FULL_CURRENT_PATH)&quot;</Command>

With the .bat as the current file I then use menu => Run => CD and run file.

The command line shown in the question appears to have too many &quot; symbols. The current directory includes the drive specifier and so the CD needs the \D option.

The command I use starts cmd \C ... (rather than the \K in the question) so the command window closes automatically. My .bat files normally finish with choice /t 60 /C Y /d Y /n so I can see the command's output.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • The problem with your command line is that `$(FULL_CURRENT_PATH)` is not quoted itself. If its value is, for example, `"c:\f\x y z\w.exe"`, then without the quotes it will be interpreted as `"c:\f\x" "y" "z\w.exe"`. However you're right that using the `/d` flag is probably a good idea. – Cauterite Sep 12 '16 at 10:21
  • @Cauterite , No. CHDIR (i.e. CD) is a funny command, it interprets spaces differently. The output from the command "CD /?" includes *"CHDIR command does not treat spaces as delimiters, so it is possible to CD into a subdirectory name that contains a space without surrounding the name with quotes"*. – AdrianHHH Sep 12 '16 at 11:24
  • `$(FULL_CURRENT_PATH)` is not used as a parameter to `cd`. The `cd` command ends at the `&&`, so this "interprets spaces differently" behaviour does not apply to the command after the `&&`. – Cauterite Sep 12 '16 at 11:41
0

Notepad++ > F5 (Run) > then type following command

cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)" 

assuming you have setup the path, or you may use C:\Python27\python.exe or the path of your python binary, and you will run the python at the folder where the python resides in. You can also save this command to shortcut by clicking button Save....

Afterwards, you also can modify the command in toolbar > Run > Modify shortcut/delete command.