Skip to main content
SysAdmin Shell Scripting Essentials

cmd /c /k Run Bat File: Syntax, Exit Codes, Troubleshooting

cmd run bat file is the operation of executing a .bat or .cmd script from the Windows Command Prompt using cmd with /c or /k, or by directly invoking the script.

cmd /c "C:Scriptscleanup.bat"

Syntax

cmd [/c | /k] "C:pathtoscript.bat" [parameter1 parameter2 ...]
call "C:pathtoscript.bat" [parameters]
start "" "C:pathtoscript.bat" [parameters]

Tested on Windows 11 23H2 with cmd.exe (Microsoft Windows [Version 10.0.22631.XXXX]).

Options and Flags

Flag Type Default Description
/c Switch N/A Runs the command/script and then terminates the cmd session.
/k Switch N/A Runs the command/script and keeps the cmd window open for further commands.
call Internal cmd N/A Invokes another batch file within the same shell context and returns control.
start “” Internal cmd N/A Launches a new cmd window (or external application) to execute the batch file.

Usage Examples

1. Basic execution with /c (fire-and-forget)

cmd /c "C:Scriptscleanup.bat"

Runs the batch file and immediately closes the CMD window. Useful for scheduled tasks or scripts that don’t need interaction.

See also  Sudo User Add: Syntax, Examples, Flags & Production Guide

2. Running with parameters and retaining the window

cmd /k "C:Deploysetup.bat" /quiet /log=C:Logssetup.log

Passes /quiet and /log=... to the batch file. The /k flag keeps the console open so the operator can inspect output or run subsequent commands.

3. Using CALL to invoke a child batch with error handling

@echo off
tasklist | findstr /i chrome.exe
if %errorlevel%==0 call "C:MyscriptsCool.bat"
echo Chrome was not running.

This parent batch checks if Chrome is running using tasklist and findstr. If matched (%errorlevel%==0), it calls Cool.bat within the same context. Otherwise it continues with fallback logic.

4. Launching a batch in a new window with START

start "" "C:Scriptsmonitor.bat" /interval=30

Opens a separate CMD window to run monitor.bat asynchronously. The empty quotes prevent CMD from misinterpreting the first quoted string as the window title.

Troubleshooting & Common Errors

Error Message / Symptom Root Cause Resolution Command
‘.’ is not recognized as an internal or external command Batch script contains a stray period (e.g., ..file.bat) or wrong syntax Edit the .bat file to remove the extra period. Use .file.bat if needed.
Batch file runs but no output / nothing happens Echo is off and no pause statement; script completes silently Add @echo on at the top or a pause at the end to see results.
“The system cannot find the path specified” Current working directory is not correct or path contains spaces without quotes Always quote paths: cmd /c "C:My Folderscript.bat"
Batch file opens and immediately closes Used /c (or double-click) and script has no pause; exit code may indicate failure Run with /k instead: cmd /k script.bat or add pause at end.
Access denied when running from CMD Insufficient permissions or file is blocked (downloaded from internet) icacls "C:pathscript.bat" /grant %username%:F and unblock: right-click → Properties → Unblock.
See also  Macbook Wget: Quick Cheat Sheet, Flags and Verified Examples

Performance Considerations and Tuning

Performance tuning for batch file execution focuses on reducing process creation and redundant checks. The documented usage of tasklist, findstr, if, and call provides proven techniques to minimize overhead.

  • Conditional execution – Use tasklist | findstr -i <process> combined with if %errorlevel%==0 to run a batch file only when a prerequisite process is active. This avoids unnecessary shell launches.
  • Process reuse with call – Invoke sub‑batch files via call "pathtobatch.bat" instead of spawning a new cmd.exe instance. This preserves environment variables and reduces overhead.
  • Errorlevel flow control – Use if errorlevel 1 to short‑circuit execution when a command fails, skipping redundant operations after a non‑zero exit code.

Buffer sizes, MTU, or timeouts are not configurable at the batch-execution layer. The above techniques directly impact runtime overhead and latency.

@echo off
:: Check if Chrome is running before invoking the study batch
tasklist | findstr /I "chrome.exe" >nul
if %errorlevel% equ 0 (
    call "C:MyscriptsCool.bat"
) else (
    echo Chrome is not running. Skipping batch.
)

Frequently Asked Questions

What is the difference between call and start when running a bat file from cmd?

Answer: call runs the bat file in the same context, preserving variable changes and errors; start runs it in a new cmd instance, discarding environment changes.

Use call to chain bat files and retain state. start launches a separate window (or hidden with /b). Example:

call cleanup.bat   REM returns control after completion
start "" cleanup.bat  REM opens new window, asynchronous

For variable persistence, always prefer call.

How do I fix ‘The system cannot find the path specified’ when running a bat file from cmd?

Answer: Verify the file path contains no typos, double quotes enclose paths with spaces, and the directory exists.

Common cause: relative paths when the current directory differs from the bat file location. Fix with full path:

C:Projectsscriptsbuild.bat

Or change directory first:

cd /d "C:Projectsscripts" && build.bat

Check for hidden characters in copied paths (use dir to confirm).

Does cmd run .bat files on Windows Server Core or Nano Server?

Answer: Yes on Server Core (full cmd.exe). On Nano Server, batch files may fail due to missing legacy subsystems.

Nano Server lacks some legacy subsystems; bat files that depend on choice.exe, msg.exe, or GUI elements will error. Test with:

cmd /c "myscript.bat"

For Nano, prefer PowerShell scripts (.ps1) for full compatibility.

What is the fastest way to run a bat file with arguments from cmd without spawning a new window?

Answer: Use cmd /c "myscript.bat arg1 arg2" or directly invoke the batch file.

Direct call (myscript.bat arg1 arg2) has <1ms overhead; start adds ~20ms. Avoid start if you don’t need a new window and do not require the parent shell to wait.

myscript.bat arg1 arg2