Windows Daily Reset

What I’ve seen in most corporate environments is that PCs are up all the time; at the end of the day people either just log off or lock the machine.  You can take advantage of this to reset Windows every night for best performance the next day.  I’ve found three things make for a useful reset:

  1. defragmenting the hard drives
  2. booting the machine
  3. defragmenting the system files that are skipped in step 1

This can be done by modifying the ScheduledDefrag task that comes with later versions of Windows or creating such as task in XP and 2003.  In Vista, you’ll find Microsoft’s task in the Scheduled Tasks snap-in at Task Scheduler Library –> Microsoft –> Windows –> Defrag –> ScheduledDefrag.  The first step is to change it to a daily schedule and make sure it’s enabled:

image

Next, the action Microsoft supplies should be changed.  Microsoft’s action is "%windir%\system32\defrag.exe –c –i" (Vista).  -c is for all drives, but what is –i?  It’s undocumented but defrag is happy to accept it.  In testing, it seemed to cause defrag to run much slower, and my guess it’s i for idle, in other words, run only when the machine is idle.  So, I’d like to change the action to "%windir%\system32\defrag.exe –c –v > c:\Logs\defrag.log 2>&1", but that doesn’t work because the scheduler calls CreateProcess, which doesn’t interpret the redirection request.  If you run the command using the shell the redirection will work.  In that case Program/script is set to CMD Add arguments (optional) is set to "/c %windir%\system32\defrag.exe g: -v > c:\Logs\defrag.log 2>&1" (without the quotes).

image

While this works, it’s not my preferred  solution since all actions will require this clumsy setup with CMD.  Instead, write a DOS script with all the work, in which case redirection works as it should:

image

We’ll take a look at the DOS script after a couple of more adjustment to Microsoft’s task.  Waiting for the machine to go idle is unnecessary, so remove that:

image

Having the task run as soon as possible if the scheduled time is missed is unnecessary and causes it to run on boot or after resuming from hibernation.  Clear Run task as soon as possible after a scheduled start is missed:

image

The script simply does the defrag and a reboot:

date /t
time /t
%windir%\system32\defrag.exe -c -v
date /t
time /t
shutdown /r /d P:4:1 /t 300 /c "daily automated reboot"

The shutdown command says to restart the machine (/r), that this is a planned shutdown (/d P:4:1), to wait 300 seconds before performing the shutdown (/t 300), and to enter a comment in the event log (/c “daily automated reboot”).  If you happen to be logged in when this runs, you be informed that a shutdown is about to take place.  At that point you have 300 seconds to abort the shutdown by running the DOS command shutdown –a.

The steps above cover defragging the drives and rebooting the machine, but some files won’t be defragged because Windows is using them when defrag runs.  To defrag those, you need to defragment as part of the boot process.  For 32 bit systems, I recommend Sysinternals PageDefrag.  Download the program, run it, and make sure Defragment every boot is selected:

image

Unfortunately, PageDefrag doesn’t work on 64 bit systems.  In that case I recommend UltraDefrag.  UltraDefrag is a general purpose defragmenter (like Microsoft’s defrag) and also does boot time defragmentation (on 32 or 64 bit systems).  After downloading UltraDefrag, run the GUI console, call up the settings, and check Enable:

image

The script that ships with UltraDefrag assumes that your operating system is on the C drive; if not, press the script button and change the drive.  See http://ultradefrag.sourceforge.net/handbook/Boot.html for more information

Speaking of UltraDefrag, you may want to consider using it in place of Microsoft’s defrag.  The net is full of talk that defrag isn’t very good at what it does and that UltraDefrag and MyDefrag are better choices.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

The Easy Way to Run a DOS Batch Script

You can run a DOS batch script from Windows explorer by double clicking on it, but the output scrolls by and then the window disappears.  What if you need to see the output?  The best thing to do is create a shortcut to the script that captures the output.  For example, given a script test.bat in C:\Program Files\Test, the target of the shortcut should be:

"C:\Program Files\Test\test.bat" > test.txt 2>&1 & test.txt

The "> test.txt" sends the output to a file; the "2>&1" sends any error messages to wherever the non errors messages are going (test.txt in our case); and the "& test.txt" launches test.txt as a command once the DOS script completes.  The net effect is that you can double click on the shortcut, the DOS script will run, and the output will be displayed using Notepad or whatever .txt is mapped to.

A variation on this is to use || (2 pipe bars) in place of the last ampersand, in which case the output file is launched only if the script returns an error code.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Not Playing Nice Together

One of the things I’ve noticed is that two industry trends are not playing nice together, those trends being multi-core CPUs and massive hard drives.  It’s not a problem if you keep your cores busy with compute intensive work, but for software developers the beauty of multi-core CPUs (along with gobs of RAM and a 64 bit OS) is virtualization.  But when you have only one hard drive (who needs another when it holds 2 TB of data?) you wind up with a serious hard drive bottleneck.  A solid state drive would definitely help, and might even be a complete solution, but the cost is ridiculous.  Two TB of solid state storage will set you back around $7,000!  A spinning 2 TB drive is only $150.

I see a couple of solutions for this.  One is the mainframe concept of near and far storage: put the stuff that will be heavily access on a solid state drive and the rest on a spinning drive.  Another solution is multiple spinning drives.  Instead of a single 2 TB drive, get four 500 GB drives.  In total, the four 500 GB drives will cost about $100 more than the single 2 TB drive.  You’ll need to be smart about what drive you place things on so that the load is spread evenly.  Another option, for better performance, would be four 10,000 RPM 300 GB drives, but that would cost about $800 more than the singe 2 TB drive and would deliver only 1.2 TB of space.

All pricing based on Microcenter as of March 14, 2010.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Be Prepared

Your machine is dead.  Now what?  I keep these bootable CDs on hand, just in case:

  1. SpinRite.  This program has saved me several times.  Fixes unusable disks that cause systems to hang.  Worth $90 the first time you use it. 
  2. Ultimate Boot CD for Windows.  This will allow you to boot a dead system from a CD and copy all the important stuff to an external hard drive (plug the hard drive in before booting).  There are also a lot of system utilities on the CD to help with repair.  Free but requires a Windows XP license.
  3. Microsoft Diagnostics and Recovery Toolset is similar in concept to Ultimate Boot CD for Windows but is from Microsoft.  Unfortunately, Microsoft restricts distribution to customers who have purchased Software Assurance.
  4. Acronis True Image Home boot disk.  Boot from CD and backup to external hard drive.  Program also performs regular backups from Windows.  $50 list.
  5. Memory testers.  These exercise the system’s memory and report on any problems found.  All free.  Memtest86, memtest86+, Windows Memory Diagnostic.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Round & Round with Enums

Enumerated constants in C# can be represented 3 different ways:

  1. as a C# program constant
  2. as a number (typically int)
  3. as a string spelled identically to the C# constant

Here a sample program demonstrating how to convert between any of these formats.

using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication2 { public enum Color { red, white, blue } class Program { static void Main(string[] args) { string toString, fromString = "red"; int toInt, fromInt = 1; Color toColor, fromColor = Color.blue; // string to enum toColor = (Color) Enum.Parse(typeof(Color), fromString); // string to int toInt = (int) (Color) Enum.Parse(typeof(Color), fromString); // int to enum toColor = (Color) fromInt; // int to string toString = ((Color) fromInt).ToString(); // enum to string toString = fromColor.ToString(); // enum to int toInt = (int) fromColor; Console.ReadLine(); } } }
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Dropbox Does it Right

I sent my kid off to college this fall and it didn’t require a soothsayer to know that she wouldn’t do any backups and at the end of the semester her hard drive would crap out.  After an emergency repair with the magnificent SpinRite, I started looking into personal cloud based backups.  Dropbox was the only solution that did it all just right.  After installing Dropbox you designate a folder to watch (your dropbox).  Then just do your work, saving it in your dropbox.  Every time a changed file is closed it gets backed up to the cloud.  Nice touches include an file icon overlay to indicate backup status and a tray icon that shows overall backup status.  Need your stuff?  Go to the Dropbox web site and download it or use another computer with Dropbox installed.  You can get 2 gigabytes of space for free.  What’s not to like?

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Upcoming Speaking Engagements

I’ll be speaking on New Debugging Features of Visual Studio 2010 twice next week.

First will be the New Jersey Microsoft Developer's Group on Thursday, March 4 at 6:00.

On Saturday, March 6, I’ll be one of many speakers at Code Camp NYC 4, speaking time not yet set.

Hope to see you at either event!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Speaking at the Fairfield / Westchester Code Camp

This Saturday, November 7, the Fairfield / Westchester .NET User Group is hosting the 3rd annual Fairfield / Westchester Code Camp at the Connecticut Information Technology Institute in Stamford, CT.  I’ll be speaking on New Debugging Features in Visual Studio 2010.  Hope to see you there!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Fixing Your Windows Explorer Shortcut

I’m sure you’ve noticed that Windows Explorer thinks that everything you care about is in My Documents.  If you’re like me that isn’t true; important stuff is in the C drive, network shares, external storage, etc.  Wouldn’t it be nice if Windows Explorer opened showing all the devices on the computer?  Change your Windows Explorer shortcut as below and it will.

%SystemRoot%\explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Microsoft Should Just Give Up on Search

So I’m doing a little coding using reflection and the following exception is raised:

It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead.

I don’t get it so I pump the error message into Microsoft’s Live Search.  Live Search comes back with:

We did not find any results for It is illegal to reflect on the custom attributes of a Type loaded via ReflectionOnlyGetType (see Assembly.ReflectionOnly) -- use CustomAttributeData instead..

Not believing this for even 1 nanosecond, I run the same query using Google.  Google’s first hit points me to Kirk Evens’ MSDN blog where the exact messages appears in the entry!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati