Notepad++ Run a Batch File From Current Directory
-
how can I Edit shortcuts.xml file so that I Can Run a batch file from my current directory. Actually, when I Run a Batch File in Notepad++ ,it is taking it to its directory C:\Program Files\Notepad++ and then running it in that directory, but i want it to run that file where it is located.
My Batch File Is Located In:D:\Projects\Java\Executor Java.bat<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">"D:\Projects\Java\Executor Java.bat"</Command>
Can You Please Edit this So That I Can Run It from the current Directory where it is located.
Thanks. -
for example
<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd $(CURRENT_DIRECTORY) && "D:\Projects\Java\Executor Java.bat"</Command>
/k is for keeping the cmd window open, /C would be used in order to close it automatically.
-
@Ekopalypse Sorry, but batch file is still going to notepad++ directory.
-
@Garvit-Joshi said in Notepad++ Run a Batch File From Current Directory:
but batch file is still going to notepad++ directory
Not for me.
If I do
cmd /k
then I get the Notepad++ directory.But doing
cmd /k cd $(CURRENT_DIRECTORY)
I get the directory of the active file.Just like what I would expect from what @Ekopalypse said…
-
<NotepadPlus> <InternalCommands /> <Macros> <Macro name="Trim Trailing Space and Save" Ctrl="no" Alt="yes" Shift="yes" Key="83"> <Action type="2" message="0" wParam="42024" lParam="0" sParam="" /> <Action type="2" message="0" wParam="41006" lParam="0" sParam="" /> </Macro> </Macros> <UserDefinedCommands> <!-- The following shortcuts are dangerous if your computer is compromised. (But if your computer is compromised, EVERYTHING IS DANGEROUS!) Remove the comment and use them at your own risk!!! <Command name="Launch in Firefox" Ctrl="yes" Alt="yes" Shift="yes" Key="88">firefox "$(FULL_CURRENT_PATH)"</Command> <Command name="Launch in IE" Ctrl="yes" Alt="yes" Shift="yes" Key="73">iexplore "$(FULL_CURRENT_PATH)"</Command> <Command name="Launch in Chrome" Ctrl="yes" Alt="yes" Shift="yes" Key="82">chrome "$(FULL_CURRENT_PATH)"</Command> <Command name="Launch in Safari" Ctrl="yes" Alt="yes" Shift="yes" Key="65">safari "$(FULL_CURRENT_PATH)"</Command> <Command name="Send via Outlook" Ctrl="yes" Alt="yes" Shift="yes" Key="79">outlook /a "$(FULL_CURRENT_PATH)"</Command> --> <Command name="Get PHP help" Ctrl="no" Alt="yes" Shift="no" Key="112">https://www.php.net/$(CURRENT_WORD)</Command> <Command name="Wikipedia Search" Ctrl="no" Alt="yes" Shift="no" Key="114">https://en.wikipedia.org/wiki/Special:Search?search=$(CURRENT_WORD)</Command> <Command name="Open file in another instance" Ctrl="no" Alt="yes" Shift="no" Key="117">$(NPP_FULL_FILE_PATH) $(CURRENT_WORD) -nosession -multiInst</Command> <Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd $(CURRENT_DIRECTORY) && "D:\Projects\Java\Executor_Java.bat"</Command> </UserDefinedCommands> <PluginCommands /> <ScintillaKeys /> </NotepadPlus>
here’s the whole shortcuts.xml file.
-
Hi, @garvit-joshi, @ekopalypse, @Alan-kilborn and All,
So, as I understand it, independently of the @garvit-joshi’s problem, writing
cmd /k cd $(CURRENT_DIRECTORY)
, in theRun > Run...
menu option, is equivalent to theFile > Open Containing Folder > cmd
menu option ?Best Regards,
guy038
-
@guy038 Sir, Actually When I Wrote
cmd /k cd $(CURRENT_DIRECTORY)
It opened cmd inC:\Program Files\Notepad++
,butFile>Open Containing Folder >cmd
opened cmd in theD:\Projects\Java>
, where i want it to open -
Reading through this thread, I am not 100% sure of your exact sequence of events. There have been mentions of editing
shortcuts.xml
, but also of using the Run > Run menu.So, some questions for clarification:
- If you have a file from
D:\Projects\Java
open, and select the menu command Run > Run, and only typecmd /k cd $(CURRENT_DIRECTORY)
, does it open inC:\Program Files\Notepad++
or inD:\Projects\Java
? - What is your Settings > Preferences > Default Directory (Open/Save) set to?
- When you edited
shortcuts.xml
to say<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd $(CURRENT_DIRECTORY) && "D:\Projects\Java\Executor_Java.bat"</Command>
, what was your sequence? I ask, because just saving that file is not enough: you have to reload Notepad++. You also need to make sure you are editing the right one. - To find out which is the right
shortcuts.xml
, please go to the ? menu (the menu to the right of Window on the menu bar), select Debug Info, and Copy debug info into clipboard, and paste into your reply. - If it says
Local Conf mode : OFF
, then the rightshortcuts.xml
is%AppData%\Notepad++\shortcuts.xml
, so if you were editingC:\Program Files\Notepad++\shortcuts.xml
, you were not influencing the Run menu at all.
Assuming you are not in local conf mode, the right sequence, as described in the official docs, is
- Close all active instances of Notepad++
- Open one instance of Notepad++
- Edit the configuration file (for you, type
%AppData%\Notepad++\shortcuts.xml
when you do File > Open) - Save
- Exit Notepad++
- Reload Notepad++
- The changes will now be in effect.
If you are still having problems after being certain you edited the correct file and have re-started, then we’ll probably run an experiment where we simplify your command (temporarily not having the
&& "D:\Projects\Java\Executor_Java.bat"
in the command) to see whether it’s the&&
notation messing things up.But let’s start with having you answer everything I’ve asked in this post, first.
- If you have a file from
-
Yes. :-)
-
@PeterJones Answers:
1.When I Run This I gotC:\Program Files\Notepad++
2.The Default Directory IS Set toFollow Current Document
.
3.Yes I have Done Everything In sequence , as I can See a new run statementJava_Executor
in Run.
4.My rightshortcuts.xml
lies inAPPDATA
not inProgram Files
5.My Debug info:Notepad++ v7.8.4 (64-bit)
Build time : Jan 29 2020 - 01:31:03
Path : C:\Program Files\Notepad++\notepad++.exe
Admin mode : OFF
Local Conf mode : OFF
OS Name : Windows 10 Home Single Language (64-bit)
OS Version : 1909
OS Build : 18363.657
Plugins : mimeTools.dll NppConverter.dll NppExport.dll -
@Garvit-Joshi said in Notepad++ Run a Batch File From Current Directory:
1.When I Run This I got C:\Program Files\Notepad++
but you made sure that the file you had open actually is a saved file, correct?
I mean, if your current document is for example a new 1 file it would return the npp directory but if the current document is a saved file it
should open the directory of that file. -
re: #1: As @Ekopalypse said, that shouldn’t happen unless you’re in a new file, rather than a file in the desired directory.
If I use the following in the Run > Run menu:
cmd /k echo "$(CURRENT_DIRECTORY)" "$(FULL_CURRENT_PATH)" "$(FILE_NAME)" "$(NAME_PART)" "$(EXT_PART)" "$(CURRENT_WORD)" "$(CURRENT_LINE)" "$(CURRENT_COLUMN)" "$(NPP_DIRECTORY)" "$(NPP_FULL_FILE_PATH)"
If I run that command in a new document, I get:
"" "new 1" "new 1" "new 1" "" "" "0" "200" "C:\usr\local\apps\notepad++" "C:\usr\local\apps\notepad++\notepad++.exe"
If I run that same command in a different file, which has been saved and exists on disk (I just change the active tab to the already-existing file, then Run > Run again):
"C:\usr\local\share\PassThru\homeTresor\Other\.cpanreporter" "C:\usr\local\share\PassThru\homeTresor\Other\.cpanreporter\petercj.readme" "petercj.readme" "petercj" ".readme" "or" "9" "35" "C:\usr\local\apps\notepad++" "C:\usr\local\apps\notepad++\notepad++.exe"
please run a similar experiment: once when you have your real file loaded, and once when you have a new file in the active editor tab.
-----
#3: You said,
I have Done Everything In sequence , as I can See a new run statement Java_Executor in Run.
That doesn’t necessarily mean you’ve done everything in proper sequence. Have you reloaded Notepad++ since you changed
shortcuts.xml
? Because in your first post, you originally had<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">"D:\Projects\Java\Executor Java.bat"</Command>
but now (post https://community.notepad-plus-plus.org/post/50725) you showed
<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd $(CURRENT_DIRECTORY) && "D:\Projects\Java\Executor_Java.bat"</Command>
both of those would still show
Java_Executor
in the run menu. So we need to know if you’ve actually reloaded Notepad++ since you made that change. And we need to know whethershortcuts.xml
still contains the newer version of the command after the reload, or whether it reverted back to your original. -
@PeterJones , thanku for Your Help i found this community very helpful, I will try this and will surely tell you if it worked… BTW i got an answer from stackoverflow… they told that my batch file was not able to take directory properly, I used
%cd%
to go to the directory… butcd /d "%~dp0"
should be used in case of opening batch file from notepad++.
You can refer to this thread: https://stackoverflow.com/questions/60261732/run-a-batch-file-from-its-parent-directory-in-notepad?noredirect=1#comment106613957_60261732
p.s.:Sorry if i wasted your time… -
I will try this and will surely tell you if it worked
It wasn’t so much a “worked” as “we need to see what Notepad++ is sending to the command processor to figure out what’s going wrong”.
But with the bit of information, it’s irrelevant now:
I used
%cd%
to go to the directory… butcd /d "%~dp0"
should be used in case of opening batch fileYes, the
/d
makes sense. I tend to forget that changing directory and drive usingcd
is more difficult; I primarily use single-drive systems (well, at work, we have network drives mapped to letters, but I’m almost nevercd
-ing into those directories)The
%~dp0
changes the batch/script name to just be a drive and folder… but since in the NotepadPlusPlus user-commands, you don’t have a batch/script filename, and$(CURRENT_DIRECTORY)
already gives what you want (just the drive and folder), you don’t need that part.So, the command you need is
<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd /d "$(CURRENT_DIRECTORY)" && "D:\Projects\Java\Executor_Java.bat"</Command>
Note: I also put quotes around
$(CURRENT_DIRECTORY)
as well, in case it has spaces in the path.BTW: Thanks for linking to the SO thread; when asking a question in multiple forums, it’s polite to crosslink both right away, so people can see whether it’s already been answered in the other location, or can learn from the other community’s responses.