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
314
votes
26 answers

I want to delete all bin and obj folders to force all projects to rebuild everything

I work with multiple projects, and I want to recursively delete all folders with the name 'bin' or 'obj' that way I am sure that all projects will rebuild everything (sometimes it's the only way to force Visual Studio to forget all about previous…
MichaelD
  • 6,739
  • 9
  • 37
  • 45
313
votes
17 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
  • 17,192
  • 12
  • 58
  • 82
307
votes
27 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,483
  • 4
  • 17
  • 19
299
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
  • 7,264
  • 4
  • 24
  • 43
296
votes
25 answers

'adb' is not recognized as an internal or external command, operable program or batch file

I am trying to run google map v2 on emulator, I am following this tutorial. When I was trying to install required apk file on emulator, I am getting below error. I tried to solve this using this tutorial.Followed all steps, added the path to…
Nibha Jain
  • 6,032
  • 10
  • 40
  • 63
295
votes
16 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
  • 48,571
  • 31
  • 77
  • 109
292
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,037
  • 4
  • 14
  • 7
291
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
  • 8,415
  • 16
  • 57
  • 88
284
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
  • 314,085
  • 114
  • 576
  • 619
281
votes
5 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
  • 202,011
  • 255
  • 600
  • 878
279
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
  • 64,328
  • 10
  • 87
  • 143
276
votes
12 answers

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

How would you implement logical operators in DOS Batch files?
JoelFan
  • 34,383
  • 31
  • 123
  • 194
270
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
  • 3,799
  • 5
  • 30
  • 34
262
votes
12 answers

How do you loop through each line in a text file using a windows batch file?

I would like to know how to loop through each line in a text file using a Windows batch file and process each line of text in succession.
Mr. Kraus
  • 7,045
  • 5
  • 25
  • 31
259
votes
9 answers

Batch file to copy files from one folder to another folder

I have a storage folder on a network in which all users will store their active data on a server. Now that server is going to be replaced by a new one due to place problem so I need to copy sub folders files from the old server storage folder to new…
user73628
  • 3,405
  • 5
  • 26
  • 24