Top 10 DOS Batch tips (Yes, DOS Batch...)

 

PowerShell's great. I'm fired up about the opportunity to use .NET objects from simple scripts. I'll admit I'm still getting up to speed with it, but I'm totally sold on PowerShell.

However, it's not installed on a lot of servers I work with, and I still do a lot of my "clumsy developer attempting DBA and network admin" tasks from DOS Batch files. You can do quite a bit with DOS Batch - the silly fact is the my most popular posts (by a huge margin) are some batch scripts to allow running IE7 and IE6 on the same computer.

So, by way of tribute to the dying art of the DOS Batch file, I present my top ten batch file tricks:

  1. Use PUSHD / POPD to change directories
    Read Scott Hanselman's writeup on PUSHD. The basic idea is that it keeps a stack, so at the simplest level you can do something like this:
    PUSHD "C:\Working Directory\" ::DO SOME WORK POPD

    That allows you to call the batch file from any directory and return to the original directory when you're done. The cool thing is that PUSHD can be nested, so you can move all over the place within your scripts and just POPD your way out when you're done.
  2. Call FTP scripts
    This sample prompts for the username and password, but they can of course be hardcoded if you're feeling lucky.
    set FTPADDRESS=ftp.myserver.com set SITEBACKUPFILE=FileToTransfer.zip set /p FTPUSERNAME=Enter FTP User Name: set /p FTPPASSWORD=Enter FTP Password: CLS > script.ftp USER >>script.ftp ECHO %FTPUSERNAME% >>script.ftp ECHO %FTPPASSWORD% >>script.ftp ECHO binary >>script.ftp ECHO prompt n :: Use put instead of get to upload the file >>script.ftp ECHO get %SITEBACKUPFILE% >>script.ftp ECHO bye FTP -v -s:script.ftp %FTPADDRESS% TYPE NUL >script.ftp DEL script.ftp
  3. Read from the registry
    You can make creative use of the FOR command to read from and parse a registry value (see my previous post for more info).
    FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" /v SQL2005') DO SET SQLINSTANCE=%%B
  4. Run SQL Commands
    You can call OSQL (or SQLCMD on servers with SQL 2005 installed) to execute SQL commands:
    osql -E -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'D:\DataBase\Backups\%DATABASENAME%_backup' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT"
  5. Check if a file or folder exists
    I used this to do a quick and dirty check to see if a Windows Hotfix had been installed in my IE7 Standalone scripts:
    IF EXIST %SystemRoot%\$NtUninstallKB915865$\ GOTO KB_INSTALLED ECHO Installing Hotfix (KB915865) to allow tab support START /D "%~dp0/Installation/Update/" xmllitesetup.exe
  6. Pause execution for a number of seconds
    There are different ways to do this from within a batch file, all with their tradeoffs. I use a ping to an invalid IP address with a timeout. The best way to do this is to find an invalid IP address and then pint it, but 1.1.1.1 is a pretty safe bet:
    ECHO Waiting 15 seconds PING 1.1.1.1 -n 1 -w 15000 > NUL
  7. Use defaults for optional parameters
    It's not really easy to check for a missing parameter. You have to use something like "IF dummy==%1dummy", which will only be true if %1 is empty. So, for example, here we're allowing a user to supply an application path via the third parameter, and defaulting it if it's missing. By the way, beware the IF syntax. The line spacing makes a difference, so this is one that I just copy and paste to avoid figuring it out every time.
    IF dummy==dummy%3 ( SET APPLICATIONPATH="C:\Program Files\MyApp\" ) ELSE ( SET APPLICATIONPATH = %3 )
  8. Process each file matching a pattern in a directory
    I previously posted a script which iterates all files named *.bak in a directory and restores them on the local instance of SQL Server. Here's an excerpt:
    PUSHD %BACKUPDIRECTORY% FOR %%A in (*.bak) do CALL :Subroutine %%A POPD GOTO:EOF :Subroutine set DBNAME=%~n1 ::RUN SOME OSQL COMMANDS TO RESTORE THE BACKUP GOTO:EOF
  9. Use batch parameter expansion to avoid parsing file or directory info
    Batch file parameters are read as %1, %2, etc. DOS Command Extensions - available on Windows 2000 and up - add a lot of automatic parsing and expansion that really simplifies reading filenames passed in as parameters. I originally put this at the top of the list, but I moved it because I figured the insane syntax would drive people off. I wrote a simple batch script that shows some examples. I think that makes it a little more readable. Stick with me, I think this is one of the best features in DOS batch and is worth learning.

    First, here's the batch file which just echos the processed parameters:
  10. @echo off echo %%~1 = %~1 echo %%~f1 = %~f1 echo %%~d1 = %~d1 echo %%~p1 = %~p1 echo %%~n1 = %~n1 echo %%~x1 = %~x1 echo %%~s1 = %~s1 echo %%~a1 = %~a1 echo %%~t1 = %~t1 echo %%~z1 = %~z1 echo %%~$PATHATH:1 = %~$PATHATH:1 echo %%~dp1 = %~dp1 echo %%~nx1 = %~nx1 echo %%~dp$PATH:1 = %~dp$PATH:1 echo %%~ftza1 = %~ftza1



    Now we'll call it, passing in "C:\Windows\Notepad.exe" as a parameter:
    C:\Temp>batchparams.bat c:\windows\notepad.exe %~1 = c:\windows\notepad.exe %~f1 = c:\WINDOWS\NOTEPAD.EXE %~d1 = c: %~p1 = \WINDOWS\ %~n1 = NOTEPAD %~x1 = .EXE %~s1 = c:\WINDOWS\NOTEPAD.EXE %~a1 = --a------ %~t1 = 08/25/2005 01:50 AM %~z1 = 17920 %~$PATHATH:1 = %~dp1 = c:\WINDOWS\ %~nx1 = NOTEPAD.EXE %~dp$PATH:1 = c:\WINDOWS\ %~ftza1 = --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE



    As I said, the syntax is completely crazy, but it's easy to look them up - just type HELP CALL at a DOS prompt; it gives you this:

    %~1 - expands %1 removing any surrounding quotes (")
    %~f1 - expands %1 to a fully qualified path name
    %~d1 - expands %1 to a drive letter only
    %~p1 - expands %1 to a path only
    %~n1 - expands %1 to a file name only
    %~x1 - expands %1 to a file extension only
    %~s1 - expanded path contains short names only
    %~a1 - expands %1 to file attributes
    %~t1 - expands %1 to date/time of file
    %~z1 - expands %1 to size of file
    %~$PATH:1 - searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string

    The modifiers can be combined to get compound results:

    %~dp1 - expands %1 to a drive letter and path only
    %~nx1 - expands %1 to a file name and extension only
    %~dp$PATH:1 - searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.
    %~ftza1 - expands %1 to a DIR like output line

    In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid argument number. The %~ modifiers may not be used with %*

  11. Learn from the masters
    By far, my favorite resource for DOS Batch trickery is the Batch Files section of Rob van der Woude's Scripting Pages. He's got some good PowerShell resources, too.

What about you? Got any favorite DOS Batch tricks?

Published Monday, November 20, 2006 12:43 AM by Jon Galloway

Comments

# Jon Rowett’s Workblog » Links for 20 November 2006

Monday, November 20, 2006 10:07 AM by Jon Rowett’s Workblog » Links for 20 November 2006

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

PUSHD and POPD are great.  An additional advantage over CD is the fact that including a drive letter changes to the drive and directory specified whereas CD just changes the active directory on the specified drive.

If you use them a lot you can save a fair bit of typing by defining two one-line command files +.cmd and -.cmd somewhere on your path.  They simply contain;

+.cmd

 @pushd %1

-.cmd

 @popd

Now you can just run + <dir> to enter a directory and - to return.

Monday, November 20, 2006 10:42 AM by Steve Crane

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

one more that I just recalled, from reading Steve's comment

cd /d <driveletter:\directory>

changes both, the directory and the drive letter

Monday, November 20, 2006 12:25 PM by Eber Irigoyen

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Very good!

And this tip: "Pause execution for a number of seconds" Gosh! Why didn't I think of it before?

Thanks for all these tips!

[]'s

Cleydson

Tuesday, November 21, 2006 11:07 AM by Cleydson

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

sleep 15 seconds:

sleep 15

Wednesday, November 29, 2006 1:25 PM by oscar

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

@oscar - The sleep command isn't available on a default install. It's part of the Windows 2003 Resource Kit, which is a separate download.

Wednesday, November 29, 2006 2:08 PM by Jon Galloway

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

I love using the findstr.exe that is built into Windows XP and later... it's closer to "grep" than the original "find" and the built in ability to search subdirectories is a real gift.

Andrew.

Sunday, February 25, 2007 7:07 PM by Andrew from Vancouver

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Tip #8 "Use batch parameter expansion to avoid parsing file or directory info" was just what I was looking for!

So what if the syntax is "insane"? (No more so, than, say, printf formatting syntax in C.)

Anyway it's easy to copy/paste what I want from your page, or, as you taught me, from CALL HELP at DOS command prompt.

I think it should be tip #1 again ...

Wednesday, April 18, 2007 4:11 PM by Tom Harris

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Oops. I meant

Tip #9 "Use batch parameter expansion to avoid parsing file or directory info" was just what I was looking for!

Thursday, April 19, 2007 7:26 AM by Tom Harris

# Rhonda Tipton&#8217;s WebLog Web Links 11.21.2006 &laquo;

Pingback from  Rhonda Tipton&#8217;s WebLog Web Links 11.21.2006 &laquo;

Wednesday, May 16, 2007 11:18 PM by Rhonda Tipton’s WebLog Web Links 11.21.2006 «

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

how do i put a automatic yes in (Y/N)option

Wednesday, January 16, 2008 3:38 PM by filipo111

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

@filipo111

echo N | del .

Thursday, January 24, 2008 8:10 AM by Alexander

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Great tips!  

How do I use xcopy to search for and copy say, word files, scatterded over a drive.  Also if none present then move on to next command.  Right now I have:

X:\XCOPY.exe C:\*.doc "X:\Word Files" /s/e/v/i/y/h

This command string copies the file directory structure which takes forever and something I do not want.  Just looking to pin point files (ie word or microsoft money - *.mny) and copy them.

Thanks,  Matt

mstanchi@yahoo.com

Friday, February 22, 2008 9:04 PM by Matt

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Have SQL script and DOS batch script in same file:

www.dostips.com/DtCodeInterfacing.php

Monday, March 17, 2008 12:57 AM by dosrocks

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Pause for x number of seconds:

ping -n x localhost

Wednesday, April 09, 2008 2:05 PM by Todd Carey

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

Hi,

I'm trying to automatically run a .reg file through a batch file, but need to find out how the "Yes/No" and "OK" pop ups can be avoided and automatically accepted so the user doesn't have to click on Yes and OK everytime.

example of my batchfile; let's say the .bat file is located on the X-drive:

x:

Permission_Required_0.reg

Thanks in advance, if anybody is able to help me out!

Dom

Wednesday, April 16, 2008 4:01 AM by Dom Peeters

# Resell Rights

Go to Port Sarim, west of Draynor village, and ask one of the people in the blue uniforms to go to Karamja and pay them 30 runescape gp. When you get there, walk off the dock and you should see a house. Enter the house and talk to a man named Luthas.

Wednesday, May 14, 2008 9:20 AM by Resell Rights

# business income opportunity xango

I have one and it is“ COMPLETE RUBBISH” ok not complete it looks good. Other than that almost every other phone I have had in the last 3 years and I have had 3, last being the blackberry perl, has done a better job than the iphone. you cant even sync

Thursday, June 26, 2008 7:35 PM by business income opportunity xango

# re: Top 10 DOS Batch tips (Yes, DOS Batch...)

hey to make an alarm with a batch file juss put:

set /a test=60

ech %test%

set /a test=%test%-1

echop %test%

repeat this several time and keep a sstop watch handy.

james

Wednesday, July 09, 2008 6:46 PM by jimmy

Leave a Comment

(required) 
(required) 
(optional)
(required)