Opening A Batch File In Notepad++ In The Folder Where Notepad++ Is Opened



  • I Made A Batch File Which Would Open In Notepad++ From The CURRENT DIRECTORY where the batch file is located(with run… feature in notepad++), But I Want batch file to open on the folder where i have opened the file.
    Example:
    My Batch File Is Located In D:\Projects\Java\Executor Java.bat
    I Have Opened A File Of .java Extension in D:\Java\Files
    I Want to open at file location,i.e. D:\Java\Files
    My batch file Looks like this:

    @ECHO OFF
    ECHO                                        WELCOME TO EXECUTOR
    ECHO                                                         -Garvit Joshi(garvitjoshi9@gmail.com)
    ECHO                                                          USER:%USERNAME%
    cd /d "%~dp0"
    :first
    ECHO LOOKING FOR FILES IN:"%~dp0"
    set /p "input=Enter The File You Want To Execute:"
    ECHO ===============================
    javac %input%.java
    ECHO ===============================
    set /p "input=Enter The Class You Want To Run:"
    ECHO ===============================
    ECHO OUTPUT:
    ECHO ===============================
    java %input%
    ECHO ===============================
    pause
    ECHO =======================================================
    ECHO *******************************************************
    ECHO =======================================================
    goto first
    




  • As the folks in SO told you, your batch file is doing a CD into the batch file’s directory; what else did you expect to happen? If you don’t want to change intot hte batch file directory, then you cannot have cd /d "%~dp0 in your batch file.

    If you want to run a batch file from within the current file’s directory, or want to have the batch change directory, there are multiple options. For now, I will be using Run > Run as my example, but if you need it as part of your UserDefinedCommands like before, it can be easily tweaked.

    I am doing extra, to help you debug things when it goes wrong for you.

    changedir then launch batch (so no cd in .bat):

    • Run>Run cmd /c "cd /d $(CURRENT_DIRECTORY) & echo %CD% & "D:\Projects\Java\Executor Java.bat" & pause"
      • the cd /d $(CURRENT_DIRECTORY) will change to the directory and drive of the currently active file. If the currently active file is your batch file, that’s what directory it will go to; if your current active file is one of your java files, then that’s the directory it will go to
      • the echo %CD% will print the current directory (after changing) to the cmd window
      • the "D:\Projects\Java\Executor Java.bat" will run the batch file in the active directory – ie, where you just cd’d into
      • the pause will prompt for hitting enter before exiting the Run>Run command

    Or, you could pass the current file’s directory, and do the cd inside the batch file:

    @ECHO OFF
    echo ==== INFO BATCH ====
    echo old dir "%cd%"
    echo arg1 "%~1"
    echo cd /d "%~dp1"
    cd /d "%~dp1"
    echo current dir "%cd%"
    

    Then from the Run>Run, you do a debug command like cmd /c "cd %TEMP% & "D:\Projects\Java\Executor Java.bat" "$(FULL_CURRENT_PATH)" & pause"

    • first I change into TEMP directory, so you know that the cd works properly inside the batch
    • then you run the batch, passing it the active file’s full current path (drive, folder, and filename)
      • inside the batch, it changes into the drive and folder for the first argument
    • then you pause again

    When you are done debugging this one, it becomes Run>Run "D:\Projects\Java\Executor Java.bat" "$(FULL_CURRENT_PATH)" or Run>Run cmd /c""D:\Projects\Java\Executor Java.bat" "$(FULL_CURRENT_PATH)"", depending on whether you want a wrapper cmd.exe or not



  • @Garvit-Joshi

    Do you know NppExec plugin?
    Maybe worth to have a look.


Log in to reply