3

Is the directory of findstr always C:\Windows\system32\ for all OS from Windows XP to server 2012?

Or in other words is it safe to replace the following expression in a windows batch file:

findstr

to

C:\Windows\system32\findstr
Mofi
  • 46,139
  • 17
  • 80
  • 143
Brainless
  • 1,522
  • 1
  • 16
  • 30

3 Answers3

3

Extra information just for completeness.

Environment variable windir exists since Windows 95 and contains the path to Windows directory independent on which drive it is located and which name it has.

For NT based Windows (NT4, Windows 2000, Windows XP, Vista, Windows 7 / 8 / 8.1) there is additionally the environment variable SystemRoot which contains also the path to Windows directory as this is the parent directory for the system directories

  • System32
  • Sysnative (only Windows x64 and only for 32-bit applications)
  • SysWOW64 (only Windows x64)

For details about file system redirection read the File System Redirector page of Microsoft.

It is not only safe to use either

%windir%\System32\findstr.exe

or

%SystemRoot%\System32\findstr.exe

I would highly recommend using always one of those two strings in batch files as then it does not depend which folders are in environment variable PATH and which file extensions in environment variable PATHEXT.

There are some bad installers which add the folder path of the installed application to system environment variable PATH at beginning instead of appending at end and contain in the application's folder also find.exe or findstr.exe which are portings from Unix and therefore work completely different than find.exe and findstr.exe of Windows. AVRStudio is (or perhaps was as not verified with latest version of AVRStudio) an example breaking batch files of IT administrators not using always complete file name for Windows commands after installation.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Obviously, when you have multiple directories in your PATH variable, which contain files with the same name, but different content, it becomes tricky. But, in a way, that is what PATH is for. You must decide what path should take precedence .. in PATH. If you have Unix paths with dangerously named variables ... what about putting that after the Windows path, in PATH ? – tvCa Sep 04 '15 at 20:42
  • @tvCa There is of course no problem to sort the directories in system and user PATH according to importance. The problem with installer of AVRStudio was that the `bin` directory of installed AVRStudio was silently added to system PATH at beginning instead of end. Most IDEs do not add their directory with the binaries to system PATH at all and those which do that append them at end. Most IDE insert the binary directory of compiler to local copy of PATH at beginning on starting a build. For batch files executed during a build task as pre or post build step this must be often taken into account. – Mofi Sep 05 '15 at 09:06
  • @tvCa Take a look on question and my answer at [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](http://stackoverflow.com/questions/25915767/) – Mofi Sep 05 '15 at 09:10
  • I know, when adding a path to the PATH, there's few option one has : either at the start, or at the end. In theory, one can diagnose the PATH variable, and put it somewhere in the middle, but that requires much much more code. Really bad vendors, put the path at the start, because they don't care about the OS, only about their little stupid program. If you put a PATH at the start, one must know what he is doing. Often, it's not good. Unless it's hardcore OS related, or temporar. – tvCa Sep 05 '15 at 09:29
2

Rather %windir%\system32\findstr.exe as its possible windows to be installed on different than C:\ drive.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

I am afraid I don't understand the purpose of your question, so I rephrase it in a slightly different way.

If you type findstr in a computer and the FINDSTR command run, then you may know the location of such findstr command this way:

for %%a in (findstr.exe) do echo %%~$PATH:a

So you may replace the following expression:

findstr

... by the next two lines:

for %%a in (findstr.exe) do set "findstrPath=%~$PATH:a"
%findstrPath%

... in a Windows Batch file and you will get exactly the same result.

So, the question arises: if you get the same result in both cases, why do you want to use the second, more complicated one?

Note also that the same point apply with any other external command like find, xcopy, forfiles, etc. Why findstr would be special in this point?

As I said before, I don't understand the purpose of your question...

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I have an OS with no PATH environment variable, and or %%a in (findstr.exe) do echo %%~$PATH:a outputs me %%a was unexpected at this time – Brainless Jul 13 '15 at 05:29
  • @tvCa: Yes. This is why I wrote: "If you type `findstr` in a computer and the FINDSTR command run...". If there is no PATH, then typing `findstr` does NOT run FINDSTR, so @Brainless comment is, humm, err, well, a Brainless comment! – Aacini Sep 04 '15 at 22:45