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:

53766 questions
374
votes
9 answers

How to set commands output as a variable in a batch file

Is it possible to set a statement's output of a batch file to a variable, for example: findstr testing > %VARIABLE% echo %VARIABLE%
Dennis
  • 3,867
  • 5
  • 17
  • 7
366
votes
9 answers

How do I make a batch file terminate upon encountering an error?

I have a batch file that's calling the same executable over and over with different parameters. How do I make it terminate immediately if one of the calls returns an error code of any level? Basically, I want the equivalent of MSBuild's…
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148
361
votes
12 answers

Assign output of a program to a variable using a MS batch file

I need to assign the output of a program to a variable using a MS batch file. So in GNU Bash shell I would use VAR=$(application arg0 arg1). I need a similar behavior in Windows using a batch file. Something like set VAR=application arg0…
initialZero
  • 6,197
  • 9
  • 29
  • 38
352
votes
16 answers

Batch script loop

I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a for loop, but the for loop expects a list of items, hence I would need 200 files to operate on,…
Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91
351
votes
14 answers

Windows shell command to get the full path to the current directory?

Is there a Windows command line command that I can use to get the full path to the current working directory? Also, how can I store this path inside a variable used in a batch file?
user62958
  • 4,669
  • 5
  • 32
  • 35
346
votes
26 answers

Batch script: how to check for admin rights

How do I check if the current batch script has admin rights? I know how to make it call itself with runas but not how to check for admin rights. The only solutions I've seen are crude hack jobs or use external programs. Well, actually I don't care…
flacs
  • 3,913
  • 4
  • 19
  • 20
327
votes
10 answers

Which comment style should I use in batch files?

I've been writing some batch files, and I ran into this user guide, which has been quite informative. One thing it showed me was that lines can be commented not just with REM, but also with ::. It says: Comments in batch code can be made by using a…
MikeFHay
  • 8,562
  • 4
  • 31
  • 52
325
votes
18 answers

How can you zip or unzip from the script using ONLY Windows' built-in capabilities?

In Windows you can zip some files by right click → Send to → Compressed (zipped) folder And unzip by double clicking on the .zip file and extract the files. Is there a way to apply those abilities from a script (.bat file) without the need to…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
317
votes
6 answers

Batch not-equal (inequality) operator

According to this, !==! is the not-equal string operator. Trying it, I get: C:\> if "asdf" !==! "fdas" echo asdf !==! was unexpected at this time. What am I doing wrong?
ripper234
  • 222,824
  • 274
  • 634
  • 905
313
votes
27 answers

Create an empty file on the commandline in windows (like the linux touch command)

On a windows machine I get this error 'touch' is not recognized as an internal or external command, operable program or batch file. I was following these instructions which seem to be linux specific, but on a standard windows commandline it does…
CuriousAboutNode
  • 3,157
  • 2
  • 11
  • 6
313
votes
18 answers

Stop and Start a service via batch or cmd file?

How can I script a bat or cmd to stop and start a service reliably with error checking (or let me know that it wasn't successful for whatever reason)?
Keng
  • 52,011
  • 32
  • 81
  • 111
311
votes
14 answers

Logical operators ("and", "or") in DOS batch

How would you implement logical operators in DOS Batch files?
JoelFan
  • 37,465
  • 35
  • 132
  • 205
307
votes
25 answers

How to echo with different colors in the Windows command line

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.
rui
  • 11,015
  • 7
  • 46
  • 64
303
votes
6 answers

How to do something to each file in a directory with a batch script

How do you iterate over each file in a directory with a .bat or .cmd file? For simplicity please provide an answer that just echoes the filename or file path.
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
302
votes
19 answers

How to check if a process is running via a batch script

How can I check if an application is running from a batch (well cmd) file? I need to not launch another instance if a program is already running. (I can't change the app to make it single instance only.) Also the application could be running as any…
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143