Sublime Forum

Building a batch file in the cmd console problem

#1

Here’s the settings I have for my .cmd and .bat files to build them.

{
  "file_patterns": ["*.bat", "*.cmd"],
  "selector": "source.batch",
  // This opens and runs in cmd console
  "cmd": ["cmd.exe", "/C", "START", "${file_path}", "${file_name}"]
}

The filepath for the batch file is:
W:\Apps (Portable)\Batch + PowerShell\DOS\Change directories.cmd


This is the code I’m using in the batch file:

@echo off
title This will cd into the fullDir variable....
set "fullDir=C:\Program Files"
cd /d %fullDir%                                &rem changes into the full directory 'C:\Program Files'.
echo You're now cd' into the '%cd%' directory.
title This will cd into the current folder of the batch file...
%~d0                                           &rem Changes into the current drive
cd /d %~d0                                     &rem Changes into the current directory
echo You're now cd' into the '%cd%' directory.   &rem This is now your full path of the batch file cd' into.
pause & exit

The problem I’m facing is if I run the batch file by double-clicking I get this:

You're now cd' into the 'C:\Program Files' directory.
You're now cd' into the 'W:\Apps (Portable)\Batch + PowerShell\DOS' directory.

But using Tools>Build System>Batch with the settings above I get this:

You're now cd' into the 'C:\Program Files' directory.
You're now cd' into the 'C:\Program Files' directory.

You can see the issue here is that the last example is wrong for the 2nd path.

How can I get around this? Thanks.

If there was a way of just mimicking a double click on the file that would do.

0 Likes

#2

I don’t know a lot about batch file scripting in Windows, but it looks like the lines that are going wrong have comments that indicate that you expect to go into the current directory. If that’s the case, your issue is that the sublime-build file doesn’t have anything in it that tells Sublime that a specific directory should be set as the current working directory while the program is executed.

As such, adding a line like the following to your build system might help:

"working_dir": "${file_path}",
0 Likes

#3
"working_dir": "${file_path}",

didn’t work.

Basically my intention is to run the external file as it was intended by double-clicking it.

I don’t want to pass any agruments to cmd window apart from opening the file from its directory.

0 Likes

#4

In your build system, you’re using cmd:

"cmd": ["cmd.exe", "/C", "START", "${file_path}", "${file_name}"]

That tells Sublime that it should execute the program cmd.exe, and that it should pass that command four arguments; is there some significance to having the path to the file and the name of the file be two distinct arguments to the start command when you execute it, as opposed to having three arguments (/c, start and the full name of the file)?

If not, it might help to replace those last two arguments with a single "${file}" argument instead.

0 Likes

#5

Anything other than:
"cmd": ["cmd.exe", "/C", "START", "${file_path}", "${file_name}"]

will open the cmd window directed to the path like so,

If my batch file is located in: W:\Apps (Portable)\Batch + PowerShell\DOS

When I build the file it appears.

Microsoft Windows [Version 10.0.18363.592]
(c) 2019 Microsoft Corporation. All rights reserved.

W:\Apps (Portable)\Batch + PowerShell\DOS>

Whereas it runs the batch file normally with:
"cmd": ["cmd.exe", "/C", "START", "${file_path}", "${file_name}"]

Well minus the bug I have explained.

Here’s a stackoverflow thread. I’ve tried some of the things here but came up short.

I could in theory have just one or two arguments such as this example which changes directory.
"cmd": ["cmd.exe", "/c cd /d ${file_path} & start ", "${file_name}"]
and this example has the same issue I had with the one in the OP
"cmd": ["cmd.exe", "/c cd /d ${file_path} & start ${file_name}"]

I just have to figure out a way to run the file via the cmd commandline.

0 Likes

#6
{
  "file_patterns": ["*.bat", "*.cmd"],
  "selector": "source.batch",
  // This runs the batch file in the cmd window.
  "shell_cmd": "start \"${file_name}\" call \"${file}\""
  // This runs the batch file in sublime texts' console.
  // "shell_cmd": "\"${file}\""
}

This above solved the problem.

It outputs the same as a double click on the batch file.

Here’s some sample code for anyone wanting to test this.

@echo off
title %~nx0
title This will cd into the current folder of the batch file...
set "fullDir=C:\Program Files"
cd /d %fullDir%                                      &rem changes into the full directory 'C:\Program Files'.
echo Directory: %cd%
cd /d %~d0                                           &rem Changes into the current drive of the file.
echo Drive:     %~d0
cd /d Z:                                             &rem Changes into the Z: drive of the file.
echo Drive:     %cd%
cd /d "W:\Apps (Portable)\Batch + PowerShell\DOS"    &rem Changes into the named directory.
echo Directory: %cd%
cd /d %~d0                                           &rem Changes into the current drive of the file.
echo Drive:     %~d0
cd /d "W:\Apps (Portable)\"                          &rem Changes into the named directory.
echo Directory: %cd%
echo/ & pause & exit
0 Likes