0

I am attempting to search the windows path variable to see if a directory exists in it. And If it does not, add it.

My code is as follows:

@echo off

SET VAR1=%path%

echo %VAR1% > text.txt

FOR /f "tokens=* delims=;" %%a IN (text.txt) DO (
echo.%%a|findstr /C:"app0" >nul 2>&1
    if not errorlevel 1 (
    echo Directory was Found
    ) else (
    SET PATH=%PATH%;%cd%\app0
    )
)

The issue I am getting must occur in the else statement. When I replace the SET PATH with echo NOT FOUND everything works perfectly. However, when I use the line to set the path variable, it returns "\Common was unexpected at this time"; while %cd% should not include "common" as I am running from the desktop

I had run it once before and it ran perfectly and everything functioned, I went to reboot and now the same code does not achieve the same result.

After some debugging, I found that the issue is actually in the line echo %VAR1% > text.txt

Turning echo on displays the following:

echo C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R)
> Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R)
> Management Engine
> Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program
> Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;C:\Program Files (x86)\Common
> Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R)
> Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R)
> Management Engine
> Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program
> Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program
> Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files
> (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;C:\Users\Bryan
> Douglas\AppData\Local\Microsoft\WindowsApps;C:\Program
> Files\Intel\WiFi\bin\;C:\Program Files\Common
> Files\Intel\WirelessCommon\;  1>text.txt
>     \Common was unexpected at this time.

Am I doing something wrong?

Bryan Douglas
  • 137
  • 1
  • 10
  • You do know that setting the `%PATH%` variable using `Set` from the batch file will only be local to that specific instance of `cmd.exe`, don't you? Also, are you aware that `If /I "%PATH:app0=%"=="%PATH%" Echo Not Found` or `Echo "%PATH%"|Find /I "app0">Nul||Echo Not Found` may work without the need for a text file or a `For` loop. – Compo Dec 07 '18 at 16:34
  • I am very new to batch. This is my first look into scripting. Though, yes, I do know that it is instance specific – Bryan Douglas Dec 07 '18 at 16:38
  • Have a look at the top answer to [How to check if directory exists in %PATH%?](https://stackoverflow.com/q/141344/1012053) for a robust solution to this problem. – dbenham Dec 07 '18 at 21:56
  • I figured it out with my previous code. My code was correct, just implemented incorrectly – Bryan Douglas Dec 07 '18 at 22:20

1 Answers1

0

Just to address your real issue:

Your PATH contains closing parentheses, for example:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath
                     ^ here

This parenthesis closes your command block at your set command:

SET PATH=%PATH%;%cd%\app0
           ^ inside here

The next thing after the ) is \Common Files\Oracle\Java\javapath, which is interpreted as a command \Common with a parameter of Files\Oracle\Java\javapath. So consequently you get:

\Common was unexpected at this time.

Use the preferred syntax set "var=value" (note the position of the quotes). The quotes secure poisonous chars like )&|...

So your code works with just using the preferred set syntax:

SET "PATH=%PATH%;%cd%\app0"
Mofi
  • 46,139
  • 17
  • 80
  • 143
Stephan
  • 53,940
  • 10
  • 58
  • 91