Thursday, July 7, 2011

Windows 7: BOOTMGR Is Missing

One time, upon rebooting the system, I got this message:

BOOTMGR is missing
Press Ctrl+Alt+Del to restart
I rebooted from the Windows 7 DVD and went to the "Repair your computer" option.  It said "Searching for Windows installations."  Then it showed me the System Recovery Options dialog, listing "Windows 7 Ultimate (recovered)."

But that's not what it showed me the first time.  The first time, it showed me nothing.  I read somewhere that somebody found that they needed to run the Repair Your Computer option a couple of times before it would work, so possibly that was what finally populated this dialog with a Windows 7 entry.  But what I think may have solved the problem was that I booted with an Ubuntu 10.10 Live CD, went into System > Administration > GParted, and used their filesystem repair option (or whatever it was called, exactly) on my Windows 7 partition.  (I'm not sure if GParted is included in more recent versions. If not, the 10.04 version will still be available for a while.)

Anyway, I did now see Windows 7 Ultimate (recovered).  I clicked Next > Startup Repair.  It said "Searching for problems" and then "Attempting repairs."  Then it said, "Restart your computer to complete the repairs."  I did that.  The problem was solved.

Tuesday, July 5, 2011

Finding and Removing Dead Program Shortcuts

In Windows 7, I had moved my Start Menu to a folder on drive W.  This protected the Start Menu in case of Windows reinstallation, and made it available for regular backup on the same basis as everything else on drive W.  It could also be shared on a network.

That relocated Start Menu contained all the shortcuts normally found in a start menu.  I had since uninstalled some of the programs to which those shortcuts pointed.  There were a lot of programs, and a lot of shortcuts.  I wanted to find a way to automate the process of going through that folder and detecting the shortcuts that didn't work anymore. 

This post describes the start I made toward that effort.  The post is terminated prematurely.  I did not record the event leading to that termination.  I think I may have reinstalled Windows at that point.

I began with the assumption that the files I was looking for were .lnk files.  That is, I decided to disregard .url or any other kinds of link or shortcut files; I wanted only those that would link to an executable program on my computer.  This did not appear to be a major limiting assumption:  a quick look suggested that only a small number of files in the Start Menu were .url links.

On that assumption, in a CMD box (Start > Run > cmd), I went to the root of the Start Menu folder (using CD W:\) and ran a command to get a file list:

dir *.lnk /s /b /a-d > dirlist.txt
In the resulting dirlist.txt file, I found lots of lines with entries like "W:\Start Menu\Programs\Miscellany\Games\Freecell.lnk."  I copied the contents of dirlist.txt into a Microsoft Excel 2003 spreadsheet.  I added an index column at the far left, so as to number each filename that I had gotten from dirlist.txt.  I gave the files odd numbers.  For example, in cell A2 I typed this:  =A1+2.  I copied that all the way down column A.  So the consecutive rows were numbered 1, 3, 5, and so forth.  I made the numbers permanent by using this sequence:  highlight all numbers; Edit > Copy; Edit > Paste Special > Values.  (Alt-E would bring up those options in newer versions of Excel.)  Now cell A2 just contained the number 3, with no underlying formula.

I copied that worksheet into a second worksheet in the same Excel file.  Here, I replaced the numbers with even numbers.  So in this worksheet, cell A2 contained the number 4.  I named the first worksheet "Odd" and the second worksheet "Even."  I did this so that I could eventually combine the two worksheets into a Combined worksheet, and the numbers would sort in the correct order on that first column in the combined worksheet:  1, 2, 3 ...

In the Odd worksheet, I wanted to produce batch commands that would give me the name of the file we were talking about.  For this purpose, cell A1 contained the number 1, cell B1 contained a file and path name like "W:\Start Menu\Programs\Miscellany\Games\Freecell.lnk," and now cell C1 would contain this formula:  ="DIR /s /b /a-d "&CHAR(34)&B1&CHAR(34)&" >> output.txt."  The CHAR(34) would produce quotation marks that Excel would not misunderstand.  This formula produced a value in cell C1 like this:
DIR /s /b /a-d "W:\Start Menu\Adobe Acrobat.lnk" >> OUTPUT.TXT
I copied that formula all the way down column C, so that I had a whole series of commands to type the names of each link or shortcut file into the OUTPUT.TXT file.

Now I wanted OUTPUT.TXT to contain the file's contents after its name.  These link and shortcut files were typically pretty small, so I felt I could just put it all into the same OUTPUT.TXT file.  To get the file's contents, I went to the Even worksheet.  The command that I put into cell C1 in that spreadsheet was the same as the one quoted above, except here the command was TYPE rather than DIR (and had no /s or other parameters).  This would actually type the contents of the file into the tail end of OUTPUT.TXT.  Here, again, I copied the formula all the way down the worksheet.

With that done, I could combine the Odd and Even worksheets into a Combined worksheet.  I sorted the Combined worksheet on the first field, by the index numbers.  So now I had a list of paired commands, a DIR and a TYPE command for each .lnk file in my Start Menu.  So the first two resulting entries in this Combined worksheet looked like this:
DIR /s /b /a-d "W:\Start Menu\Adobe Acrobat.lnk" >> OUTPUT.TXT
TYPE "W:\Start Menu\Adobe Acrobat.lnk" >> OUTPUT.TXT
After running those two commands, OUTPUT.TXT contained gibberish looking somewhat like this:
W:\Start Menu\Adobe Acrobat.lnk
L        À      FÉP                                                     Ó PàOÐ ê:i ¢Ø +00 /C:\                   R 1     ¬>X~0 Windows < ï¾î:£ ¬>X~*   'ó                   W i n d o w s   X 1     ²>Í1 INSTAL~1  @ ï¾!>&y²>Í1*   ñ©                   I n s t a l l e r   ’ 1     ²>Í1 {A4EF1~1  z ᆵ> §²>Í1*   ô    
              { A C 7 6 B A 8 6 - 1 0 3 3 - F 4 0 0 - 7 7 6 0 - 0 0 0 0 0 0 0 0 0 0 0 4 }   h 2 ˜  ²>Í1! _SC_AC~1.EXE  L ï¾²>Í1²>Í1*   gz                   _ S C _ A c r o b a t . e x e      W . . \ . . \ . . \ . . \ . . \ W i n d o w s \ I n s t a l l e r \ { A C 7 6 B A 8 6 - 1 0 3 3 - F 4 0 0 - 7 7 6 0 - 0 0 0 0 0 0 0 0 0 0 0 4 } \ _ S C _ A c r o b a t . e x e K C : \ W i n d o w s \ I n s t a l l e r \ { A C 7 6 B A 8 6 - 1 0 3 3 - F 4 0 0 - 7 7 6 0 - 0 0 0 0 0 0 0 0 0 0 0 4 } \ _ S C _ A c r o b a t . e x e      34TL`aMnKwUK&!!'L?@$ViewerProgramFiles>c%TW.h=LK?6-6ht!be8]                                                                                                                                                                                                         3 4 T L ` a M n K w U K & ! ! ' L ? @ $ V i e w e r P r o g r a m F i l e s > c % T W . h = L K ? 6 - 6 h t ! b e 8 ]                                                                                                                                                                                                                                                                                                                                                                                                                        %SystemRoot%\Installer\{AC76BA86-1033-F400-7760-000000000004}\_SC_Acrobat.exe                                                                                                                                                                                       % S y s t e m R o o t % \ I n s t a l l e r \ { A C 7 6 B A 8 6 - 1 0 3 3 - F 4 0 0 - 7 7 6 0 - 0 0 0 0 0 0 0 0 0 0 0 4 } \ _ S C _ A c r o b a t . e x e
In other words, I had a link named "Adobe Acrobat" in W:\Start Menu, and that link connected with a file called _SC_Acrobat.exe in the "%SystemRoot%\Installer\{AC76BA86-1033-F400-7760-000000000004}" subfolder.  (The folder named %SystemRoot% would ordinarily be the C:\Windows folder.)  There no longer was any such subfolder, so this particular link was bad.  The challenge for me would thus be to extract those file and directory locations from OUTPUT.TXT, and test them to see which ones led to actual files and folders.

To extract the file and directory locations, I copied the gibberish of OUTPUT.TXT into Word 2003, and saved it as OUTPUT.DOC.  As shown in the gibberish example above, it appeared that extra spaces were added among many characters.  I searched OUTPUT.DOC and verified that there were no occurrences of @@@.  I did a find and replace in Word, replacing three consecutive spaces with @@@.  I repeated that until there were no more occurrences of three consecutive spaces.  Then I replaced all occurrences of two consecutive spaces with ### (also verified not to be present previously).  Next, I replaced all single spaces with nothing.  That is, I just eliminated them.  Now I could convert ### back to two spaces and all occurrences of @@@@ (since there had been a huge number of extra blank spaces) to just three "at" symbols (@@@).  I had to repeat that one a number of times.

Windows 7: Batch Files That Run Things on a Schedule

I was using Windows 7.  I had a bunch of things that I did every day, other things that I did every week, and so forth -- semimonthly, monthly, quarterly, yearly.  I found that these items could be run from within batch files, and that I could run those batch files using Task Scheduler.  This post summarizes some of those arrangements.

The first step was to create a batch file.  Here are the contents of a sample DAILY.BAT file:

echo Ready to open Explorer sessions and projects.
echo.
echo.
echo First, it's time to exercise!
echo.
echo Then move to the last open tab in Firefox.
echo.
pause
cls

:: Regular daily stuff

start Excel "D:\Spreadsheets\Exercise Results.xls"
start sfc /verifyonly
del "X:\Cache\Junk Temp Folder\*.tmp"

:: Open Firefox webpages

start firefox "http://www.thehungersite.com"
start firefox "http://www.npr.org/series/4703895/song-of-the-day"

:: Open folders

start explorer.exe /e,"D:\Folder1\Subfolder1"
start explorer.exe /e,"D:\Folder2\Subfolder2"

:: Not using now

:: start "" "C:\Program Files\Microsoft Office\OFFICE11\Winword.exe"
:: start "" "W:\Start Menu\Programs\PortableWinampLite\winamp.exe"

Using Notepad, I put materials like these into a text file called DAILY.BAT.  The BAT extension would notify Windows that this should run like a program.  The first commands shown here would clear the screen in a CMD window (CLS), show statements (using various forms of ECHO), run the System File Checker (SFS), delete the TMP files from a certain folder, and start various other programs.

All kinds of programs could be started this way.  The information about where to go to start them was usually available in the Properties of shortcuts to them found in the Start Menu.  Some examples here included tabs that open in Firefox (having set Firefox to open new links in new tabs), new sessions of Windows Explorer (opened with a focus on a particular folder), and Microsoft Word and Excel.  The command pointing to Word, and also the one pointing to Winamp, are commented out here (i.e., there are double colons in front of them), because I decided I didn't need them now, but I didn't want to forget how I had set them up, in case I did start to use them again).

Note the two different ways of starting Word and Excel shown here.  Both ways would work for either program; they just show two different approaches.  The reference to Word (i.e., winword.exe) included a full statement of its path, contained in quotation marks, and without specifying a particular file to open.  (The double quotation marks at the start of that Word command solved the problem that some programs would not start unless they were preceded with a dummy variable.)  But the reference to Excel just said "Excel."  That Excel command worked because I put a shortcut to Excel, called simply "Excel," in C:\Windows.  I kept a folder with a half-dozen of these kinds of shortcuts, for different programs that I would invoke frequently, and I would copy them to C:\Windows whenever I reinstalled Windows 7.

I was able to set up batch files like this DAILY.BAT example for any timeframe.  I could have had one called EVENING.BAT, or 10AM.BAT, or MONTHLY.BAT.  I would keep them in a folder on my Start Menu, so that I could run them manually or easily find them for a quick right-click > Edit.

The next step would then be to make them run on a schedule.  This called for running Task Scheduler.  Since I had a Run option on my Start Menu, I would just go to Start > Run > taskschd.msc.  An alternative would be to search for Task Scheduler.  There, I would go to Actions > Create Basic Task and go through the steps of the setup wizard.  When that was done, if I right-clicked on that task and looked at its tabs, I would verify these settings:
  • General tab:  Run with highest privileges; Configure for Windows 7.
  • Triggers:  Enabled.
  • Actions:  Start a program:  DAILY.BAT.  Start in:  insert the path to the place in the Start Menu, or wherever DAILY.BAT is stored.
Other settings might also have to be configured for the particular purpose.  I tended to favor settings that would let the program run whenever the computer was next turned on, so that I wouldn't have to leave the machine on all the time to be sure of letting everything run right when Task Scheduler said it should.

The general idea is that, with a scheduled batch file, I could change the things that would happen at 10 AM, or whenever the batch file was scheduled to run, by just editing the batch file in Notepad.  Anything that I would do every day, or on the fifth day of every month, would run automatically.  I might have to run it a couple of times to work out the bugs, but then it would just do its thing with no further involvement by me.