Notepad++ Run a Batch File From Current Directory



  • @Alan-Kilborn

    <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 &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in IE" Ctrl="yes" Alt="yes" Shift="yes" Key="73">iexplore &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Chrome" Ctrl="yes" Alt="yes" Shift="yes" Key="82">chrome &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Launch in Safari" Ctrl="yes" Alt="yes" Shift="yes" Key="65">safari &quot;$(FULL_CURRENT_PATH)&quot;</Command>
            <Command name="Send via Outlook" Ctrl="yes" Alt="yes" Shift="yes" Key="79">outlook /a &quot;$(FULL_CURRENT_PATH)&quot;</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) &amp;&amp; &quot;D:\Projects\Java\Executor_Java.bat&quot;</Command>
        </UserDefinedCommands>
        <PluginCommands />
        <ScintillaKeys />
    </NotepadPlus>
    

    here’s the whole shortcuts.xml file.
    te.PNG



  • 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 the Run > Run... menu option, is equivalent to the File > Open Containing Folder > cmd menu option ?

    Best Regards,

    guy038



  • @guy038 Sir, Actually When I Wrote cmd /k cd $(CURRENT_DIRECTORY) It opened cmd in C:\Program Files\Notepad++ ,but File>Open Containing Folder >cmd opened cmd in the D:\Projects\Java> , where i want it to open



  • @Garvit-Joshi ,

    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:

    1. If you have a file from D:\Projects\Java open, and select the menu command Run > Run, and only type cmd /k cd $(CURRENT_DIRECTORY), does it open in C:\Program Files\Notepad++ or in D:\Projects\Java ?
    2. What is your Settings > Preferences > Default Directory (Open/Save) set to?
    3. When you edited shortcuts.xml to say <Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /k cd $(CURRENT_DIRECTORY) &amp;&amp; &quot;D:\Projects\Java\Executor_Java.bat&quot;</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.
    4. 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.
    5. If it says Local Conf mode : OFF, then the right shortcuts.xml is %AppData%\Notepad++\shortcuts.xml, so if you were editing C:\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

    1. Close all active instances of Notepad++
    2. Open one instance of Notepad++
    3. Edit the configuration file (for you, type %AppData%\Notepad++\shortcuts.xml when you do File > Open)
    4. Save
    5. Exit Notepad++
    6. Reload Notepad++
    7. 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.



  • @guy038

    Yes. :-)



  • @PeterJones Answers:
    1.When I Run This I got C:\Program Files\Notepad++
    2.The Default Directory IS Set to Follow Current Document.
    3.Yes I have Done Everything In sequence , as I can See a new run statement Java_Executor in Run.
    4.My right shortcuts.xml lies in APPDATA not in Program 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.



  • @Garvit-Joshi ,

    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">&quot;D:\Projects\Java\Executor Java.bat&quot;</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) &amp;&amp; &quot;D:\Projects\Java\Executor_Java.bat&quot;</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 whether shortcuts.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… but cd /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… but cd /d "%~dp0" should be used in case of opening batch file

    Yes, the /d makes sense. I tend to forget that changing directory and drive using cd is more difficult; I primarily use single-drive systems (well, at work, we have network drives mapped to letters, but I’m almost never cd-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 &quot;$(CURRENT_DIRECTORY)&quot; &amp;&amp; &quot;D:\Projects\Java\Executor_Java.bat&quot;</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.


Log in to reply