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:

53766 questions
10
votes
7 answers

Command line to recursively delete files but excluding a certain file

I need to delete files of a certain type (.zip files, say) from a folder, and all of its sub-folders, using the command line. Ideally I am looking for something that can be run as a .bat file in Windows. I know there is a /S switch for the DEL…
Tim C
  • 70,053
  • 14
  • 74
  • 93
10
votes
2 answers

Stop MSBuild processing immediately on compilation errors

I have written a batch file, which when executed builds a visual studio solution. The solution comprises of few C# projects. I am using MSBuild utility for this. How can i stop the build from proceeding further when there are compilation errors in…
Ananya
  • 1,503
  • 3
  • 12
  • 8
10
votes
2 answers

Batch file for loop through switch-like arguments?

I'm trying to loop through the arguments that I am passing to a batch file. Based on the argument, I want to set a variable flag true or false for use later in the script So my command is "myscript.bat /u /p /s" And my code is: FOR /f %%a IN ("%*")…
DssTrainer
  • 1,439
  • 3
  • 12
  • 15
10
votes
7 answers

How to create a conda environment shortcut on Windows

With Anaconda installed I got a anaconda base shortcut on Windows startmanu. To open the virtualenv I created (e.g., myenv), I have to click the anaconda base and type in activate myenv in the opened cmd window. How can I create a shortcut to get…
John Wang
  • 4,562
  • 9
  • 37
  • 54
10
votes
5 answers

refresh desktop using batch

I need to refresh the desktop ussing batch is this possible? I have found the following VBscript that refreshes the containing window however, the desktop needs to be refreahed and its not the containing window anyways around this? Set WSHShell =…
philip
  • 345
  • 4
  • 5
  • 9
10
votes
5 answers

Can a python script persistently change a Windows environment variable? (elegantly)

Following on from my previous question, is it possible to make a Python script which persistently changes a Windows environment variable? Changes to os.environ do not persist once the python interpreter terminates. If I were scripting this on UNIX,…
Salim Fadhley
  • 22,020
  • 23
  • 75
  • 102
10
votes
3 answers

How to run bat file in jenkins

My jenkins is installed in C:\Program Files (x86)\Jenkins bat file is located in C:\Users\Admin\workspace\demoWork\run.bat When i run this bat file from cmd everything works fine. But when i try from jenkins executing batch command as mentioned in…
Tatkal
  • 568
  • 4
  • 9
  • 29
10
votes
2 answers

How to rename the files in the path with new different names in batch?

I have one file destination.txt with path information about my CDs: C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\SME99.ISO C:\Users\NekhayenkoO\Desktop\LOG Dateien CD Imaging\Biomasse.iso C:\Users\NekhayenkoO\Desktop\LOG Dateien CD…
Oleg_08
  • 447
  • 1
  • 4
  • 23
10
votes
9 answers

how to start stop tomcat server using CMD?

I set the path for the tomcat and set all variables like JAVA_HOME=C:\Program Files…
user346077
10
votes
3 answers

Create folder in batch script and ignore if it exists

How do I create folder (and any subfolders) in batch script? But the important thing is that, if the folder (or any subfolders) already exists, it should not return error. For example, something like this: mkdir mydir - success(directory is now…
steavy
  • 1,483
  • 6
  • 19
  • 42
10
votes
2 answers

How to start PowerShell script from BAT file with proper Working Directory?

I'm trying to create bat script that can start PowerShell script named the same as bat file in proper working directotry. This is what I got: @ECHO OFF PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile…
Hooch
  • 28,817
  • 29
  • 102
  • 161
10
votes
2 answers

How to get only the user path variable?

Echo %PATH% returns system PATH variable + user PATH variable. How to get only the user PATH variable?
Snap Aoe
  • 145
  • 1
  • 1
  • 11
10
votes
2 answers

Strange redirect in Batch/Powershell polyglot

While attempting to rewrite a batch file, I was looking into the possibility of using powershell. The main issue that I had with it was that .ps1 files aren't executable by default. I found a solution here, which I'll copy below, but I'm very…
Matt Soucy
  • 103
  • 7
10
votes
1 answer

How to call a batch file from another batch file with parameters that contain spaces?

I have 2 (maybe more in future) layers of batch files that are making my life easier up until the point I tried to add paths with spaces in them. Batch file 1: @echo off set thinga=c:\final build set thingb=\\server\deployment for final…
My Other Me
  • 5,007
  • 6
  • 41
  • 48
10
votes
2 answers

Why is ErrorLevel set only after || operator upon a failed redirection?

Upon a failed redirection (due to a non-existent file or insufficient file access), the ErrorLevel value seems not to be set (in the following examples, file test.tmp is write-protected and file test.nil does not exist): >>> (call ) & rem // (reset…
aschipfl
  • 33,626
  • 12
  • 54
  • 99