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
529
votes
27 answers

How can you find and replace text in a file using the Windows command-line environment?

I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?
Ray
  • 169,974
  • 95
  • 213
  • 200
525
votes
7 answers

How to capture cURL output to a file?

I have a text document that contains a bunch of URLs in this format: URL = "sitehere.com" What I'm looking to do is to run curl -K myfile.txt, and get the output of the response cURL returns, into a file. How can I do this?
Tony
  • 6,524
  • 7
  • 27
  • 40
498
votes
3 answers

How to check if a file exists from inside a batch file

I need to run a utility only if a certain file exists. How do I do this in Windows batch?
sab
  • 8,917
  • 11
  • 38
  • 48
497
votes
10 answers

Create folder with batch but only if it doesn't already exist

Can anybody tell me how to do the following in in a Windows batch script? (*.bat): Create a folder only if it doesn't already exist In more detail, I want to create a folder named VTS on the C:\ drive, but only if that folder doesn't already…
Bill
  • 4,971
  • 2
  • 13
  • 3
495
votes
4 answers

Defining and using a variable in batch file

I'm trying to define and use a variable in a batch file. It looks like it should be simple: @echo off set location = "bob" echo We're working with "%location%" The output I get is the following: We're working with "" What's going on here? Why is…
Jamie Dixon
  • 49,653
  • 18
  • 119
  • 157
445
votes
13 answers

How to get list of arguments?

I'd like to find a Windows batch counterpart to Bash's $@ that holds a list of all arguments passed into a script. Or I have to bother with shift?
wheleph
  • 7,107
  • 7
  • 33
  • 53
439
votes
13 answers

In Windows cmd, how do I prompt for user input and use the result in another command?

I have a Windows .bat file which I would like to accept user input and then use the results of that input as part of the call to additional commands. For example, I'd like to accept a process ID from the user, and then run jstack against that ID,…
marc
401
votes
32 answers

Displaying Windows command prompt output and redirecting it to a file

How can I run a command-line application in the Windows command prompt and have the output both displayed and redirected to a file at the same time? If, for example, I were to run the command dir > test.txt, this would redirect output to a file…
Ammu
374
votes
8 answers

How to get the path of the batch script in Windows?

I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.bat I would path to be equal to c:\path\to\my\file How could I achieve that ?
Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700
373
votes
9 answers

What does %~d0 mean in a Windows batch file?

I'm looking at a batch file which defines the following variables: set _SCRIPT_DRIVE=%~d0 set _SCRIPT_PATH=%~p0 What do %~d0 or %~p0 actually mean? Is there a set of well-known values for things like current directory, drive, parameters to a…
Chris Smith
  • 16,814
  • 12
  • 55
  • 78
372
votes
17 answers

Iterate all files in a directory using a 'for' loop

How can I iterate over each file in a directory using a for loop? And how could I tell if a certain entry is a directory or if it's just a file?
Vhaerun
  • 11,406
  • 15
  • 36
  • 38
335
votes
15 answers

How to delete files/subfolders in a specific directory at the command prompt in Windows

Say, there is a variable called %pathtofolder%, as it makes it clear it is a full path of a folder. I want to delete every single file and subfolder in this directory, but not the directory itself. But, there might be an error like 'this file/folder…
Deniz Zoeteman
  • 8,671
  • 23
  • 65
  • 96
322
votes
10 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 arg1.
initialZero
  • 5,667
  • 8
  • 26
  • 37
316
votes
4 answers

Get current batchfile directory

Firstly, I saw this topic but I couldn't understand that. Question : There is a batch file in D:\path\to\file.bat with following content : echo %cd% pause Output is : C:\ It must be D:\path\to What am I doing wrong?
Hamed Kamrava
  • 10,533
  • 32
  • 78
  • 117
315
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
  • 25,181
  • 26
  • 90
  • 147