ISFILE with URL via PowerShell

Further to ISFILE with URL , PowerShell may provide a solution with System.Uri

Example:
Code:
[System.Uri]'http://jpsoft.com'


AbsolutePath  : /
AbsoluteUri  : http://jpsoft.com/
Authority  : jpsoft.com
Host  : jpsoft.com
HostNameType  : Dns
IsDefaultPort  : True
IsFile  : False
IsLoopback  : False
IsUnc  : False
LocalPath  : /
PathAndQuery  : /
Port  : 80
Query  :
Fragment  :
Scheme  : http
OriginalString : http://jpsoft.com
DnsSafeHost  : jpsoft.com
IsAbsoluteUri  : True
Segments  : {/}
UserEscaped  : False
UserInfo  :
or
Code:
[System.Uri]'file:///C:/utils/4utils.txt'


AbsolutePath  : C:/utils/4utils.txt
AbsoluteUri  : file:///C:/utils/4utils.txt
Authority  :
Host  :
HostNameType  : Basic
IsDefaultPort  : True
IsFile  : True
IsLoopback  : True
IsUnc  : False
LocalPath  : C:\utils\4utils.txt
PathAndQuery  : C:/utils/4utils.txt
Port  : -1
Query  :
Fragment  :
Scheme  : file
OriginalString : file:///C:/utils/4utils.txt
DnsSafeHost  :
IsAbsoluteUri  : True
Segments  : {/, C:/, utils/, 4utils.txt}
UserEscaped  : False
UserInfo  :
To use just the IsFile method;
Code:
([System.Uri]'http://jpsoft.com').IsFile
which returns

Code:
False
To call this from TCC;
Code:
powershell -noprofile -command ([System.Uri]'http://jpsoft.com').IsFile
Using with a local file as the argument, both of these examples return the same result;
Code:
PS C:\utils> ([System.Uri]'file:///C:/utils/4utils.txt').IsFile
True
PS C:\utils> ([System.Uri]'C:/utils/4utils.txt').IsFile
True
These all work with PowerShell 2.0 and above.

Joe
 
May 29, 2008
539
3
Groton, CT
Further to ISFILE with URL , PowerShell may provide a solution with System.Uri

...

Code:
PS C:\utils> ([System.Uri]'file:///C:/utils/4utils.txt').IsFile
True
PS C:\utils> ([System.Uri]'C:/utils/4utils.txt').IsFile
True
These all work with PowerShell 2.0 and above.

Joe
However, a reference to a nonexistent file still returns True, but should return False!
Code:
> powershell -noprofile -command ([System.Uri]'C:/nonexistent/x.txt').IsFile
True
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,797
48
Albuquerque, NM
prospero.unm.edu
I'll bet PowerShell is ultimately calling UrlCreateFromPath() and PathCreateFromUrl(). It should be possible to achieve the same end without the overhead of the bloated PowerShell via a plugin, or possibly by using @WINAPI to call those helper routines directly.
 
Interesting.

Looking into this further, it would seem that the IsFile method is not meant to determine whether a file exists or not, but "Gets a value indicating whether the specified Uri is a file URI."

Ditto for IsUNC. It does not determine if the UNC exists, but "Gets whether the specified Uri is a universal naming convention (UNC) path."

So, after determining if the Uri is a file URI;
Code:
test-path 'c:\nonexistent\x.txt'
False

test-path 'c:\utils\4utils.txt'
True
Or, after determining if the Uri is a file URI, just use the TCC IsFile;
Code:
if isfile c:\utils\4utils.txt (echo Yes) else (echo No)
Yes
Joe
 
I'll bet PowerShell is ultimately calling UrlCreateFromPath() and PathCreateFromUrl(). It should be possible to achieve the same end without the overhead of the bloated PowerShell via a plugin, or possibly by using @WINAPI to call those helper routines directly.
Code:
echo %@winapi[shlwapi.dll,PathIsURL,"http://jpsoft.com"]
1
Code:
echo %@winapi[shlwapi.dll,PathIsURL,"c:\utils\4utils.txt"]
0
Ref: Shell Path Handling Functions

For people who use TCC/LE, and who do not have the ability to create a plugin, PowerShell can still be a solution to this problem.

Joe
 
Last edited: