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 expressive 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" but for questions related to the shell language only.

Useful links

3148 questions
188
votes
20 answers

Run a batch file in a completely hidden way

I'm looking for some way to run a batch file (.bat) without anything visible to the user (no window, no taskbar name, .etc..). I don't want to use some program to do that, I'm looking for something cleaner. I've found a solution that uses VBScript,…
172
votes
9 answers

How to run a batch file without launching a "command window"?

On Windows XP, can I run a batch (.bat or .cmd) file, via a shortcut, without a "black window"?
dugres
  • 1,875
136
votes
16 answers

Delete all files from a folder and its sub folders

I want to remove all files from a folder structure, so I'm left with an empty folder structure. Can this be achieved in either batch or VBScript scripting? I have tried a very basic batch command, but this required the user to allow the deletion of…
RobN
  • 1,812
  • 5
  • 19
  • 28
105
votes
7 answers

How does the Windows RENAME command interpret wildcards?

How does the Windows RENAME (REN) command interpret wildcards? The built in HELP facility is of no help - it doesn't address wildcards at all. The Microsoft technet XP online help isn't much better. Here is all it has to say regarding…
dbenham
  • 11,794
103
votes
22 answers

How can I delete all files/subfolders in a given folder via the command prompt?

I would like to delete all files and subfolders using a batch file in Windows 7 and keep the top folder. Basically emptying the folder.
Tony_Henrich
  • 12,156
100
votes
10 answers

How to check if a directory exists in Windows?

I would like to translate this Linux/Bash script to Windows shell: if test -d myDirName; then echo "ok"; else mkdir myDirName; fi It tests if a directory exists, and if it doesn't it creates it.
Pietro
  • 1,861
97
votes
6 answers

How can I check a system's current NTP configuration?

I need to verify the current NTP configuration on some Windows systems. Ideally, I'd like to be able to do this via command line instead of navigating through configuration screens. (I'm working on several different OS versions, and configuration…
Iszi
  • 14,163
86
votes
5 answers

How can I remove a forkbomb from the Startup folder?

I have an older laptop running Windows Vista, from about 2005 and I was fooling around with it a little bit. In a moment of sheer stupidity, I created a Batch file and put the following code in it: %0|%0 I ran this and I had to restart my laptop.…
86
votes
9 answers

Remove USB device from command line

I'm constructing a backup script for Windows 7, and the last action I want it to perform is to safely "remove" the USB drive that it is backing up to. I am under the impression that plugging the drive into the same USB port all the time will keep…
Canadian Luke
  • 24,640
71
votes
9 answers

Is it possible to automatically run a batch file as administrator

I am wondering whether it is possible to automatically open a batch file as admin when you double-click on the batch file, because the commands need to be run with administrative rights. Note: I already know about the solution of right-clicking on…
Antp
  • 1,175
66
votes
12 answers

How to prevent the command prompt from closing after execution?

My problem is that in Windows, there are command line windows that close immediately after execution. To solve this, I want the default behavior to be that the window is kept open. Normally, this behavior can be avoided with three methods that come…
Sk8erPeter
  • 1,140
64
votes
5 answers

Shortcut to batch file cannot be pinned to Win10 Start menu

Right-click on the batch file shortcut (as well as batch file itself). Context menu doesn't contain "Pin to Start" command. Is there any way to pin batch file to Windows 10 start menu?
Alex F
  • 975
49
votes
7 answers

In Windows 7, how to change proxy settings from command line?

How do I change proxy settings from command line in Windows 7? I'm not talking about just the http_proxy. I need to set system-wide proxy settings (the ones in Internet properties setting). How do I do that?
prongs
  • 639
49
votes
6 answers

How to extract part of a string in Windows batch file?

Suppose I have a value such as "Abc_123" that's stored in a variable in a batch (cmd) file. I want to end up with a variable that contains only "123". Is there any built-in way to do this? I'm not terribly picky about the method, or performance,…
Ken
  • 635
48
votes
4 answers

how can I make my .bat file continue after an error

I have a .bat file in windows that does three things cmd1 arg1 arg2 cmd2 arg3 cmd3 arg4 arg5 arg6 Sometimes cmd1 can fail and that's fine, I would like to carry on and execute cmd2 and cmd3. But my bat stops at cmd1. How can I avoid this? Update…
1
2 3
99 100