How to find if a drive letter exists?

Hi All,

Windows 7 & 10

I want to know if a drive letter exists:


K:\Windows\NtUtil>df -kPT
Filesystem           Type  1024-blocks      Used Available Capacity 
Mounted on
C:/Program Files/Git ntfs     40585620  15044068  25541552      38% /
D:                   udf       5294394   5294394         0     100% /d
E:                   ntfs         8188      3908      4280      48% /e
F:                   ntfs        10236      3908      6328      39% /f
J:                   smbfs   951048868 514547660 436501208      55% /j
K:                   smbfs   951048868 514547680 436501188      55% /k
L:                   smbfs   951048868 514547700 436501168      55% /l

K:\Windows\NtUtil>dir e:
  Volume in drive E is DRIVERS
  Volume Serial Number is 44EB-5181

  Directory of E:\

File Not Found

This exists:
K:\Windows\NtUtil>raku -e "say 'H:'.IO.d.Bool;"
True

This does not:
K:\Windows\NtUtil>raku -e "say 'Z:'.IO.d.Bool;"
True


Many thanks,
-T


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
5/17/2020 7:52:18 AM
perl.perl6.users 1504 articles. 0 followers. Follow

5 Replies
67 Views

Similar Articles

[PageSpeed] 18

On 2020-05-17 00:52, ToddAndMargo via perl6-users wrote:
> Hi All,
>=20
> Windows 7 & 10
>=20
> I want to know if a drive letter exists:
>=20
>=20
> K:\Windows\NtUtil>df -kPT
> Filesystem=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =
Type=C2=A0 1024-blocks=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Used Available Capac=
ity=20
> Mounted on
> C:/Program Files/Git ntfs=C2=A0=C2=A0=C2=A0=C2=A0 40585620=C2=A0 150440=
68=C2=A0 25541552=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 38% /
> D:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C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smbfs=C2=A0=C2=A0 951048868 51454=
7680 436501188=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 55% /k
> L:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 smbfs=C2=A0=C2=A0 951048868 51454=
7700 436501168=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 55% /l
>=20
> K:\Windows\NtUtil>dir e:
>  =C2=A0Volume in drive E is DRIVERS
>  =C2=A0Volume Serial Number is 44EB-5181
>=20
>  =C2=A0Directory of E:\
>=20
> File Not Found
>=20
> This exists:
> K:\Windows\NtUtil>raku -e "say 'H:'.IO.d.Bool;"
> True
>=20
> This does not:
> K:\Windows\NtUtil>raku -e "say 'Z:'.IO.d.Bool;"
> True
>=20
>=20
> Many thanks,
> -T
>=20
>=20

Follow up:

Hi All,

Prince213 on the chat line helped me figure this out.

This is from my IO keeper:

Drive (letter) exists (Windows only):
    use `IO.e.Bool`

    # forward slashes
    >raku -e "say 'X:/'.IO.e.Bool;"
    False

    >raku -e "say 'D:/'.IO.e.Bool;"
    True

    >raku -e "say 'd:/'.IO.e.Bool;"
    True

    >raku -e "say 'Z:/'.IO.e.Bool;"
    False

    # back slashes
    >raku -e "say 'd:\\'.IO.e.Bool;"
    True

    >raku -e "say Q[d:\].IO.e.Bool;"
    True

    >raku -e "say Q[z:\].IO.e.Bool;"
    False


-T
0
perl6
5/17/2020 9:56:32 AM
--000000000000ca113805a5d921e5
Content-Type: text/plain; charset="UTF-8"

Why do you have `.Bool` on all of the `.e` tests?

A file or directory either exists or it doesn't. So `.e` always returns a
Bool.
So there is zero reason to try to coerce it to a Bool.

You can look at the return value of `.e`.

    > say '.'.IO.can('e').head.signature
    (IO::Path:D: *%_ --> Bool)

If you don't trust that, you can look at the source
https://github.com/rakudo/rakudo/blob/fc88b9c2d2e71ab7fa492a53f0f90847c945bfcd/src/core.c/IO/Path.pm6#L738-L740

method e(IO::Path:D: --> Bool:D) {
nqp::hllbool(Rakudo::Internals.FILETEST-E(self.absolute)) }

It says it returns a Bool. It even uses `nqp::hllbool` which coerces the
value to `Bool`.

One of the design ethos of Raku is to reduce busy-work.
That is work that you don't really need to do.

I really can't understand why you insist on doing that busy-work anyway.


On Sun, May 17, 2020 at 4:56 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-05-17 00:52, ToddAndMargo via perl6-users wrote:
> > Hi All,
> >
> > Windows 7 & 10
> >
> > I want to know if a drive letter exists:
> >
> >
> > K:\Windows\NtUtil>df -kPT
> > Filesystem           Type  1024-blocks      Used Available Capacity
> > Mounted on
> > C:/Program Files/Git ntfs     40585620  15044068  25541552      38% /
> > D:                   udf       5294394   5294394         0     100% /d
> > E:                   ntfs         8188      3908      4280      48% /e
> > F:                   ntfs        10236      3908      6328      39% /f
> > J:                   smbfs   951048868 514547660 436501208      55% /j
> > K:                   smbfs   951048868 514547680 436501188      55% /k
> > L:                   smbfs   951048868 514547700 436501168      55% /l
> >
> > K:\Windows\NtUtil>dir e:
> >   Volume in drive E is DRIVERS
> >   Volume Serial Number is 44EB-5181
> >
> >   Directory of E:\
> >
> > File Not Found
> >
> > This exists:
> > K:\Windows\NtUtil>raku -e "say 'H:'.IO.d.Bool;"
> > True
> >
> > This does not:
> > K:\Windows\NtUtil>raku -e "say 'Z:'.IO.d.Bool;"
> > True
> >
> >
> > Many thanks,
> > -T
> >
> >
>
> Follow up:
>
> Hi All,
>
> Prince213 on the chat line helped me figure this out.
>
> This is from my IO keeper:
>
> Drive (letter) exists (Windows only):
>     use `IO.e.Bool`
>
>     # forward slashes
>     >raku -e "say 'X:/'.IO.e.Bool;"
>     False
>
>     >raku -e "say 'D:/'.IO.e.Bool;"
>     True
>
>     >raku -e "say 'd:/'.IO.e.Bool;"
>     True
>
>     >raku -e "say 'Z:/'.IO.e.Bool;"
>     False
>
>     # back slashes
>     >raku -e "say 'd:\\'.IO.e.Bool;"
>     True
>
>     >raku -e "say Q[d:\].IO.e.Bool;"
>     True
>
>     >raku -e "say Q[z:\].IO.e.Bool;"
>     False
>
>
> -T
>

--000000000000ca113805a5d921e5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Why do you have `.Bool` on all of the `.e` tests?<div><br>=
</div><div>A file or directory either exists or it doesn&#39;t. So `.e` alw=
ays returns a Bool.</div><div>So there is zero reason to try to coerce it t=
o a Bool.</div><div><br></div><div>You can look at the return value of `.e`=
..</div><div><br></div><div>=C2=A0 =C2=A0 &gt; say=C2=A0&#39;.&#39;.IO.can(&=
#39;e&#39;).head.signature</div><div>=C2=A0 =C2=A0=C2=A0(IO::Path:D: *%_ --=
&gt; Bool)</div><div><br></div><div>If you don&#39;t trust that, you can lo=
ok at the source=C2=A0<a href=3D"https://github.com/rakudo/rakudo/blob/fc88=
b9c2d2e71ab7fa492a53f0f90847c945bfcd/src/core.c/IO/Path.pm6#L738-L740">http=
s://github.com/rakudo/rakudo/blob/fc88b9c2d2e71ab7fa492a53f0f90847c945bfcd/=
src/core.c/IO/Path.pm6#L738-L740</a></div><div><br></div><div><table class=
=3D"gmail-highlight gmail-tab-size gmail-js-file-line-container" style=3D"b=
order-spacing:0px;border-collapse:collapse;color:rgb(36,41,46);font-family:=
-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-=
serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;;font-size:14=
px"><tbody style=3D"box-sizing:border-box"><tr style=3D"box-sizing:border-b=
ox"><td id=3D"gmail-LC738" class=3D"gmail-blob-code gmail-blob-code-inner g=
mail-js-file-line gmail-highlighted" style=3D"box-sizing:border-box;padding=
:0px 10px;line-height:20px;vertical-align:top;overflow:visible;font-family:=
SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,monospace;font-si=
ze:12px;white-space:pre;background-color:rgb(255,251,221)">    <span class=
=3D"gmail-pl-k" style=3D"box-sizing:border-box;color:rgb(215,58,73)">method=
</span> e(<span class=3D"gmail-pl-c1" style=3D"box-sizing:border-box;color:=
rgb(0,92,197)">IO</span>::Path<span class=3D"gmail-pl-k" style=3D"box-sizin=
g:border-box;color:rgb(215,58,73)">:</span><span class=3D"gmail-pl-k" style=
=3D"box-sizing:border-box;color:rgb(215,58,73)">D</span><span class=3D"gmai=
l-pl-k" style=3D"box-sizing:border-box;color:rgb(215,58,73)">:</span> <span=
 class=3D"gmail-pl-k" style=3D"box-sizing:border-box;color:rgb(215,58,73)">=
--&gt;</span> <span class=3D"gmail-pl-c1" style=3D"box-sizing:border-box;co=
lor:rgb(0,92,197)">Bool</span><span class=3D"gmail-pl-k" style=3D"box-sizin=
g:border-box;color:rgb(215,58,73)">:</span><span class=3D"gmail-pl-k" style=
=3D"box-sizing:border-box;color:rgb(215,58,73)">D</span>) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 nqp::hllbool(Rakudo::Internals.FILETEST-E(self.=
absolute))
    }</td></tr></tbody></table></div><div><br></div><div>It says it=C2=A0re=
turns a Bool. It even uses `nqp::hllbool` which coerces the value to `Bool`=
..</div><div><br></div><div>One of the design ethos of Raku is to reduce bus=
y-work.</div><div>That is work that you don&#39;t really need to do.</div><=
div><br></div><div>I really can&#39;t understand why you insist on doing th=
at busy-work anyway.</div><div><br></div></div><br><div class=3D"gmail_quot=
e"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, May 17, 2020 at 4:56 AM To=
ddAndMargo via perl6-users &lt;<a href=3D"mailto:perl6-users@perl.org">perl=
6-users@perl.org</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" =
style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pa=
dding-left:1ex">On 2020-05-17 00:52, ToddAndMargo via perl6-users wrote:<br=
>
&gt; Hi All,<br>
&gt; <br>
&gt; Windows 7 &amp; 10<br>
&gt; <br>
&gt; I want to know if a drive letter exists:<br>
&gt; <br>
&gt; <br>
&gt; K:\Windows\NtUtil&gt;df -kPT<br>
&gt; Filesystem=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 Type=C2=A0 1024-blocks=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Used Available Capaci=
ty <br>
&gt; Mounted on<br>
&gt; C:/Program Files/Git ntfs=C2=A0=C2=A0=C2=A0=C2=A0 40585620=C2=A0 15044=
068=C2=A0 25541552=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 38% /<br>
&gt; D:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 udf=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 5294394=C2=A0=C2=A0 5294394=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 0=C2=A0=C2=A0=C2=A0=C2=A0 100% /d<br>
&gt; E:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ntfs=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 8188=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 3908=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 4280=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 48% /e<br>
&gt; F:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ntfs=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 10236=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 3908=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 6328=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 39% /f<br>
&gt; J:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 smbfs=C2=A0=C2=A0 951048868 5145=
47660 436501208=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 55% /j<br>
&gt; K:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 smbfs=C2=A0=C2=A0 951048868 5145=
47680 436501188=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 55% /k<br>
&gt; L:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 smbfs=C2=A0=C2=A0 951048868 5145=
47700 436501168=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 55% /l<br>
&gt; <br>
&gt; K:\Windows\NtUtil&gt;dir e:<br>
&gt;=C2=A0 =C2=A0Volume in drive E is DRIVERS<br>
&gt;=C2=A0 =C2=A0Volume Serial Number is 44EB-5181<br>
&gt; <br>
&gt;=C2=A0 =C2=A0Directory of E:\<br>
&gt; <br>
&gt; File Not Found<br>
&gt; <br>
&gt; This exists:<br>
&gt; K:\Windows\NtUtil&gt;raku -e &quot;say &#39;H:&#39;.IO.d.Bool;&quot;<b=
r>
&gt; True<br>
&gt; <br>
&gt; This does not:<br>
&gt; K:\Windows\NtUtil&gt;raku -e &quot;say &#39;Z:&#39;.IO.d.Bool;&quot;<b=
r>
&gt; True<br>
&gt; <br>
&gt; <br>
&gt; Many thanks,<br>
&gt; -T<br>
&gt; <br>
&gt; <br>
<br>
Follow up:<br>
<br>
Hi All,<br>
<br>
Prince213 on the chat line helped me figure this out.<br>
<br>
This is from my IO keeper:<br>
<br>
Drive (letter) exists (Windows only):<br>
=C2=A0 =C2=A0 use `IO.e.Bool`<br>
<br>
=C2=A0 =C2=A0 # forward slashes<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say &#39;X:/&#39;.IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 False<br>
<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say &#39;D:/&#39;.IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 True<br>
<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say &#39;d:/&#39;.IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 True<br>
<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say &#39;Z:/&#39;.IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 False<br>
<br>
=C2=A0 =C2=A0 # back slashes<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say &#39;d:\\&#39;.IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 True<br>
<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say Q[d:\].IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 True<br>
<br>
=C2=A0 =C2=A0 &gt;raku -e &quot;say Q[z:\].IO.e.Bool;&quot;<br>
=C2=A0 =C2=A0 False<br>
<br>
<br>
-T<br>
</blockquote></div>

--000000000000ca113805a5d921e5--
0
b2gills
5/17/2020 2:47:45 PM
On 2020-05-17 07:47, Brad Gilbert wrote:
> Why do you have `.Bool` on all of the `.e` tests?

Hi Brad,

I do this because it keeps me out of trouble.

I want back a yes or no answer, not True or Fail
(X::IO::DoesNotExist).

And I never remember which IO.<some_letter> will return
a True or Fail or which function (`if` does) will
convert X::IO::DoesNotExist to a False for me.

Plus it makes it more readable for me.
    [something].IO.<someletter>.Bool
tells me instantly I will be getting back a yes
or no answer.

So if you want to be a purist, you can experiment with
which IO.<some_letter> are going to give you a yes
or no answer or which functions will convert X::IO::DoesNotExist for 
you.  OR you can just tack .Bool
at the end and forgo the hair pulling.

Also the cryptograms in the documentation are not
correct.  For instance:

     https://docs.raku.org/routine/d

     method d(--> Bool:D)

Says it return a "Bool" not a "True" or "X::IO::DoesNotExist".
The modification to Bool is stated at the bottom of the page.

So, basically, it is to keeps me out of trouble.  And if
I am going to get `X::IO::DoesNotExist` back instead of
a Boolean, .Bool automatically converts it for me and
with no hair pulling.

So, basically, my LONG WINDED answer is that I do it for me.

-T
0
perl6
5/17/2020 7:10:18 PM
Follow up:

When I go to maintain this thing in five days or five
years and I have one of those "what the h*** is IO.d.Bool
suppose to mean?" moments, I created the follow subs to make the code 
more readable/maintainable.

And mind you I can always look up what IO.d.Bool means
in my own IO documentation, but this makes it human readable.
Saving a lot of time.  Plus, since I program in Top Down,
I call all three of these all over the place in this program:


sub DirectoryExists( Str $DirPath     --> Bool ) { return 
"$DirPath".IO.d.Bool; }      # $Full DirPath format is `H:\MyDocsBackup`

sub DriveExists(     Str $DriveLetter --> Bool ) { return 
"$DriveLetter".IO.e.Bool; }  # $Drive Letter format is `A:\`

sub FileExists(      Str $FilePath    --> Bool ) { return 
"$FilePath".IO.f.Bool; }     # $File Path format is `H:\IAmBackup`


    if not  "%Options<DriveLetter>".IO.e.Bool {..}

versus

    if not  DriveExists( %Options<DriveLetter> ) {...}

And it is instantly understandable at a glance.  Put
a little extra effort up front and downstream is a
lot easier.

Also, I adore using `not` in place of False and not equal
as it is more human readable.

:-)

-T

We will let the optimizer take care of any of
my unneeded coercing.
0
perl6
5/18/2020 4:56:32 AM
On 2020-05-17 21:56, ToddAndMargo via perl6-users wrote:
> Follow up:

Thank you for all the help figuring all this out!
0
perl6
5/18/2020 4:57:31 AM
Reply: