0

I wish to dynamically break a string using ; as a delimitter C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\RSA SecurID Token Common;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;

and then find if a path already exist. If C:\windows\system32 is already in the string then it should raise a flag.

Please help

Snap Aoe
  • 145
  • 1
  • 1
  • 11
  • 1
    Where is the problem in your code? – jeb May 09 '17 at 09:23
  • Besides the "'Pretty print' windows..." Q&A, you should also look at [How to check if directory exists in %PATH%](http://stackoverflow.com/q/141344/1012053) – dbenham May 10 '17 at 04:26

2 Answers2

1

A simple, not bulletproof, method may look something like this:

@ECHO OFF
SET "FLAG="
FOR %%A IN ("%PATH:;=";"%") DO IF /I %%A=="%SystemRoot%\system32" SET "FLAG=T"
IF DEFINED FLAG ECHO The location exists as an entry in %%PATH%%
TIMEOUT -1

It will work correctly if you do not have paths with ; or ".

Edit
If you are unlucky enough to have entries using double quotes, you could use a slightly different method which replaces each semi-colon with a line feed character:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "FLAG="
SET LF=^
%FLAG%
%FLAG%
FOR /F "DELIMS=" %%A IN ("%PATH:;=!LF!%"
) DO IF /I "%%A"=="%SystemRoot%\system32" SET "FLAG=T"
IF DEFINED FLAG ECHO The location exists as an entry in %%PATH%%
TIMEOUT -1
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Can you explain the use of FOR %%A IN ("%PATH:;=";"%") – Snap Aoe May 09 '17 at 09:57
  • `"%PATH:;=";"%"` places each entry of path into doublequotes, _(it replaces each semi-colon with a double-quote, semi-colon, doublequote sequence and adds an opening double-quote to the first entry and closing double-quote to the last entry)_. The for loop then separates each entry because the semi-colon, `;`, is one of it's default delimiters. Each entry is attached to `%%A` which is then compared against your target string using the case insensitive `IF` option, `/I`. – Compo May 09 '17 at 10:58
  • 2
    But `;` is also a valid character in a directory, like `PATH="C:\my;dir\";C:\windows` – jeb May 09 '17 at 13:56
  • 1
    I'm aware of that @jeb, however the stipulation of a semi-colon delimiter means that it is unnercessary to provide for that scenario. _You need to also bear in mind that my examples use %PATH%, the OP on the other hand has not._ – Compo May 09 '17 at 15:27
0

complement prior answer, also

@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (^"!path:^;^=^
% Do NOT remove this line %
!^") do IF /I "%%a"=="%SystemRoot%\system32" SET "FLAG=T"
IF DEFINED FLAG ECHO The location exists as an entry in %%PATH%%

this can handle paths with quotes.

elzooilogico
  • 1,659
  • 2
  • 17
  • 18