Friday, January 28, 2011

Making a Batch File For Disk Management


batch disk management
In previous tutorial I showed you how to create batch file for doing multiple tasks. Now in this tutorial we will create a simple batch utility file for disk management, so that the following tasks can be done.


  1. Locating bad sectors .

  2. Changing file system from FAT32 to NTFS.

  3. Disk Defragmentation.

Put the following code in batch file to do all the above tasks:


@echo off
echo MENU
color 47
echo 1.Check for Bad Sectors.
echo 2.Change File system from FAT32 to NTFS.
echo 3.Disk Defragmentation.
set /p choice=Enter Your Choice 1, 2, 3:
if %choice%==1 goto 1
if %choice%==2 goto 2
if %choice%==3 goto 3
:1
cls
set /p choice=Enter the Letter of the drive which you want to be inspected:
set /p choice1=Would you like to fix if errors are located? (Y/N):

if /i %choice1%==Y (
chkdsk %choice%: /r /f
) else (
chkdsk %choice%: /r
)
exit
:2
cls
set /p choice=Enter the Letter of the drive which you want to be changed to NTFS:
convert %choice%: /FS:NTFS
exit
:3
cls
set /p choice=Enter the Letter of the drive which you want to be defragment:
set /p choice1=Do you want to analyze only? (Y/N):
if /i %choice1%==Y (
defrag %choice%: -a
) else (
defrag %choice%:
)


Note: After converting your file system to NTFS you will not be able to convert it back to FAT32 without formatting.

Explanation:

You will see that the above code has been divided into 3 separate blocks. Each block has its own condition for execution.
For example if user enters 3 as choice, the condition for block 3 code will becomes true. So the code under 3 will be executed.
Now lets lets look at block 3 code:

cls
set /p choice=Enter the name of drive which you want to be defragment:
set /p choice1=Do you want to analyze only? (Y/N):
if /i %choice1%==Y (
      defrag %choice%: -a
  ) else (
      defrag %choice%:
  )

First command we used is cls which is used to clear screen, we used this command so that the screen doesn't look messy.

After that we have set the two variables “choice” and “choice1” by using “set” command to store information entered by the user.

And for doing conditional processing we used “If else” statement along with “I” switch, which is for doing case intensive string compare.


For example if user enters "Y" as choice it will be look like this:


                if /i Y==Y (condition is true)

So the code following "if" statement will be executed Which is:

                 defrag D: -a (here it is assumed that user entered “D” as drive letter)


     Now assume if user enters “N” as a choice it will look like this:


                 if /i N==Y (As N is not equal to Y, condition is false)

So the code following “else” will be executed which is


               defrag D: (without “–a” switch, which is for analyze only)

To get more information about particular command, type following in command prompt:
           commandname/?  i.e: set/?

Share this

0 Comment to "Making a Batch File For Disk Management"

Post a Comment