Trigger a hardware detection scan from Delphi, InstallShield, C++, script or Run prompt

In my deployment process, it had looked like I was going to need to detect some changes in hardware and then perform a reboot.
I researched how to do this but it turns ou that I don’t need this code. Into the cave it goes.

You can of course run the “Add New Hardware” wizard manually. Here’s the command line to do just that:
“C:\WINDOWS\system32\rundll32.exe” C:\WINDOWS\system32\shell32.dll,Control_RunDLL “C:\WINDOWS\system32\hdwwiz.cpl”,Detect Hardware

However, what if you want to automate the process.

The information for how to do this is relatively scarce even though there is a technet page about it. Strangely enough the first thing I found was an NSIS script for doing this through that open source instalation program. The strange thing about it is that it was on a WinAMP website (link).

Here’s that code:
[code]
Function ScanForNewHW
SetPluginUnload alwaysoff
StrCpy $1 “”

System::Call ‘setupapi::CM_Locate_DevNodeA(*i .r0, t r1, i r2) i .r3’
System::Call ‘setupapi::CM_Reenumerate_DevNode(i r0, i r4) i .r5’

SetPluginUnload manual
System::Free 0
FunctionEnd
[/code]

Armed with the DLL name, the second thing I found was an Install Shield script (link) that allowed it to be done:
[code]
function ScanForHardwareChanges()
NUMBER devInst, myreturn;
begin
if(UseDLL(WINSYSDIR ^ “cfgmgr32.dll”) != 0)then
MessageBox(“Didn’t load Dll”, SEVERE);
return FALSE;
endif;
myreturn = CM_Locate_DevNodeA(&devInst, “\0”, 0);
myreturn = CM_Reenumerate_DevNode(devInst, 0);
UnUseDLL(WINSYSDIR ^ “cfgmgr32.dll”);
return TRUE;
end;
[/code]

Armed with the DLL name and a possible procedure name, I was able to track down the Microsoft support page about it (link). That page provided a C routine for calling the code. Here it is:

[C]
BOOL ScanForHardwareChanges()
{
DEVINST devInst;
CONFIGRET status;

//
// Get the root devnode.
//

status = CM_Locate_DevNode(&devInst, NULL, CM_LOCATE_DEVNODE_NORMAL);

if (status != CR_SUCCESS) {
printf(“CM_Locate_DevNode failed: %x\n”, status);
return FALSE;

}

status = CM_Reenumerate_DevNode(devInst, 0);

if (status != CR_SUCCESS) {
printf(“CM_Reenumerate_DevNode failed: %x\n”, status));
return FALSE;
}

return TRUE;
}
[/c]

However, I wanted to do this in Delphi. With the correct constant names, I was able to find two references to this routine. The Delphi JEDI project has a provides a routine for loading the DLL that allows these calls to be made and either someone (link) translated Microsoft’s code into a routine for scanning for the hardware or there was a, now gone, JEDI demo project that included this routine. Either way, the French site was the first one I’d found that scanned for new hardware with Delphi.

Here is that code:

[delphi]
procedure SomeProcedure;
// First you need to load the module.
LoadConfigManagerApi;
// Then call a translation of the MS routine
ScanForHardwareChanges;
end;

// Here’s the translation of the ScanForHardwareChanges
function ScanForHardwareChanges: boolean;
var
dev: DEVINST;
status: CONFIGRET;
begin

status := CM_Locate_DevNode(dev, ”, CM_LOCATE_DEVNODE_NORMAL);

if (status <> CR_SUCCESS) then
begin
result := FALSE;
exit;
end;

status := CM_Reenumerate_DevNode(dev, 0);

if (status <> CR_SUCCESS) then
begin
result := FALSE;
exit;
end;
Result := TRUE;
end;
[/delphi]

That routine was picked up on a Russian site (link) and modified to be independent of the JEDI files. However, both of these routines include way more information than is needed.

The process is really simple.
1. Load the DLL
2. Get the location of the two methods you need.
3. Call them (using the appropriate constants
4. Unload everything.

I’ve written my own Delphi routine that does all that and has no extra baggage dragged (drug?) along for the ride..

My all-in-one solution:
[delphi]
{******************************************************************************
ScanForHardwareChanges
by Brian Layman at TheCodeCave.com
******************************************************************************}
function ScanForHardwareChanges: Boolean;
const
CFGMGR32_DLL = ‘cfgmgr32.dll’;
CM_LOCATE_DEVNODE_NAME = ‘CM_Locate_DevNodeA’;
CM_REENUMERATE_DEVNODE_NAME = ‘CM_Reenumerate_DevNode’;
CM_LOCATE_DEVNODE_NORMAL = $00000000;
CR_SUCCESS = $00000000;
var
DeviceNode: DWord;
HCfgMgr: THandle;
CM_Locate_DevNode: function(var dnDevInst: DWord; pDeviceID: PAnsiChar;
ulFlags: ULONG): DWord; stdcall;
CM_Reenumerate_DevNode: function(dnDevInst: DWord; ulFlags: ULong): DWord; stdcall;
begin // ScanForHardwareChanges
Result := FALSE;
HCfgMgr := LoadLibrary(CFGMGR32_DLL);
if (HCfgMgr < 32) then MessageDlg('Error: could not find Configuration Manager DLL', mtError, [mbOk], 0) else begin try CM_Locate_DevNode := GetProcAddress(HCfgMgr, CM_LOCATE_DEVNODE_NAME); CM_Reenumerate_DevNode := GetProcAddress(HCfgMgr, CM_REENUMERATE_DEVNODE_NAME); if (CM_Locate_DevNode(DeviceNode, NIL, CM_LOCATE_DEVNODE_NORMAL) = CR_SUCCESS) then Result := (CM_Reenumerate_DevNode(DeviceNode, 0) = CR_SUCCESS); finally // wrap up FreeLibrary(HCfgMgr); end; // try/finally end; end; // ScanForHardwareChanges [/delphi] As a bonus, here it is combined into a project that scans for new hardware and then reboots the computer. Continue reading Trigger a hardware detection scan from Delphi, InstallShield, C++, script or Run prompt

Copy To Clipboard in HTML

or Speed blogging Exposed…
or “Copy as Text Link” and “Copy as Image Link” Blogging Tools explained.

Usually when I post something technical, I include a detailed rambling explaniation of how it does what it does. In the post “Copy as Text Link” and “Copy as Image Link” Blogging Tools” I created a batch file that did some neat stuff. But I didn’t give a full explaination of the details. Now I will.

The batch file did the following neat stuff…
1. Used Environmental variables to create HTML files in a standard Windows directory
2. Dynamically add string and DWord registry entries
3. Demonstrated adding menu items to Maxthon & Internet Explorer (Unfortunatly these do not show up in FF or Opera.)
4. Displayed different contexts based upon what was selected.
5. Launched another IE window that does not effect the output but does process information selected in the first.
6. Demonstrated how to copy HTML source to the paste buffer via HTML and javascript.:
7. Copied text to the clipboard, formatting it into HTML

Not bad for a small batch file.

So, here’s how the magic was done…

First, the easy stuff. The batch file creates two text files. That is done using the redirection symbol (aka the output to file symbol) which is > (aka the Greater Than sign). The Greater than sign appends the displayed results directly to a file. Two greater thans in a row cause the file to be recreated. I use the Echo command to echo (go figure) text to two HTML files I will be creating. Since I use >> in the first line, those files will always be recreated if the batch file is run again.

Because HTML also uses GT and LT symbols, they must be denoted as literal symbols. That is done with the carot symbol ^. Thusly
[dos]
echo ^>%windir%\web\copytextaslink.htm
echo ^

[/js]

As you can see, the bulk of the file is JavaScript. The second line clearly declares it so. However, instead of putting Language=”JavaScript” (which is the old school way of doing things and how I learned originally), I should have specified it as a MIME type like this Type=”text/javascript” The end result is the same, however eventually the old language option will be dropped.

Another option that you might not be familiar with is the defer flag. This is a browser load time optimizer. It tells the browser that the contents of the script will not effect the layout of the page and therefore the browser can continue drawing the page.

The process is also pretty easy to follow because it uses Microsoft’s DOM technology. DOM is the Document Object Module and allows IE to handle all web pages as if it were Object Oriented. You can reference the document and the various parts/divisions of any web page. And that allows you to directly access properties or methods of these objects.

So what the code above does is asks for the document in the parent window (the one in which you right clicked). It then asks for the current selection (if any). It then gets the current selection as text and then asks for the clibboard object for the current window. At that point it is a simple call to SetData and the selected text is added (with a few additions) to the clippboard. There isn’t much more than that. One thing that should be noted is that the type of data in the clibboard must be specified, in this case, as text. You can read more about the other types of data that a clib board can handle in my article about Clipboard manipulation with Delphi HERE:

The second file proceeds much the same way except that it goes further into the DOM and retreives the Height and Width of the original image so that the pasting of the tag in your blog can include that information (most specifically, the aspect ratio.

[js]


[/js]

That’s it for the HTML. The only thing left is to create a couple of triggers to activate those two files. That’s done through the registry.

Internet explorer reads through the registry each time it pops up a menu. It first decides what is clicked and then looks at the registry entries under HKey_CurrentUser\Software\Microsoft\Internet Explorer\MenuExt\ all of the menu items that match the type of selection. MSDN has a full article that explains this in detail HERE, however, the following table is probably all you’ll need:

Context Value
Default 0x1
Images 0x2
Controls 0x4
Tables 0x8
Text selection 0x10
Anchor 0x20

So, the last step of the process is to route the text to the text processing HTML and the img tag source to the image text file.

That’s done in these two lines…
[dos]
reg ADD “HKCU\Software\Microsoft\Internet Explorer\MenuExt\Copy Image as Lin&k” /ve /t REG_SZ /d “%WINDIR%\web\copyimageaslink.htm” /f
reg ADD “HKCU\Software\Microsoft\Internet Explorer\MenuExt\Copy Image as Lin&k” /v Contexts /t REG_DWORD /d 2 /f
.
[/dos]

That’s it! You’ve got your full walk through! Any questions?

“Copy as Text Link” and “Copy as Image Link” Blogging Tools

Justin of JustInsomnia had a great idea for a FireFox plug in:

I want to be able to copy a quote and its URL at the same time, without having to so much as think about it. To do so, I wrote my first Firefox extension, which I opted to name the somewhat homely, Copy as HTML Link.

So he created Copy as HTML Link for Firefox and generously shared his moment with wp-hackers Subscribers .

The plugin looks good and he did a really good job documenting it (THIS IS RARE!):

Click to go to the original site.

I must say that since I’ve started using this method, it’s been a quite a time saver. This was a GREAT idea. Inspired! So, if you have FireFox, please head on over there and get that plug-in!

Ah, but here’s the fly in the ointment and the sand in my… socks: A Firefox plugin won’t work for Maxthon!!!! (Click to go to the original site.) or Internet Explorer!

You see, Firefox just doesn’t do it for me. Click to go to the original site. Opera comes darn close to being right, but I like the extra usability features in Maxthon.

So, I thought… Hey… I could do that for Maxthon and IE AND I could do it all from a BATCH file!

So, here it is…

Run this Batch file and you will have installed on your system, two new menu items. When you highlight text, you’ll have an option to “Copy as Link” and when an image is right clicked, you will get an option to “Copy Image as Link”. Hopefully I’ve chosen hotkeys that will work well for you… If not, you can change them in RegEdit at HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\

BTW EVERY image and link up to this point in this post was created using this tool. Man, Good idea Justin!

Also – If you find that IE is prompting you asking for permissions to open the links, please post a comment and I’ll show how to fix that. I’ve gotten that message on other things I’ve like this I’ve created and know how to get past it. I’ve disabled that workaround on this computer and I am not getting that “Are you sure” message. So these menu items should be safe. ALSO, some popup blockers may block these items. You might have to add localhost as a site that is allowed to show popups in your particular popup blocker. Let me know if you have any problems.

Here’s the download: http://www.TheCodeCave.com/downloads/batch/AddCopyAsLink.bat

Here is the source:
[DOS]
@echo Off
:: Implements “Copy * as Link” menu items in IE and Maxthon
:: Written by Brian Layman
:: Visit me at: http://www.TheCodeCave.com
:: Version 0.1
:: Update Url: http://www.thecodecave.com/?p=152
:: Inspired by Justin’s Firefox Plubin
:: Found at: http://justinsomnia.org/2006/05/copy-as-html-link-for-firefox/

:: Create file for the Copy Text as Link
echo ^>%windir%\web\copytextaslink.htm
echo ^

How to: Creating a unique temporary file and determining the working drive and path

This is a demonstration of how to use a TempFile in a .BAT or .CMD file. The goal of this example is to determine the relative path of the current working directory and assign it to a variable.

Concepts Demonstrated:

  • Generating a unique file names
  • Creating a temp file, using the Windows Naming convetion, and deleting it when done
  • Calling support subroutines in a batch file
  • Using GOTO :EOF to end exit a subroutine but not the batch file
  • Use of the command line “FOR” routine
  • Using Variables with “for /F”
  • How to get the current directory
  • Verbose and Quiet modes for a batch file

Temporary files are incredibly useful in batch files. When used in combination with a “For” loop, you can accomplish some amazing stuff from the command line.

In this example we assign a new file name to the TEMPORARY_FILE variable and then export the current working path to that file by echoing cd environment variable to it:

call :GETTEMPNAME
echo %cd%>%TEMPORARY_FILE%

Once our temp file has contents, the For routine can be used to parse each line and execute a command with the result. So, we use the For command to divide the working directory, lets say it is “C:\WorkingDir” into several peices (aka tokens):

FOR /F “tokens=1* delims=:” %%G IN (%TEMPORARY_FILE%) DO set RelativePath=%%H

Literaly we are telling it to break the line up at every colon ( : ), and to assign the first token to the variable %G. The asterisk (*) indicates that each token after that should be assigned to a new variable letter (H,I,J, and etc.). In our example, our %H will contain “\WorkingDir”.

“For /F” is really most usefull with larger files containing multiple lines. Since our file contains only one line with two tokens, the process stops there but you could, for example, parse all the names of files in a directory (dir *.xyz /b > %TEMPORARY_FILE%), or the contents of a registry key exported to that file using (REG QUERY […] > %TEMPORARY_FILE%). I’ve used both of these methods in the past to do some really neat things that might have otherwise involved creating a custom executible. One advantage of doing it from teh command line is that it keeps the registry clean of all the garbage left behind by Windows anytime you run an exe.

Oh!

Something that is not often emphasized in “For /F” documentation is that it can parse variables as well as files. This could greatly optimize this example. Instead of parsing our file, we would simply put our %CD variable in quotes and call:

FOR /F “tokens=1* delims=:” %%G IN (“%CD%”) DO set RelativePath=%%H

We will get exactly the same results and can cut our batch file size in half! Of course, then we wouldn’t have a good example of temp file usage would we? So, we will do it the hard way.

Thus:
Continue reading How to: Creating a unique temporary file and determining the working drive and path

A batch file to find the first matching file in the search path…

This is another small app I wrote for someone to use at the ZTree forum.

This one is a DOS batch file that finds files in your search path that match the criteria you pass to it.
For instance you could type “SearchPath WPFile.Doc” and it would return the location of that file in your search path. If you type in simply “SearchPath MyApp” it will do a search for all executible files as defined by the PATHEXT environmental variable.

I found this program extremely useful since when clicking Start>Run and entering NO (it was supposed to autocomplete to notepad) ran a program that I could not find anywhere. SearchPath found it in a network directory.

Concepts Demonstrated:
Batch file subroutines – Use a CALL to execute a jump location in the same batch file as if it was a seperate batch
GOTO :EOF – Used to return out of a batch file OR batch Subroutine.
For Loops & Accessing to Environment variables
Child Recursion – A parent calls the child and the child in turn calls the parent which could in turn
child again.

[DOS]
:: *************************************************************************
:: SearchPath.Bat 10/31/2005
:: Written by Brian Layman (AKA Capt. Queeg)
:: Visit him at http://www.TheCodeCave.com
::
:: A batch written to display the program that would be run when
:: a filename is typed at the command prompt. Just a demo for
:: Hartmut at http://www.ztw3.com/forum/forum.cgi
::
:: Usage: SearchPath ProgramName[.EXT]
::
:: History:
:: 10/31/2005 – BL – Created
:: 11/01/2005 – BL – Removed Temp File Usage
::
:: *************************************************************************
@echo Off
:: All this is boiled down to one subroutine that sets a variable of the
:: same name.
call :SearchedFilePath %1

:: If no program is found, say so.
if “%SearchedFilePath%”==”” echo There is no matching program in the search path

:: If a program was found, echo its name.
if NOT “%SearchedFilePath%”==”” echo %SearchedFilePath%

:: Clear out our temp variable
set SearchedFilePath=

:: Quit
GOTO :EOF
:: *************************************************************************

:: *************************************************************************
:: Support procedures
::
:: These routines are called with a CALL directive and the GOTO :EOF
:: terminates that CALL but does not terminate the entire running of the
:: batch file.
:: *************************************************************************

:: *************************************************************************
:SearchedFilePath
:: Returns the full path to a passed file in the searchpath
::
:: Returns blank if not found.
::
:: *************************************************************************
: set SearchedFilePath=
:: Set the default value to blank.
set SearchedFilePath=

:: If there is no extension handle it
if “%~x1″==”” Call :SearchWithExtensions %1&GOTO :EOF

:: There is no extension, is it blank?
if “%1″==”” GOTO :EOF

:: So, we have an extension. That means we can do a simple search.
:: %~dp$PATH:1 automatically searches the path for us. It is a
:: variable set by the Call command.
set SearchedFilePath=%~dp$PATH:1%1
if “%SearchedFilePath%”==”%1” set SearchedFilePath=&GOTO :EOF
GOTO :EOF
:: *************************************************************************

:: *************************************************************************
:SearchWithExtensions
:: Iterates the extensions gathered from the PATHEXT environment
:: and searches until the file is found.
::
:: Returns blank if not found.
::
:: *************************************************************************
:: Initialize a counter for looking at multiple search results in one line
set cnt=0

:SearchLoop
:: Break out after 20 checks.
:: If you might have more than 20 extensions, increase this value.
:: If you could find out how many periods there are in the temp file,
:: you could optimize this.
if “%cnt%”==”20” GOTO :SearchLoopCleanup
set /A cnt=%cnt%+1
:: Continually search the single line file returning each sequential
:: search result and recursively pass it to the SearchedFilePath routine.
:: When we ask for a token # that doesn’t exist and blank is returned,
:: abort out.
for /F “tokens=%cnt% delims=.;” %%C in (“%PATHEXT%”) do call :SearchedFilePath %1.%%C
if “%SearchedFilePath%”== “” GOTO :SearchLoop

:SearchLoopCleanup
:: Clear our Temp variable
set cnt=
GOTO :EOF
:: *************************************************************************[/DOS]

A command line method to reattach to networks

This routine does the same thing as the Windows Network Repair function, but from the command prompt.  Now why is this important? Well, it is VERY important when you have effected network changes, or physically had someone detach the computer from one network and attach it to another.  When this is done, the computer has to know to forget everything it knew about the old network and learn what it can from the new network.  Double clicking on a short cut to this batch file can take care of that and it is much simpler than leading someone through the steps to get to the network repair option.

Concepts Demonstrated: Network manipulation
Concepts Demonstrated: Quiet Parameter

      ::@Echo off ::************************************************************************* :: This Batch file performs the rough equivelant of a Windows XP SP1  :: Network Connection Repair. :: :: Usage: ::   NetworkRepair      <-- Prompts for permission & repairs all networks ::   NetworkRepair [ConnectionName]     <-- Prompts & repairs one network ::   NetworkRepair Quiet    <-- Does Not Prompt (Quiet is Case Sensitive) ::   NetworkRepair Quiet [ConnectionName]    <-- Repairs only one network :: :: A Windows Network Connection Repair (Right click on the Local Area  :: Connection and choose Repair) performs a series of commands that allows :: a computer to rebuild a its connection to the network.  The commands :: that are invoked by Repair are included below in their command-line :: equivalents.  The only current  difference is that the Repair process :: first checks whether DHCP is enabled and then, if enabled, it issues a :: broadcast renew to refresh the IP address.   This will cause the  :: computer to accept a lease from *any* DHCP server that is on the network.  :: You cannot do that from the command line.  In contrast, a unicast renew :: (as done through ipconfig /renew) will only renew the existing lease :: from the currently used DHCP server.  I've  tried to work around that :: difference calling ipconfig /release.  Hopefully, putting the :: ipconfig /renew further down in the batch file will cause a broadcast :: request for a new IP address.  I do not know if that will happen or not.  :: In any case, this would only affect the behavior of the repair if your :: network has multiple DHCP servers. :: :: In addition, a normal repair is performed for one connection at a time. :: This batch file will repair all network connections unless you specify :: the mask to use for finding the correct networks. Like so: ::   NetworkRepair Wi*         <-- Repair any connection that has its name ::                                 starting with "Wi" ::   NetworkRepair Quiet *Con* <-- Quietly repair all connections with, ::                                 "Con" anywhere in the name.  Examples ::                                 include "Local Area Connection 1" or ::                                         "Local Area Connection 2":: ::   NetworkRepair quiet       <-- Repair all networks named "quiet" :: :: :: Author - Brian Layman :: Created - 5/29/2003 :: Last Modified - 7/27/2005 :: :: License - If this helps you - Great! Use it, modify it share it. :: :: Indemnity - You're playing with network settings and clearing caches. ::    That means that if your settings are messed up on this computer or any ::    of your network servers or routers, running this batch may cause it to ::    see even fewer network resources from this computer.  This batch file  ::    makes the computer forget everything it ever knew about the way the  ::    network was working before.  When it asks the routers and servers for new ::    information, that information must be correct for your problem to go away. ::    ::    Use this batch file at your own risk.  I'm only calling built-in Windows ::    commands, but if a typo or service pack change affects what this routine ::    does, it is not my fault.  In fact, you should just stop right now and ::    not run this file.  For if it causes blue smoke to be emitted from your ::    network card, if it resets your home site to HowToKillMyBoss.com, or if ::    it makes your sister break up with her lawyer boyfriend and start dating ::    a caver, it is not my fault.  (Actually that last one might be an ::    improvement, but it is still not my fault.) :: :: Uses - This routine allows you to automate switching from one network ::    connection to another simply by double-clicking a link on the desktop. ::    It may also be useful in performing quick repairs of network connections ::    rather than guiding your relative/friend/spouse through the process over ::    the phone. :: :: Donations - If this batch file really helps you out, feel free to make a $5 ::    (US) donation via Paypal to Brian@TheCodeCave.com or just send a Thank ::    You via email to that address and include your country of origin. :: :: MD5 - The correct MD5 is available at: ::                     http://www.The-WildWest.com/Queeg/NetworkRepair :: :: Enjoy! :: ::*************************************************************************  :: ************************************************************************* :Quiet Check ::  Check for the Quiet password. :: ************************************************************************* :: Put a bracket around the %1 to trap for empty values and to allow the :: string comparison to work if [%1] == [Quiet] GOTO :TheWorks :: *************************************************************************  :: ************************************************************************* :TheWarning ::  ::  Display some info about the program and also allow several ways to ::  abort an accidental launch. :: :: ************************************************************************* Echo. Echo This batch file will reset your network connection.   Echo. Echo. By running it you're playing with network settings and clearing caches. Echo. That means that if your settings are messed up on this computer or any Echo. of your network servers or routers, running this batch may cause it to Echo. see even fewer network resources from this computer.  This batch file Echo. makes the computer forget everything it ever knew about the way the network Echo. was working before.  When it asks the routers and servers for new Echo. information, that information must be correct for your problem to go away. Echo. Echo. Use this batch file at your own risk.  I'm only calling built-in Windows Echo. commands, but if a typo or service pack change affects what this routine Echo. does, it is not my fault.  In fact, you should just stop right now and Echo. not run this file.  For if it causes blue smoke to be emitted from your Echo. network card, if it resets your home site to HowToKillMyBoss.com, or if Echo. it makes your sister break up with her lawyer boyfriend and start dating Echo. a caver, it is not my fault.  (Actually that last one might be an Echo. improvement, but it is still not my fault.) Echo.  @pause  :: ************************************************************************* :TheWorks :: ::  Let the fun begin! :: :: ************************************************************************* if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Release the DHCP lease - NOT PART OF REPAIR if NOT [%1] == [Quiet] ipconfig /release %1 if [%1] == [Quiet] ipconfig /release %2 if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Flush the ARP cache arp -d *  if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Flush the NetBIOS cache nbtstat -R  if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Flush the DNS cache ipconfig /flushdns  if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Request a new the DHCP lease - NOT PART OF REPAIR if NOT [%1] == [Quiet] ipconfig /renew %1 if [%1] == [Quiet] ipconfig /renew %2  if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Re-register with WINS nbtstat -RR  if NOT [%1] == [Quiet] Echo. if NOT [%1] == [Quiet] Echo * Re-registers with DNS ipconfig /registerdns  if [%1] == [Quiet] CLS  :: ************************************************************************* :TheEnd :: ::  So long and thanks for all the fish! :: :: ************************************************************************* 

Display the program that will run when type a name at the cmd prompt

You can also find this file here: http://www.the-wildwest.com/Queeg/Batches/SearchPath.bat

:: ************************************************************************* ::  SearchPath.Bat                                               10/31/2005 ::  Written by Brian Layman (AKA Capt. Queeg) ::  Visit him at http://www.thecodecave.com/ ::  ::  A batch written to display the program that would be run when ::  a filename is typed at the command prompt.  Just a demo for ::  Hartmut at http://www.ztw3.com/forum/forum.cgi ::  ::  Usage: SearchPath ProgramName[.EXT] ::  ::  History: ::     10/31/2005 - BL - Created ::     11/01/2005 - BL - Removed Temp File Usage ::     02/28/2006 - BL - Changed my URL ::  :: ************************************************************************* @echo Off :: All this is boiled down to one subroutine that sets a variable of the :: same name. call :SearchedFilePath %1 :: If no program is found, say so. if "%SearchedFilePath%"=="" echo There is no matching program in the search path :: If a program was found, echo its name. if NOT "%SearchedFilePath%"=="" echo %SearchedFilePath% :: Clear out our temp variable set SearchedFilePath= :: Quit GOTO :EOF :: ************************************************************************* :: ************************************************************************* ::  Support procedures :: ::  These routines are called with a CALL directive and the GOTO :EOF ::  terminates that CALL but does not terminate the entire running of the ::  batch file. :: ************************************************************************* :: ************************************************************************* :SearchedFilePath ::  Returns the full path to a passed file in the searchpath :: ::  Returns blank if not found. :: :: ************************************************************************* : set SearchedFilePath=    :: Set the default value to blank.   set SearchedFilePath=   :: If there is no extension handle it   if "%~x1"=="" Call :SearchWithExtensions %1&GOTO :EOF   :: There is no extension, is it blank?   if "%1"=="" GOTO :EOF   :: So, we have an extension.  That means we can do a simple search.   :: %~dp$PATH:1 automatically searches the path for us.  It is a   :: variable set by the Call command.   set  SearchedFilePath=%~dp$PATH:1%1   if "%SearchedFilePath%"=="%1" set SearchedFilePath=&GOTO :EOF   GOTO :EOF :: ************************************************************************* :: ************************************************************************* :SearchWithExtensions ::  Iterates the extensions gathered from the PATHEXT environment ::  and searches until the file is found. :: ::  Returns blank if not found. :: :: *************************************************************************     :: Initialize a counter for looking at multiple search results in one line     set cnt=0     :SearchLoop       :: Break out after 20  checks.       :: If you might have more than 20 extensions, increase this value.       :: If you could find out how many periods there are in the temp file, you could optimize this.       if "%cnt%"=="20" GOTO :SearchLoopCleanup       set /A cnt=%cnt%+1       :: Continually search the single line file returning each sequential search result and recursively pass it to the       :: SearchedFilePath routine.  When we ask for a token # that doesn't exist and blank is returned, abort out.       for /F "tokens=%cnt% delims=.;" %%C in ("%PATHEXT%") do call :SearchedFilePath %1.%%C       if "%SearchedFilePath%"== "" GOTO :SearchLoop         :SearchLoopCleanup       :: Clear our Temp variable       set cnt=   GOTO :EOF :: *************************************************************************