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.

Filter by
Sorted by
Tagged with
1436 votes
20 answers
2.5m views

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....
Keng's user avatar
  • 52.6k
1344 votes
24 answers
1.5m views

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's user avatar
  • 267k
1266 votes
7 answers
1.3m views

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 ...
Sebastián Grignoli's user avatar
1156 votes
12 answers
1.7m views

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 ...
user1397044's user avatar
1001 votes
7 answers
619k views

Split long commands in multiple lines through Windows batch file

How can I split long commands over multiple lines in a batch file?
user avatar
895 votes
12 answers
567k views

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 ...
Chris Noe's user avatar
  • 36.8k
882 votes
29 answers
2.6m views

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

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 ...
nonopolarity's user avatar
868 votes
18 answers
1.1m views

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, ...
user avatar
790 votes
27 answers
1.3m views

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's user avatar
755 votes
25 answers
1.4m views

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 ...
Kibbee's user avatar
  • 65.9k
725 votes
11 answers
1.2m views

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 ...
Tony's user avatar
  • 9,141
685 votes
10 answers
2.5m views

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 ...
Keng's user avatar
  • 52.6k
678 votes
21 answers
1.0m views

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's user avatar
  • 17.3k
657 votes
34 answers
574k views

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's user avatar
  • 8,321
647 votes
9 answers
858k views

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 ...
Aaron de Windt's user avatar
637 votes
30 answers
1.7m views

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 @...
Rory's user avatar
  • 41.1k
624 votes
4 answers
1.2m views

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 ...
Jamie Dixon's user avatar
  • 53.7k
610 votes
30 answers
1.2m views

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 ...
Ray's user avatar
  • 190k
601 votes
14 answers
942k views

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:\ ...
Bill's user avatar
  • 6,011
564 votes
3 answers
1.4m views

How to check if a file exists from inside a batch file [duplicate]

I need to run a utility only if a certain file exists. How do I do this in Windows batch?
sab's user avatar
  • 9,857
514 votes
14 answers
1.5m views

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 ...
user avatar
490 votes
13 answers
489k views

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's user avatar
  • 8,174
454 votes
17 answers
829k views

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's user avatar
  • 13.1k
452 votes
30 answers
223k views

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 ...
MichaelD's user avatar
  • 8,547
450 votes
32 answers
622k views

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 >...
user avatar
433 votes
8 answers
477k views

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 want path to be equal to c:\path\to\my\file. How can I achieve that?
Misha Moroshko's user avatar
427 votes
30 answers
1.2m views

'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 ...
Nibha Jain's user avatar
  • 7,891
418 votes
9 answers
435k views

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 ...
Chris Smith's user avatar
  • 18.5k
405 votes
16 answers
1.3m views

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. ...
Deniz Zoeteman's user avatar
389 votes
9 answers
820k views

How to set commands output as a variable in a batch file [duplicate]

Is it possible to set a statement's output of a batch file to a variable, for example: findstr testing > %VARIABLE% echo %VARIABLE%
Dennis's user avatar
  • 4,017
388 votes
4 answers
630k views

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 ...
Hamed Kamrava's user avatar
376 votes
9 answers
344k views

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? ...
Josh Kodroff's user avatar
  • 27.7k
369 votes
12 answers
421k views

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. ...
initialZero's user avatar
  • 6,287
367 votes
16 answers
1.3m views

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 ...
Tom J Nowell's user avatar
  • 9,831
366 votes
14 answers
858k views

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's user avatar
  • 4,819
350 votes
26 answers
258k views

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 ...
flacs's user avatar
  • 3,953
331 votes
10 answers
372k views

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 ...
MikeFHay's user avatar
  • 8,729
327 votes
18 answers
839k views

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 ...
Roee Gavirel's user avatar
  • 19.2k
324 votes
6 answers
560k views

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's user avatar
  • 226k
323 votes
27 answers
647k views

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,...
CuriousAboutNode's user avatar
318 votes
14 answers
595k views

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

How would you implement logical operators in DOS Batch files?
JoelFan's user avatar
  • 38k
317 votes
27 answers
458k views

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's user avatar
  • 11.2k
313 votes
18 answers
958k views

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's user avatar
  • 52.6k
307 votes
6 answers
450k views

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's user avatar
306 votes
20 answers
777k views

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 ...
Matt Lacey's user avatar
  • 65.8k
302 votes
18 answers
495k views

What is the proper way to test if a parameter is empty in a batch file?

I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever %1 is surrounded by quotes such as the case when %1 is "c:\some path with spaces". ...
blak3r's user avatar
  • 16.3k
300 votes
24 answers
391k views

Quickly create large file on a Windows system

In the same vein as Quickly create a large file on a Linux system, I'd like to quickly create a large file on a Windows system. By large I'm thinking 5 GB. The content doesn't matter. A built-in ...
Leigh Riffel's user avatar
  • 6,511
297 votes
12 answers
648k views

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's user avatar
  • 8,025
296 votes
20 answers
334k views

Windows batch: echo without new line

What is the Windows batch equivalent of the Linux shell command echo -n which suppresses the newline at the end of the output? The idea is to write on the same line inside a loop.
gregseth's user avatar
  • 13.2k
292 votes
10 answers
1.6m views

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 ...
user73628's user avatar
  • 3,735

1
2 3 4 5
1079