Community
    • Login

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

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 2.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Garvit JoshiG
      Garvit Joshi
      last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • Garvit JoshiG
        Garvit Joshi
        last edited by

        I Have Asked this question on stackoverflow: https://stackoverflow.com/questions/60395741/opening-a-batch-file-in-notepad-in-the-folder-where-notepad-is-opened?noredirect=1#comment106841929_60395741

        EkopalypseE 1 Reply Last reply Reply Quote 0
        • PeterJonesP
          PeterJones
          last edited by

          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

          1 Reply Last reply Reply Quote 3
          • EkopalypseE
            Ekopalypse @Garvit Joshi
            last edited by

            @Garvit-Joshi

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

            1 Reply Last reply Reply Quote 3
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors