batch-file
asynchronous-command
redirect-output
command-line
file-io

Execute a command asynchronously while redirecting the output to a file in a batch file?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a Windows batch file, the usual way to start a command asynchronously is start. To redirect the command output to a file at the same time, you normally wrap the real command in cmd /c, because redirection has to happen inside the new process rather than in the parent batch script.

Use start with cmd /c

The basic pattern looks like this:

bat
@echo off
start "" /b cmd /c "ping 127.0.0.1 -n 6 > output.log 2>&1"
echo Command launched

This does three important things:

  • 'start launches a new process without waiting for it to finish'
  • 'cmd /c runs the command and exits when it is done'
  • '> output.log 2>&1 redirects both standard output and standard error'

The batch file continues immediately after start, which is why echo Command launched appears right away.

Why the Empty Quotes Matter

One of the most confusing details in start syntax is that the first quoted string is treated as the window title, not as the command. If you skip the empty title and your command itself is quoted, start may misread the command line.

Correct:

bat
start "" /b cmd /c "dir C:\ > listing.txt"

Incorrect:

bat
start "cmd /c dir C:\ > listing.txt"

That small empty string prevents a lot of quoting bugs.

Redirect Output Inside the Spawned Command

Redirection must be inside the command string passed to cmd /c. If you place the redirection outside, you are often redirecting the parent batch script instead of the asynchronous child process.

Good:

bat
start "" /b cmd /c "mytool.exe -v > tool.log 2>&1"

Not what you usually want:

bat
start "" /b cmd /c mytool.exe -v > tool.log 2>&1

The first version keeps the redirection attached to the spawned command. The second version is much easier to misparse, especially once spaces and quoted paths are involved.

Append Instead of Overwrite When Needed

If you want the new command to append to an existing file instead of replacing it, use >>:

bat
@echo off
start "" /b cmd /c "echo Run started >> task.log 2>&1"

For long-running scheduled scripts, append mode is often the better default because it preserves previous runs.

Handle Commands and Paths with Spaces

Windows command lines become tricky as soon as executable paths contain spaces. Put the full command inside the cmd /c string and quote the executable path there.

bat
@echo off
start "" /b cmd /c "\"C:\Program Files\My App\worker.exe\" --mode batch > worker.log 2>&1"

The escaped quotes around the executable are necessary because they live inside the larger quoted string passed to cmd /c.

Know What /b Changes

The /b option tells start to run without creating a new visible console window. That is useful for background jobs launched from scripts.

bat
start "" /b cmd /c "timeout /t 5 > delay.log 2>&1"

If you omit /b, Windows may open a separate command window for the asynchronous task. Sometimes that is desirable for debugging, but it is usually noisy for automation.

Logging and Exit Codes

Because the command is asynchronous, the parent batch file does not automatically receive the child process exit code. If you need to know whether the child succeeded, you usually need one of these patterns:

  • have the child write a success marker file
  • log the exit code from inside the cmd /c string
  • run synchronously instead of asynchronously

Example of logging the exit code:

bat
@echo off
start "" /b cmd /c "mytool.exe > tool.log 2>&1 & echo ExitCode=%%errorlevel%%>> tool.log"

That works inside a batch file because %%errorlevel%% is expanded by the child command interpreter.

Common Pitfalls

The biggest mistake is forgetting that the first quoted argument to start is the window title, not the command path. Another common problem is placing redirection outside the cmd /c string, which redirects the wrong process. Quoting paths with spaces is also easy to get wrong because there are multiple layers of command parsing involved. Finally, asynchronous execution means you cannot rely on the parent batch file's %errorlevel% to tell you whether the launched command succeeded.

Summary

  • Use start "" /b cmd /c "your command > file 2>&1" to run a command asynchronously and capture its output.
  • Keep the redirection inside the quoted cmd /c command string.
  • Include the empty title argument after start so quoting works correctly.
  • Use >> if you want to append to the log file instead of overwriting it.
  • Remember that asynchronous commands do not give the parent batch file a normal synchronous exit code.

Course illustration
Course illustration

All Rights Reserved.