Archive for the 'Batch File Examples' Category

Run a specified program only on certain days at boot time.

This demo makes use of two tricks: It reads a custom INI file, and it puts the current day into the environment.
@echo off
:: This batch file allows you to run programs on certain days.
:: Have this TODAY.BAT program CALLed from your AUTOEXEC.BAT.
:: TODAY.BAT will read entries in a TODAY.INI file you must make.
:: The entries [...]

Have your batch file to anser “Do you really want to do this?” prompt.

If you find yourself trying to automate the deletion of files, the formatting of floppies, or do anything else REALLY INTERESTING, you’re going to hit a spot where everything stops and you get asked “Are you sure?”. Sometimes you can pass a “/y” on the command line (like the COPY command) to override this, but [...]

Get user input from batch file

Sure, you’ve done it, but can you do it without having to hit a Ctrl-Z, without ANSI, without a debug script, and without a separate “set” file?
rem  This batch file gets a character or word of user input and
rem  returns it in the environment variable VALUE. Two tricks are
rem  used to accomplish this:
rem
rem  (1) The [...]

Batch file to Stop/Start Service

I would like to restart a service by using a service account in batch. The batch file will need to read over 300 file services from a text file and stop the service. A second file will start the service in the same manner.
You can do this using a combination of for /f and “net [...]

DOS Batch FTP - Simple - FTP script and batch in a single file

Embed FTP script into a batch script. Add this line at the beginning of the FTP script:
@ftp -i -s:”%~f0″&GOTO:EOF
The “FTP -s:ftpscript.txt” option executes a FTP script wheres “%~f0″ resolved to the name of the running batch file. “GOTO:EOF” ends the batch script and makes sure the FTP script doesn`t run as part of the [...]

OSQL.EXE - Run SQL script from DOS Batch - SQL script and dos batch script in one file, the One-File Solution

Embedding SQL script within a batch script is just as easy. The following batch script executes itself in SQL context. The trick is the GOTO command in the first line of the script. When executing GOTO START in batch context than the command processor will jump to the label “:START” and [...]

DOS Batch - Find and Replace - Search a file and replace all occurrences of a string with another string

This batch allows string substitution in a text file. It parses each line of a text file for a particular string and replaces it with another string.
I.e. To replace all occurrences of “Yellow Submarine” in “color.txt” with “uboot” and put the output on the screen run:
BatchSubstitute.bat “Yellow Submarine” uboot color.txt
Or
type color.txt|BatchSubstitute.bat “Yellow Submarine” uboot
Optionally pipe [...]

DOS Batch File - Progress - Show progress in batch using the title bar

Showing progress in the output window seems impractical in DOS batch, since there is no way to overwrite a previews output for an updated progress status during each progress tick. Using the ECHO command is not nice, screen content quickly scrolls out of sight. A practicable alternative may be to use the window TITLE for [...]

DOS Batch FTP - Get New Files Only - Ftp script to download only files that don`t exist in local folder

This batch connects twice to the FTP server. First time it retrieves a list of files on the FTP server. This list is being trimmed to contain only files that don`t already exist locally. The files in the trimmed list are then downloaded during a second connection.  Note: Since all files are passed into the [...]