MSDOS Introduction
Copyright B Brown, 1988-2000. All rights reserved.

Home Page

prev page next page


MSDOS Batch File Examples

not display commands as they are executed


	echo  off


to stop a single command from being displayed when executed


	@echo  off


to insert comments into batch files


	rem  display1


to display an argument passed to batch file


	rem display the first argument
	echo  %1


;but what if no argument is passed, so lets add a test to check argument and if present, display it


	rem  display and check if there is an argument
	if  "%1"==""  goto  noparameter
	echo  the  parameter  is  %1
	goto  exit
	:noparameter
	echo  you  must  supply  an  argument  to  this  batch  file
	:exit


display all names of files in current directory


	@echo  off
	rem   list   files  in  the  current  directory
	for  %%f  in  ( *.*)   do   echo   %%f


test to see if a file exists


	@echo  off
	rem   check  to  see  if  a  file  exists
	if  exist  %1  goto  fileexists
	goto  nofile
	:fileexists
	echo  the  file  %1  exists
	rem  other  commands  here .....
	goto  end
	:nofile
	echo  the  file  %1  does  not  exist
	goto  end
	:end


check whether a batch file command executed okay


	@echo  off
	rem  this  batch  file  runs  the  next  command  "format"
	rem  if  this  command  was  successful,  it  sets  the  MSDOS  variable
	rem  ErrorLevel  to  0,  else if  it  fails,  to  a  non-zero  value
	echo   Insert  new  disk  in  drive  A:
	pause
	format  a:
	if  errorlevel  1  goto  error
	echo  disk  was  formatted  okay
	goto  end
	:error
	echo  format  was  unsuccessful
	goto  end
	:end


format a drive passed an argument to batch file


	@echo  off
	rem  this  batch  file  runs  the  next  command  "format"
	rem  if  this  command  was  successful,  it  sets  the  MSDOS  variable
	rem  ErrorLevel  to  0,  else if  it  fails,  to  a  non-zero  value
	rem  the  drive  to  format  is  passed  as  the  first  argument
	echo   Insert  new  disk  in  drive  %1
	pause
	format  %1
	if  errorlevel  1  goto  error
	echo  disk  in  drive  %1  was  formatted  okay
	goto  end
	:error
	echo  format  of  disk  in  drive  %1  was  unsuccessful
	goto  end
	:end


format a drive passed an argument to batch file and check for the drive argument


	@echo  off
	rem  this  batch  file  runs  the  next  command  "format"
	rem  if  this  command  was  successful,  it  sets  the  MSDOS  variable
	rem  ErrorLevel  to  0,  else if  it  fails,  to  a  non-zero  value
	rem  the  drive  to  format  is  passed  as  the  first  argument
	rem  and checks to make sure the drive argument is specified
	if  "%1"==""  goto  nodrive
	echo   Insert  new  disk  in  drive  %1
	pause
	format  %1
	if  errorlevel  1  goto  error
	echo  disk  in  drive  %1  was  formatted  okay
	goto  end
	:error
	echo  format  of  disk  in  drive  %1  was  unsuccessful
	goto  end
	:nodrive
	echo  You  must  specify  a  drive  to  format
	goto  end
	:end


format a drive passed an argument to batch file and check for the drive argument but dont format drive c:


	@echo  off
	rem  this  batch  file  runs  the  next  command  "format"
	rem  if  this  command  was  successful,  it  sets  the  MSDOS  variable
	rem  ErrorLevel  to  0,  else if  it  fails,  to  a  non-zero  value
	rem  the  drive  to  format  is  passed  as  the  first  argument
	rem  and checks to make sure the drive argument is specified
	if  "%1"==""  goto  nodrive
	if  "%1"=="C:"  goto  drivec
	echo   Insert  new  disk  in  drive  %1
	pause
	format  %1
	if  errorlevel  1  goto  error
	echo  disk  in  drive  %1  was  formatted  okay
	goto  end
	:error
	echo  format  of  disk  in  drive  %1  was  unsuccessful
	goto  end
	:nodrive
	echo  You  must  specify  a  drive  to  format
	goto  end
	:drivec
	echo  You  cannot  format  the  hard  disk
	goto  end
	:end