4

Context:

I have a .BAT file with the following code:

cd C:\folder1\folder2
subl .

When executing that .BAT file in dir "C:/folder3", I want to stay at the navigated directory "C:\folder1\folder2" from my .BAT file. Now i'm just returning to my previous directory where i've executed that bat file, which is C:\folder3.

This because i want to execute some manual commands in that directory "C:\folder1\folder2"

  • Is this the full code of the batch file? if not, are you using setlocal? – MC ND Oct 13 '16 at 10:56
  • No i'm not using setlocal (don't have any experience with bat files..) – JavascriptDeveloper Oct 13 '16 at 12:51
0

Both cd and pushd will both work with any commands you're trying to use within the batch file - the issue here is that when you try to use subl it will execute outside of the batch file (ignoring attempts to change the directory). The first thing you're going to want to make sure of is that you have the correct environment variables in place to run Sublime from command.

To do this:

  1. Go to Control Panel
  2. Click System
  3. Click Advanced system settings
  4. Click Environment Variables...
  5. Create a New System variable
  6. Name the variable SUBLIME
  7. Make the variable value the folder path of your Sublime installation
    (i.e. C:\Program Files\Sublime Text 3)
  8. Click OK
  9. Edit the Path variable located in your User variables
  10. Add %SUBLIME% to your variable value (after the ;)
  11. Click OK on all the windows to close them

Even when subl will work from command, the issue remains that the sublime_text.exe or the subl.exe will still run from its home directory - regardless of what current directory is set within your batch. To work around this, you'll need to include your target directory in your subl commands so subl knows where to go to execute them. You can set your target directory as a variable and use it within the subl commands:

@echo off

set "dir=C:\folder1\folder2"
subl "%dir%\targetfileorfolder"

Hopefully that helps; it's nothing you're doing wrong in your batch file - it ultimately boils down to the subl configuration. I was able to find a semi-relevant answer here for Python, and you may be able to do something similar for cmd/batch if you are comfortable changing those kinds of settings (if they are available at all).

0

You need to use pushd

:: to call and make direcoty current
pushd C:\folder1\folder2
subl .
:: go back to your old current working directory
popd 

cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.