Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 442 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if a directory exists in %PATH%

#11
Just as an alternative:

1. In the folder you are going to search the `PATH` variable for, create a temporary file with such an unusual name that you would never ever expect any other file on your computer to have.

2. Use the standard batch scripting construct that lets you perform the search for a file by looking up a directory list defined by some environment variable (typically `PATH`).

3. Check if the result of the search matches the path in question, and display the outcome.

4. Delete the temporary file.

This might look like this:

@ECHO OFF
SET "mypath=D:\the\searched-for\path"
SET unusualname=nowthisissupposedtobesomeveryunusualfilename
ECHO.>"%mypath%\%unusualname%"
FOR %%f IN (%unusualname%) DO SET "foundpath=%%~dp$PATH:f"
ERASE "%mypath%\%unusualname%"
IF "%mypath%" == "%foundpath%" (
ECHO The dir exists in PATH
) ELSE (
ECHO The dir DOES NOT exist in PATH
)

Known issues:

1. The method can work only if the directory exists (which isn't always the case).

2. Creating / deleting files in a directory affects its 'modified date/time' attribute (which may be an undesirable effect sometimes).

3. Making up a globally unique file name in one's mind cannot be considered very reliable. Generating such a name is itself not a trivial task.
Reply

#12
rem

[To see links please register here]

rem Don't get mess with %PATH%, it is a concatenation of USER+SYSTEM, and will cause a lot of duplication in the result.
for /f "usebackq tokens=2,*" %%A in (`reg query HKCU\Environment /v PATH`) do set userPATH=%%B
rem userPATH should be %USERPROFILE%\AppData\Local\Microsoft\WindowsApps

rem

[To see links please register here]

for /f "delims=" %%A in ('echo ";%userPATH%;" ^| find /C /I ";%WINAPPS%;"') do set pathExists=%%A
If %pathExists%==0 (
echo Inserting user path...
setx PATH "%WINAPPS%; %userPATH%"
)

Reply

#13
You can accomplish this using PowerShell;

Test-Path $ENV:SystemRoot\YourDirectory
Test-Path C:\Windows\YourDirectory

This returns `TRUE` or `FALSE`

Short, simple and easy!


Reply

#14
Just to elaborate on [Heyvoon's response][1] (2015-06-08) using PowerShell, this simple PowerShell script should give you detail on %path%:

$env:Path -split ";" | % {"$(test-path $_); $_"}

It is generating this kind of output which you can independently verify:

True;C:\WINDOWS
True;C:\WINDOWS\system32
True;C:\WINDOWS\System32\Wbem
False;C:windows\System32\windowsPowerShell\v1.0\
False;C:\Program Files (x86)\Java\jre7\bin

To reassemble for updating Path:

$x = $null; foreach ($t in ($env:Path -split ";") ) {if (test-path $t) {$x += $t + ";"}}; $x

[1]:

[To see links please register here]


Reply

#15
`-contains` worked for me

```lang-none
$pathToCheck = "c:\some path\to\a\file.txt"

$env:Path - split ';' -contains $pathToCheck
```

To add the path when it does not exist yet I use

```lang-none
$pathToCheck = "c:\some path\to\a\file.txt"

if(!($env:Path -split ';' -contains $vboxPath)) {
$documentsDir = [Environment]::GetFolderPath("MyDocuments")
$profileFilePath = Join-Path $documentsDir "WindowsPowerShell/profile.ps1"
Out-File -FilePath $profileFilePath -Append -Force -Encoding ascii -InputObject "`$env:Path += `";$pathToCheck`""
Invoke-Expression -command $profileFilePath
}
```



Reply

#16
This may work:

echo ;%PATH%; | find /C /I ";<string>;"

It should give you 0 if the string is not found and 1 or more if it is.

Reply

#17
Building on [Randy's answer][1], you have to make sure a substring of the target isn't found.

if a%X%==a%PATH% echo %X% is in PATH
echo %PATH% | find /c /i ";%X%"
if errorlevel 1 echo %X% is in PATH
echo %PATH% | find /c /i "%X%;"
if errorlevel 1 echo %X% is in PATH

[1]:

[To see links please register here]





Reply

#18
Another way to check if something is in the path is to execute some innocent executable that is not going to fail if it's there, and check the result.

As an example, the following code snippet checks if [Maven][1] is in the path:

mvn --help > NUL 2> NUL
if errorlevel 1 goto mvnNotInPath

So I try to run *mvn --help*, ignore the output (I don't actually want to see the help if Maven is there) (*> NUL*), and also don't display the error message if Maven was not found (*2> NUL*).

[1]:

[To see links please register here]



Reply

#19
Add the directory to PATH if it does not already exist:

set myPath=c:\mypath
For /F "Delims=" %%I In ('echo %PATH% ^| find /C /I "%myPath%"') Do set pathExists=%%I 2>Nul
If %pathExists%==0 (set PATH=%myPath%;%PATH%)

Reply

#20
This version works fairly well. It simply checks whether executable *vim71* ([Vim][1] 7.1) is in the path, and prepends it if not.

@echo off
echo %PATH% | find /c /i "vim71" > nul
if not errorlevel 1 goto jump
PATH = C:\Program Files\Vim\vim71\;%PATH%
:jump

This demo is to illustrate the *errorlevel* logic:

@echo on
echo %PATH% | find /c /i "Windows"
if "%errorlevel%"=="0" echo Found Windows
echo %PATH% | find /c /i "Nonesuch"
if "%errorlevel%"=="0" echo Found Nonesuch

The logic is reversed in the *vim71* code since *errorlevel* 1 is equivalent to *errorlevel* >= 1. It follows that *errorlevel* 0 would always evaluate true, so "`not errorlevel 1`" is used.

**Postscript**: Checking may not be necessary if you use [SETLOCAL][2] and [ENDLOCAL][3] to localise your environment settings, e.g.,

@echo off
setlocal
PATH = C:\Program Files\Vim\vim71\;%PATH%
rem your code here
endlocal

After ENDLOCAL you are back with your original path.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through