Home > Windows Server Tips > Windows Systems Management and Administration > Create batch file to log chkdsk results
Windows Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

WINDOWS SYSTEMS MANAGEMENT AND ADMINISTRATION

Create batch file to log chkdsk results


Brien M. Posey, Contributor
07.25.2007
Rating: -5.00- (out of 5)


Expert advice on Windows-based systems and hardware
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


More on CHKDSK

Learn how to use CHKDSK to detect file and folder corruption.

Visit our resource center to find more information on using  CHKDSK and other storage management tools and utilities.

You can use the AT command in Windows to schedule chkdsk to automatically run on a daily basis. The problem with this technique is that chkdsk results are quickly flashed on the screen, then disappear. If you're not sitting at the server console when chkdsk runs, you won't ever see the scan results.

A better solution would be to create a batch file that logs the chkdsk results, so you can read those results at your leisure. I've used the following command to automate chkdsk:

AT \\Server1 3:00 /Interactive /every:Friday CHKDSK C:

This command runs chkdsk every Friday at 3:00 a.m. The command is set up to call chkdsk directly. But if you want to log the results, you'll need to tweak this command so that rather than calling chkdsk directly, it calls a batch file that in turn calls chkdsk.

You can name the batch file anything you want. Here we'll call it DRIVSCAN.BAT. Now we'll alter the AT command so it calls DRIVSCAN.BAT instead of chkdsk. The new command will look like this:

AT \\Server1 3:00 /Interactive /every:Friday DRIVSCAN.BAT

Now that we've changed our AT command to call a batch file, we need to create a batch file named DRIVSCAN.BAT. There are several methods for creating batch files; the easiest to use is probably Notepad. When you save your batch file, you must save it to a folder in the system path so the AT command can find it.

Exporting the results of a chkdsk to a log file is simple. In a command-line environment, placing the greater than (>) symbol at the end of a command tells Windows to export the command's results. You can complete the process by following the > sign with the filename you want the command results sent to. For example, if you wanted the chkdsk results exported to a file named SCAN.LOG, you'd use the following command:

CHKDSK > SCAN.LOG

You could use the command shown above as the only command in the DRIVSCAN.BAT file, and the batch file would work fine. The problem is you've told Windows (through the AT command) that you want to run the batch file every Friday at 3:00 a.m. The first time the command runs, the SCAN.LOG file will be created. But the second time the batch file runs, the chkdsk results will be appended to the end of the log file. In some environments, this behavior might be perfectly acceptable, but I prefer to place each set of chkdsk results in its own unique log file. This means that each time the batch file runs, it must generate a unique filename.

The easiest way to accomplish this is to base the filename on the date. The following command will help us create a filename based on the system date. The command takes the date, puts it in MMDDYYYY format and appends the .LOG extension. The date with the .LOG extension is assigned to a variable named datefile. For example, on July 1, 2007 the datefile variable would contain: 07012007.LOG

The command for setting up the datefile variable is:

for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log

If you want to view the contents of the datefile variable, you can do so by using the echo command. Since datefile is a variable, you must place percentage signs on each end of it any time you reference it. For example, the command for viewing the contents of this variable would be:

Echo %datefile%

But we're not interested in merely viewing the contents of the datefile variable. Our goal is to use the variable's contents as a filename, and to export the chkdsk results to the file that is referenced by the datefile variable. To do so, use this command:

Chkdsk > %datefile%

The batch file in its entirety would look like this:

for /F "tokens=2,3,4 delims=/ " %%i in ('date /t') do set datefile=%%i%%j%%k.log Chkdsk > %datefile%

You could easily add a file system path to the batch file if you wanted to place the log files in a specific folder. There are several ways to do this, but the easiest is to include the path information as a part of the chkdsk command. Here's an example of the chkdsk command with a path added below:


Chkdsk > c:\logs\%datefile%

In this article I've shown you how to log the results of each chkdsk scan. However, you still have to go to each individual server to read the logs. My next article will show you how to modify the batch file we've created so that the server will automatically email you the scan results.

About the author: Brien M. Posey, MCSE, is a Microsoft Most Valuable Professional for his work with Windows 2000 Server, Exchange Server and IIS. He has served as CIO for a nationwide chain of hospitals and was once in charge of IT security for Fort Knox. He writes regularly for SearchWinComputing.com and other TechTarget sites.

Rate this Tip
To rate tips, you must be a member of SearchWindowsServer.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Windows Data Storage Administration Tools
eZines and eBooks for Windows server professionals
Windows scripting secrets for disk quota management
Top 10 Windows storage management tips of 2007
Exploring the Windows Server 2003 Resource Kit: Compress.exe and Expand.exe
How to use the g4u network-based hard disk cloning utility
When to use third-party tools in place of NTBACKUP
CDBurnerXP 4.0: A CD/DVD/ISO freeware tool for Windows
Data backup utility mkisofs uses UDF disk-image format
Create custom boot sector manually with Boot Build utility
Unstoppable Copier recovers data from bad files by ignoring errors

Windows Systems and Network Management Tools and Techniques
A first look at Internet Information Services 7.0
Windows registry hack improves offline file access for mobile users
Reducing the size of network backups in Windows
Monitor network bandwidth with CyberGauge
How to format NTFS: More tricks to improve file system performance
Key enhancements to SCCM give admins more control over assets, licensing
Archiving information with New-Item in Windows PowerShell
The new Microsoft System Center: What happened to SMS and MOM?
New Russinovich tool scans for open file references from command line
Identify file extension types with TrID

Windows Systems Management and Administration
Tips for Windows domain controller optimization
Quick hits: Troubleshooting service account failure, batch job execution
Case Study: Troubleshooting Windows service dependency failures
Troubleshooting common Windows service failures
How to format NTFS: More tricks to improve file system performance
Key enhancements to SCCM give admins more control over assets, licensing
Windows scripting secrets for disk quota management
Optimizing NTFS file system performance
The new Microsoft System Center: What happened to SMS and MOM?
New Russinovich tool scans for open file references from command line

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2004 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts