50,364
questions
1445
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....
1363
votes
25
answers
1.6m
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?
1273
votes
7
answers
1.4m
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 ...
1181
votes
12
answers
1.8m
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 ...
1015
votes
7
answers
637k
views
Split long commands in multiple lines through Windows batch file
How can I split long commands over multiple lines in a batch file?
906
votes
12
answers
580k
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 ...
895
votes
29
answers
2.7m
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 ...
881
votes
18
answers
1.2m
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, ...
793
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
759
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 ...
748
votes
11
answers
1.3m
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 ...
687
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?
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 ...
663
votes
34
answers
585k
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?
652
votes
9
answers
879k
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 ...
644
votes
30
answers
1.8m
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 @...
633
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 ...
616
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 ...
605
votes
14
answers
962k
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:\ ...
571
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?
519
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 ...
494
votes
13
answers
495k
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?
461
votes
30
answers
234k
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 ...
455
votes
17
answers
849k
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?
453
votes
32
answers
637k
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 >...
441
votes
30
answers
1.3m
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 ...
438
votes
8
answers
488k
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?
419
votes
9
answers
442k
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 ...
407
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.
...
392
votes
4
answers
645k
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
...
392
votes
9
answers
840k
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%
381
votes
9
answers
353k
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?
...
373
votes
14
answers
891k
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?
371
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 ...
371
votes
12
answers
426k
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.
...
351
votes
26
answers
263k
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 ...
332
votes
27
answers
671k
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,...
331
votes
10
answers
375k
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 ...
329
votes
18
answers
843k
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 ...
328
votes
6
answers
572k
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?
323
votes
14
answers
606k
views
Logical operators ("and", "or") in DOS batch
How would you implement logical operators in DOS Batch files?
320
votes
28
answers
473k
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.
315
votes
18
answers
976k
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)?
308
votes
20
answers
789k
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 ...
307
votes
6
answers
458k
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.
305
votes
18
answers
505k
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".
...
304
votes
24
answers
412k
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 ...
300
votes
12
answers
658k
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.
299
votes
20
answers
339k
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.
294
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 ...