HOWTO · Batch

How to Run CMD Commands in Batch Script

This tutorial provides a comprehensive guide on how to run CMD commands in a batch script. Learn about batch scripting basics, combining commands, using variables, and implementing conditional logic to automate tasks efficiently in Windows. Discover practical examples and detailed explanations to enhance your scripting skills and streamline your workflow.

On this page

A Windows batch file is a text file that runs CMD commands in sequence, so you can save a repeated set of steps and run them together. Put each command on its own line, save the file with a .bat or .cmd extension, then launch it from File Explorer or Command Prompt. The order matters: a command that uses a folder or file must run after the step that creates or reaches it.

How Batch Scripts Run CMD Commands

CMD reads a batch file from top to bottom and processes each line as a command. The @echo off line hides the commands themselves while they run; it does not hide text printed by echo. The runnable shell example below prints the same message; the equivalent CMD line in a .bat or .cmd file is echo Batch commands run in order.

Run this example:

printf '%s\n' 'Batch commands run in order.'
Batch commands run in order.

When the shell example runs, printf prints the message to standard output. In a Windows batch file, the equivalent echo command prints it in the Command Prompt window. The example is intentionally small; add other CMD commands on separate lines when you need more steps.

Put Commands in a Batch File

Open a plain-text editor, enter the commands in the order they should run, and save the file with a descriptive name such as daily-task.bat. The .bat and .cmd extensions both identify Windows command scripts. Check that the editor saved the chosen extension instead of silently appending a text-file extension.

For a longer task, keep dependent commands in order. For example, define a reusable folder with set "WORK_DIR=%TEMP%\BatchDemo", create it with if not exist "%WORK_DIR%" mkdir "%WORK_DIR%", move into it with cd /d "%WORK_DIR%", then write and display a status file with echo Batch file ran successfully.>status.txt and type status.txt. mkdir comes before cd, so the folder exists before the script enters it. cd /d changes both the current drive and directory when needed, and quoting the variable keeps a path with spaces together. These are script commands; their exact effects depend on the Windows environment where the batch file runs.

Run the Batch File

In File Explorer, double-click the saved batch file to launch it. To run it from an existing Command Prompt window, change to its folder with cd /d and enter its filename. If the file is elsewhere, enter its full path in quotes, for example "C:\Users\Sam\Scripts\daily-task.bat"; replace that sample path with the location on your computer.

Running from Command Prompt is useful when you want to read messages or diagnose a script as it proceeds. A pause command waits for a key press, but it is usually unnecessary when you run the file in a window that remains open. You can also omit @echo off while troubleshooting to see each command as CMD processes it.

Use Variables and Conditions

A variable stores a value that can be reused. In a batch file, define one with set "NAME=value" and refer to it as %NAME%. The quoted assignment form avoids accidentally including spaces around the value. The example above stores a folder path in WORK_DIR and uses it in several commands, so changing the folder in one place updates those references.

Use an if statement when a later command should depend on a condition. For example, if exist "%WORK_DIR%\status.txt" echo Status file found displays a message only when that file exists. Put a group of commands in parentheses after the condition when several actions should happen together. Keep the path quoted in both the condition and commands so spaces in a folder name do not split it into separate arguments.

Check Common Problems

If the script does not start, confirm that the saved filename ends in .bat or .cmd, and that the editor did not add another extension. If CMD reports that a command cannot find a path, check the folder spelling and make sure any directory-creation step comes before a command that enters or uses that directory. Quote paths that contain spaces.

If a window closes before you can read a message, run the file from Command Prompt or add pause while diagnosing it. When a command behaves differently from what you expect, run the commands one at a time in CMD and compare their order and paths with the batch file. Keeping repeated commands together makes routine Windows tasks easier to run and update consistently.