Link to home
Start Free TrialLog in
Avatar of psych0naut
psych0naut

asked on

Automate Scandisk & Defrag via Script

I would like to have a script (or in this case perhaps scripts) which will automatically scandisk /F and defrag users' hard drives every week.  Thx in advance!

-Check "C:\Windows\Scripts" for the name of the defrag/scandisk script.
-If present, schedule the script to run via windows task scheduler every Friday at Midnight.  
-If absent, create the folder in question, copy the script there, and then schedule as above.

Also, I would obviously not want the script to schedule another instance of the scan/defrag script so perhaps we would need to have the aforementioned script check to see if scan/defrag is already scheduled before adding it??
Avatar of ColinRoyds
ColinRoyds

This could be done using the "at" command to call a script which would initiate a scandisk and defrag at a specified time and interval  


C:\>at/?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
    [ /EVERY:date[,...] | /NEXT:date[,...]] "command"

\\computername     Specifies a remote computer. Commands are scheduled on the
                   local computer if this parameter is omitted.
id                 Is an identification number assigned to a scheduled
                   command.
/delete            Cancels a scheduled command. If id is omitted, all the
                   scheduled commands on the computer are canceled.
/yes               Used with cancel all jobs command when no further
                   confirmation is desired.
time               Specifies the time when command is to run.
/interactive       Allows the job to interact with the desktop of the user
                   who is logged on at the time the job runs.
/every:date[,...]  Runs the command on each specified day(s) of the week or
                   month. If date is omitted, the current day of the month
                   is assumed.
/next:date[,...]   Runs the specified command on the next occurrence of the
                   day (for example, next Thursday).  If date is omitted, the
                   current day of the month is assumed.
"command"          Is the Windows NT command, or batch program to be run.


C:\>
or you can use the schtasks util


C:\schtasks /?

SCHTASKS /parameter [arguments]

Description:
    Enables an administrator to create, delete, query, change, run and
    end scheduled tasks on a local or remote system. Replaces AT.exe.

Parameter List:
    /Create         Creates a new scheduled task.

    /Delete         Deletes the scheduled task(s).

    /Query          Displays all scheduled tasks.

    /Change         Changes the properties of scheduled task.

    /Run            Runs the scheduled task immediately.

    /End            Stops the currently running scheduled task.

    /?              Displays this help/usage.

Examples:
    SCHTASKS
    SCHTASKS /?
    SCHTASKS /Run /?
    SCHTASKS /End /?
    SCHTASKS /Create /?
    SCHTASKS /Delete /?
    SCHTASKS /Query  /?
    SCHTASKS /Change /?

this is how to make schtasks run  under win 2k http://www.winnetmag.com/Article/ArticleID/25186/25186.html
Avatar of Lee W, MVP
The built in Windows Defrag was NOT intended to be scripted.  BUT, there are ways around this.  NOTE, for a FAR more reliable config, you really are better buying a retail defrag product.  That said, read here for a VBS script that can invoke defragmentation.
ah but you can use defrag from the cmd line
C:\>defrag /?
Usage:
defrag <volume> [-a] [-f] [-v] [-?]
  volume  drive letter or mount point (d: or d:\vol\mountpoint)
  -a      Analyze only
  -f      Force defragmentation even if free space is low
  -v      Verbose output
  -?      Display this help text

C:\>
Colin - what version of Windows are you using?  I just entered "defrag /?" on a Windows 2000 system and got "'defrag' is not recognized as an internal or external command, operable program or batch file."
The defrag program in XP is contained in a management console that cannot be scripted.
Leew, windows XP SP1, the defrag cmd was run via cmd.exe .
You are right just checked no defrag cmd for Windows 2000or NT but:

For you NT4.0 and Win2k users there is a free defrag utility made by SysInternals.com called Contig. It will run on NT4.0 and newer but not any of the Win9X platforms. With it you can be more grangular in the files you want defragged instead of just specifying a drive letter.

contig -a -s *.*
Contig will analyse all files and subfolders on the current harddrive and display a fragmentation summary.



contig -s *.*
Contig will defragment all files and subfolders on the current harddrive.

http://www.sysinternals.com/ntw2k/freeware/contig.shtml

pos this question in programming channel there are lots of experts there who would give their professional advices
Avatar of psych0naut

ASKER

I found the following batch file on TechRepublic:

********************************************************************************
REM chkdsk and defrag automation
for /F "eol= tokens=1 delims=( " %%i in (DrvLtr.txt) do set DrvLtr=%%i& call :dsKchk
 
:dsKchk
If %DrvLtr% == end goto :eof
chkdsk %DrvLtr%
If not errorlevel 3 goto :defrag
If not exist %DrvLtr%\winnt If not exist %DrvLtr%\windows If not exist %DrvLtr%\pagefile.sys goto :dskchkon
 
:dskchkoff
cd\
%DrvLtr%
echo Y chkdsk /F /R
goto :defrag
 
:dskchkon
chkdsk %DrvLtr% /F /R
 
:defrag
cd\
%DrvLtr%
defrag %DrvLtr% -b
defrag %DrvLtr%
:EOF
********************************************************************************

This is placed in a directory along with a text file called "DrvLtr.txt" which lists which drives the operations will be performed on as follows:


********************************************************************************
C:
D:
end
********************************************************************************

The problem is, if run that from the server it will want to perform the operations on the C and D drive of the server.  How can I edit this so that it performs the actions on each individual workstation?  Alternately, can any of you make a script or batch file for me which can be run on users logon script and perform the following action:  Check a particular location for presence of the batch file and copy it there if it's not present.  The second step would be to schedule the batch file via windows scheduler.  Whaddyathink?

Thx in advanc
ASKER CERTIFIED SOLUTION
Avatar of ColinRoyds
ColinRoyds

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial