Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've set an system environment variable called find which points to a batch script. I did this so that in Win command prompt i could type %find% and it would execute my script. It works the only problem is it only works once, my script takes a parameter or requires user input (have tried both), and then it is as if the %find% is temporarily overwritten, and the %find% of course no longer works, until i reopen the command window. Basically it works once and that's it!

How can i make it work every time? i want to execute my script using the environment variable over and over again at will without reloading the command window.

Thanks.

share|improve this question

4 Answers 4

I created a batch script with the following code:

@ECHO off
echo hello

and added a environmental variable called TEST that points to the script. I have no problem executing the script using the environmental variable multiple times.

Can you please provide some information or code of what your script does?

share|improve this answer
    
It works find the first time, then when i run it again i get "'%find%' is not recognized as an internal or external command, operable program or batch file.". Also, the script is like a linux grep script using findstr. –  Draconian Times May 3 '13 at 9:01
1  
Check Peters answer, though I also tried to rename my environmental variable to "find" and it still works. To be sure, rename it to something like "find2". –  red-nuht May 3 '13 at 9:12
    
Thanks guys i know what the problem is now, the environment variable was the same name as the batch file, with different names it now works all the time :D I've had this problem before in a slightly different case and should have realized. Thanks!! –  Draconian Times May 3 '13 at 9:19
    
@DraconianTimes: This is not the problem. You may test it by changing the name of the Batch file (and the value in the variable) and preserving the former name of the variable: the problem should persist (see my answer). –  Aacini May 3 '13 at 21:01

Remember that find is a MS-supplied utility.

Try using a different name. And show us your batch - even possibly describe what happens when it "no longer works." Games of 20-questions are tedious.

share|improve this answer

The problem is that the Batch script uses a variable with the same name, so after it run for the first time the variable value is overwritten and no longer works. To prevent this to happen, insert a setlocal command at beginning of the Batch file; this way, when the script ends all variables are reset to the values they had before the script run. This method also delete all new variables defined in the Batch script, so it keep the environment clean.

share|improve this answer
    
In conjunction with this, it would also be good to add endlocal to the end of the script (to restore environment variables to their previous state). –  Seth McCauley May 7 '13 at 18:08
1  
@SethMcCauley: The help for setlocal /? command indicate: "When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script." –  Aacini May 8 '13 at 0:44
    
Thanks, I did not realized that... good to know! –  Seth McCauley May 9 '13 at 20:56

If your intention is to override the behavior of the existing find.exe utility, you could add the location of the script to the global path variable before your System32 folder (where find.exe is located). For example, let's say your script is C:\Scripts\find.bat. If your path variable is currently set to this:

%SystemRoot%\system32;%SystemRoot%

...then you would change it to this:

C:\Scripts;%SystemRoot%\system32;%SystemRoot%

Beware though... doing this could break other scripts that use the find command (if they don't use the absolute path to find.exe).

If you are just wanting an easy way to run your alternate find command, you could just give it a different name as the others have suggested, then add it to the end of the path or place it in the System32 folder. That would save you from having to type the percent signs at least.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.