Questions tagged [batch-file]

A batch file is a text file containing a series of commands that are executed by the command interpreter on MS-DOS, IBM OS/2, or Microsoft Windows systems.

Batch files are text-based scripts, usually saved with the .bat, .cmd, or .btm filename extension. They are executed by the command processor (typically COMMAND.COM on MS-DOS and earlier versions of Windows, cmd.exe on IBM OS/2 and later version of Windows). Note that, while batch files are still supported under Windows, recent versions have the much more expansive PowerShell.

Example

This is the source code to a typical "Hello world" program in batch programming:

@ECHO off
ECHO Hello World!
PAUSE

Note the ! may not display if delayed expansion is enabled.

Tag usage

The tag can be used for programming-related problems in writing a batch script file for a Windows-based operating system. Please avoid "suggest a book"-type questions. Note the tag is not to be used for questions referring to a "batch of files" or referring to the "Spring Batch" framework but for questions related to the shell language only.

Useful links

See also:

50401 questions
1237
votes
18 answers

How can I pass arguments to a batch file?

I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file. Here's what the command line looks like: test.cmd admin P@55w0rd > test-log.txt
Keng
  • 48,571
  • 31
  • 77
  • 109
1098
votes
7 answers

What does %~dp0 mean, and how does it work?

I find %~dp0 very useful, and I use it a lot to make my batch files more portable. But the label itself seems very cryptic to me... What is the ~ doing? Does dp mean drive and path? Does the 0 refer to %0, the path to the batch file that includes…
Sebastián Grignoli
  • 29,517
  • 17
  • 67
  • 82
1084
votes
21 answers

How do I run two commands in one line in Windows CMD?

I want to run two commands in a Windows CMD console. In Linux I would do it like this touch thisfile ; ls -lstrh How is it done on Windows?
flybywire
  • 232,954
  • 184
  • 384
  • 491
964
votes
11 answers

How to "comment-out" (add comment) in a batch/cmd?

I have a batch file that runs several python scripts that do table modifications. I want to have users comment out the 1-2 python scripts that they don't want to run, rather than removing them from the batch file (so the next user knows these…
user1397044
  • 10,099
  • 6
  • 17
  • 16
819
votes
6 answers

Split long commands in multiple lines through Windows batch file

How can I split long commands over multiple lines in a batch file?
Dan
795
votes
13 answers

Windows batch files: .bat vs .cmd?

As I understand it, .bat is the old 16-bit naming convention, and .cmd is for 32-bit Windows, i.e., starting with NT. But I continue to see .bat files everywhere, and they seem to work exactly the same using either suffix. Assuming that my code will…
Chris Noe
  • 33,647
  • 22
  • 66
  • 90
757
votes
30 answers

How to sleep for five seconds in a batch file/cmd

Windows's Snipping tool can capture the screen, but sometimes I want to capture the screen after five seconds, such as taking an image being displayed by the webcam. (Run the script and smile at the camera, for example.) How do I sleep for 5 seconds…
nonopolarity
  • 130,775
  • 117
  • 415
  • 675
750
votes
18 answers

How to run multiple .BAT files within a .BAT file

I'm trying to get my commit-build.bat to execute other .BAT files as part of our build process. Content of commit-build.bat: "msbuild.bat" "unit-tests.bat" "deploy.bat" This seems simple enough, but commit-build.bat only executes the first item in…
Th3Fix3r
705
votes
25 answers

Batch file to delete files older than N days

I am looking for a way to delete all files older than 7 days in a batch file. I've searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the…
Kibbee
  • 62,900
  • 26
  • 139
  • 178
690
votes
19 answers

How can I echo a newline in a batch file?

How can you you insert a newline from your batch file output? I want to do something like: echo hello\nworld Which would output: hello world
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
666
votes
10 answers

How do I shutdown, restart, or log off Windows via a bat file?

I've been using Remote Desktop Connection to get into a workstation. But in this environment, I cannot use the power options in Start Menu. I need an alternative way to shutdown or restart. How do I control my computer's power state through the…
Keng
  • 48,571
  • 31
  • 77
  • 109
569
votes
29 answers

How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

Update: Now that it's 2016 I'd use PowerShell for this unless there's a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using date. See @npocmaka's…
Rory
  • 36,412
  • 47
  • 153
  • 230
564
votes
9 answers

What is the current directory in a batch file?

I want to create a few batch files to automate a program. My question is when I create the batch file, what is the current directory? Is it the directory where the file is located or is it the same directory that appears in the command prompt, or…
Aaron de Windt
  • 13,661
  • 12
  • 44
  • 58
558
votes
17 answers

How to prevent auto-closing of console after the execution of batch file

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?
joe
  • 15,556
  • 35
  • 90
  • 129
544
votes
31 answers

How do I measure execution time of a command on the Windows command line?

Is there a built-in way to measure execution time of a command on the Windows command line?
Kuroki Kaze
  • 7,142
  • 3
  • 34
  • 48
1
2 3
99 100