Windows Batch





Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

Share tech news, updates, or what's on your mind.

Sign up to Post

Remove Software Remotely
This article will show you how you can remove software remotely with the use of PsTools, and how to remotely install another one.
Become a Microsoft Certified Solutions Expert
LVL 13
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and will then append that to the file name.

Expert Comment

by:Mmavr Mmavr
Comment Utility
Thank you very much.
Great Script.
Thank you

Expert Comment

by:guru prasad
Comment Utility
I got error, Incorrect syntax as my database name has 'ver1.0', then i gave databae name [dbversion1.0], then error resolved. Thanks for the post. keep it up.
Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was able to create the below script to complete my task. I have modified the original script as per my requirement & give credit to the creator of this script ( Anonymous ).

For Original script click here...
Below is the modified script:
@echo off

Title autozip.bat

REM updated 30/07/2015
REM created by: Anonymous
REM modified by: Yashwant Vishwakarma

REM This script compresses files in a folder Note: files with the same name 
REM but with different extensions will be in the same archive.

path=%path%;"c:\program files\winrar"

REM ****************** Folder to compress******************
set dir=C:\testzip
REM *******************************************************

REM change to directory
cd %dir%

echo Folder to compress in *.RAR format:
echo %dir%

echo Compressing files started....

REM Compress files in directory individually without subdirectories

FOR %%i IN (*.*) do (
rar a "%%~ni" "%%i"
goto eof

Open in new window

For more commands and syntax follow winrar command line help online

In the original batch file script, there were 7 options which required manual intervention to choose and perform tasks. However, I want to completely automate this task so I modified it and its working absolutely fine for me.

Coding Explanation:

This is what I did with a given script, so I wanted to share my learning and experience with you. I hope it will help someone also when this type of requirement will come.

Keep smiling, rising, shining & stay blessed.
                                                                 forefinger-down-512.png****Kindly vote this if you liked this article by clicking on the 'Vote' button & leave your precious comment !!! *****

Author Comment

by:Yashwant Vishwakarma
Comment Utility
Thank You bfuchs :)
Have a great day ahead :)
Keep smiling n shining always :)
Stay blessed :)
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space.

w60 means Windows Vista or Windows Server 2008.
w61 means Windows 7 or Windows Server 2008 R2.

There are various guides found on the internet on how-to integrate updates into Windows installation media using dism.
This article is a post-integrate how-to to this numerous articles.
This article is addressed to users, who are comfortable using dism to mount, update, manage and unmount Windows Image files for deployment of Windows Systems.
So mounting and dismounting images is intentionally left out.

Administrators often include windows update packages into their deployment images to save time when deploying.
In Windows 8 dism.exe has built-in command-line extensions to remove superseded updates from Windows 8 image files.
Earlier versions of Windows do not have this feature.
So when you integrate packages into a windows installation media (w60 or w61), superseded or outdated packages reside in the deployment image and use unneccessary space. This guide shows how to list and remove superseded packages from the command-line or by batch.

Short description for what you have to do before:
- Download Windows Updates for Windows (w60 or w61).
  Best practice is using wsus offline update.
- Mount Windows installation media.
- Apply packages to the mounted image.

The listings below offers two options.
The first option …
AutoHotkey logo
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to additional resources for EE members who want to learn more.

Expert Comment

Comment Utility
Excellent. Thanks Joe.
LVL 63

Author Comment

by:Joe Winograd, Fellow 2017
Comment Utility
You're welcome, Carlos, and thanks to you for the compliment. If you take a moment to click the thumbs-up icon at the bottom of the article, I''ll appreciate it — as you can see, you'll be endorsement #20. :) Regards, Joe
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the time the update script ran.

When only a few files are in use, you usually go to the "Manage open files" GUI on the server that provides the shared disk to force the files closed and re-run your script.

Now you may have a lot of those files or, even worse, you may want to schedule your script during the night, and you cannot because of those files that are blocked. Immediately, the OPENFILES /disconnect command comes to mind but this function does not allow you to close accessed files or folders in a recursive way. You close one file or one folder at a time OR you do it by username but then you have no more control on which folder you target.

I've looked for a decent workaround on the Internet and found none but complicated or manual procedures.
So I came up with my own piece of batch that fits in just a few couples …
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat file called gcal.bat.  Then you will be able to view the current calendar (default) or specify a month and year, all with a simple command:

  gcal  [month]  [YYYY]


  gcal  July
  gcal  August 2012
  gcal  09 2012
  gcal  10/2012
  gcal  nov .  dec .  jan 2013

Notes:  This is a pure script solution - no additional third party tools or scripts are required.  (EDIT: I now use the wmic program that comes with the OS to get the default month and year in a regionally non-dependant manner, but I left the "pure script" version in for reference, and for use with older OS versions.  See the formatParams section.)

The script is divided into four sections.  The first section (formatParams) validates and formats the input parameters (if any).  For flexibility the script will take various month formats.  The years is always a number from 1-9999.

The second section (getDayList) simply builds a string of numbers to represent the days in the specified month, 1 - 28, 29, 30, or 31 as required.

The third section (getSTARTDOW) actually does the work - determining which day of the week is the first of the month specified.

The fourth section (displayCalendar) displays the …

Expert Comment

by:Oleg Aney
Comment Utility
Good script! I'll be using it in one of my powershell programmes.
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help.

This command will place all the files that have not been used recently into a folder but keep the directory structure in case you need to return any of them.

The first step is to check with the department that this can be done. Check they do not need to keep these files for historical or legal reasons. If they give you the go ahead then the next step is to make sure you have ‘robocopy’; you’ll find it in the Microsoft RKT

Robocopy is a copy/move program like xcopy but with far more powerful command line switches.

What I would like is for it to go through
and check each file for the last time it was used.  If it has not been used after a date I set then move it with its existing folders into a new folder.

To start with I am making the folder outside the folder I am working on. If you made it inside in its final destination then you would get into a loop where it would start to search inside itself and move its files into itself (not good).

So here is the command :

robocopy /s /MOVE /MINAGE:20100101 /r:1 /w:1 "P:\FINANCE-FOLDER" "P:\Archive"

Open in new window

/s = Hunt down through the folders.  You could use /e to also move empty ones and create a copy of the full structure.
LVL 15

Administrative Comment

by:Eric AKA Netminder
Comment Utility

Your article has been published.

Page Editor
Mouse Trap
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some basics I will create a tutorial to be published here very soon (reference will be posted here).

It's an Interpreter
If using complex mechanisms like subshelling (using commands enclosed in round brackets), keep in mind batch files are not really like using a programming language - batch language isn't that accurate, there are several flaws in the interpreter we have to take care of.

As  cmd.exe  being an interpreter, it reads line after line, and does some pattern replacing in advance to executing the line.
A "line" can be
* a single physical line
* several physical lines connected by a caret (^) at the end of each line
* several physical lines enclosed in round brackets

A single line is hence:
@echo off

Open in new window

if 1 == 1 (echo yes) else (echo no)

Open in new window

echo Writing a long ^
text here

Open in new window

if 1 == 1 (
  echo yes
) else (
  echo no

Open in new window

Why should one know? As said already, it's an interpreter we use, and it's applying some string replacements.
If you are using environment variables, which is one of the things you will use every time, this gets most important.
E.g. the following code will not work as expected:
set example=1
if %example% == 1 (
  set example=2
  echo %example%
REM result:   1
REM expected: 2

Open in new window

In the first line, variable  example  is set to 1. In the next "line", containing  IF  up to the closing bracket, each occurance of  %example%
LVL 75

Author Comment

Comment Utility
Thanks, footech. I have introduced a new line 2 now, so the reference is correct ;-).
LVL 11

Expert Comment

by:Lofty Worm
Comment Utility
very nice, going in my DOS batch links library!

Windows Batch





Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.