Garvit Joshi
Garvit Joshi

Reputation: 133

Run a batch file from its parent directory in NotePad++

I made a batch file that identifies java files kept in that particular folder and compiles them. But when I was running it from notepad++, the batch file was going to notepad++ working directory i.e. C:\Program Files\Notepad++, and then starting, so I was not getting desired output.

<Command name="Java_Executor" Ctrl="no" Alt="no" Shift="no" Key="0">&quot;D:\Projects\Java\Executor Java.bat&quot;</Command>

I tried to edit it many times but didn't got desired output.

<Command name="second try" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C &quot;cd /d D:\Projects\Java\ &amp;&amp; D:\Projects\Java\Executor Java.bat&quot;</Command>

This was my second try.

This is the third try:

<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 should I edit so that my batch file runs from the same folder where it is located? Assuming my bat file is D:\Projects\Java\Executor Java.bat with the contents of:

@ECHO OFF
color F0
ECHO                                        WELCOME TO EXECUTOR
ECHO                                                         -Garvit Joshi([email protected])
ECHO                                                          USER:%USERNAME%
cd %cd% 
:first
ECHO LOOKING FOR FILES IN:%cd%
color F0
ECHO Name Of Java Executable Files Present In Folder Are:
python Filename_java.py
set /p "input=Enter The File You Want To Execute:"
ECHO ===============================
javac %input%.java
ECHO ===============================
ECHO Name Of Java Executable Class Present In Folder Are:
python Filename_class.py
ECHO ===============================
javac %input%.java
ECHO ===============================
set /p "input=Enter The Class You Want To Run:"
color 0A
ECHO ===============================
ECHO OUTPUT:
ECHO ===============================
java %input%
ECHO ===============================
color 0F
pause
ECHO =======================================================
ECHO *******************************************************
ECHO =======================================================
goto first

Upvotes: 0

Views: 943

Answers (1)

michael_heath
michael_heath

Reputation: 5372

<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>

Third try seems almost there. You are changing directory from 'C:/Program Files/Notepad++ to D:\Projects\Java which will require /d after cd, else it will not change directory as to being on a different drive. Probably a good idea to enclose $(CURRENT_DIRECTORY) with double quotes as the current directory may have special characters with it's path i.e. "D:\dogs & cats". Executor Java.bat displays same issues with use of cd so it will behave similar.

Test script "D:\Projects\Java\Executor Java.bat" being:

@echo cd: "%cd%"

Run Java Executor from Run menu of Notepad++ displays prompt window with:

cd: "D:\Projects\Java"

D:\Projects\Java>

The prompt is ready for input as cmd /k was used.

In the batch-file in the question post is the line:

cd %cd%

I would expect that to do nothing as %cd% is already the current directory.

Perhaps wanting to change directory to the script directory:

cd /d "%~dp0"

View for /? or call /? about modifiers like dp.

Upvotes: 1

Related Questions