Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running File from Notepad Plus Plus and Current Directory

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?

like image 822
mcu Avatar asked Aug 28 '16 15:08

mcu


People also ask

How do I change the directory in Notepad++?

Okay, able to revisit for a couple more minutes: if you need it to be in Run > Run rather than NppExec, starting from cmd /c cd /d "$(CURRENT_DIRECTORY)" && dir "$(FILE_NAME)" && echo change the dir and this last echo into just the compiler.exe -o "$(NAME_PART).exe" "$(FILE_NAME)" or whatever && pause as the command ...

How do I run a batch file in Notepad++?

Run -> Run (F5) Type command line into "The Program to Run" Save, type the name (choose the shortcut if you need)

Where does Notepad++ store its files?

If no cloud location is specified (see above), Notepad++ stores its settings files in %AppData%\Notepad++ , which resides in the user profile. The location of the settings files can be moved to the installation directory by creating the file DoLocalConf.

How do I run a Notepad++ file?

Instructions: Click the Menu "Run -> Run..." (or alternatively hit F5) Enter "$(FULL_CURRENT_PATH)" in the field (without quotes) Click "Save..."


2 Answers

Try this:

cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
like image 88
Jackssn Avatar answered Oct 03 '22 20:10

Jackssn


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;
like image 27
Cauterite Avatar answered Oct 03 '22 19:10

Cauterite