Results 1 to 11 of 11
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Does UNC Drive Exist

    Is it possible to use DOS commands to determine if a UNC drive exists: I have a thumbdrive that I copy information to and wanted to create a batch file that recognized the UNC of the drive; not the drive letter assignment.

    Something like:
    If Exist TRAVELDRIVE GoTo Found_1
    If Not Exist TRAVELDRIVE GoTo Exit

    :Found_1
    Echo Found It
    Pause
    Goto Exit
    :Exit
    End

    Thanks,
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Does UNC Drive Exist

    Which version of Windows are you using? (I'll move the thread to the appropriate forum, this is not a web design question)

  3. #3
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does UNC Drive Exist

    XP Professional with SP2

  4. #4
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Does UNC Drive Exist

    Since what you appear to be trying to find is the Volume Name, rather than a sharename, or whatever, the only way I can think of to achieve this is something like: <code>

    @echo off
    setlocal
    set volname=TRAVELDRIVE
    :: initially set drive to null/nonexistent
    set drive=
    :: test a reasonable set of drive letters
    for %%a in (C D E F G H) do call rocess %%a

    if defined drive echo %~n0: volume %volname% was found on drive %drive%
    endlocal
    goto :eof
    ::---------------------------------------------------------------------

    rocess the drive letter passed as %1
    set letter=%1
    :: we are looking for the message
    :: "Volume on drive x is yyyyyyyy"
    :: remove this line and the next line when you are happy with the results
    DIR %LETTER%: | FINDSTR /c:"Volume in drive"

    set forcmds=dir %letter%: 2^^^> nul ^^^| findstr /c:"Volume in drive"

    for /f "tokens=6" %%a in ('%forcmds%') do (
    if %errorlevel% EQU 0 set set drive=%letter%
    )
    goto :eof</code>

    Cut and paste it into Notepad and save it as FINDVOL.BAT, or some such, and see if it does what you want. If any explanation is needed, just ask!

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  5. #5
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does UNC Drive Exist

    John,

    It did find the volume of the USB drive named "TRAVELDRIVE".

    How would one include a IF statement in the code such as:

    If EXISTS TRAVELDRIVE GoTo ContineWithSomeCode

    I can not get the line above to recognize the volname of TRAVELDRIVE and then execute more code.

    Thanks,
    John

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Does UNC Drive Exist

    Since John isn't on line at the moment, I'll offer a suggestion...

    Where he has the line
    <code>if defined drive echo %~n0: volume %volname% was found on drive %drive%</code>

    you could put
    <code>if defined drive GoTo ContineWithSomeCode</code>

    StuartR

  7. #7
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Does UNC Drive Exist

    <hr>If EXISTS TRAVELDRIVE GoTo ContineWithSomeCode<hr>
    Stuart has given you the answer.

    You are confusing the Volume Name of a disk drive with the UNC convention of servernamesharename (or pcnamesharename).

    The BATch file IF EXIST command only works off the filename in the form [drive:][directoryname][filename], as in
    IF EXIST C:BOOT.INI <do something>

    An alternative might be
    IF EXIST <pcname>D$filename.ext <do something>

    Remember that BATch files were around before UNC conventions, so many of the commands only work off drive letters, hence you often have to do
    NET USE y: servernamesharename
    to be able to do anything useful.

    But nowhere can you use the Volume Name in the way you are trying to do!

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  8. #8
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does UNC Drive Exist

    John,

    Now I'm confused. If one is able to identify the volume name and it's associated drive letter then it should be conceivable that files can be copied from the hard drive to the USB drive letter. One thing to note is that when the USB drive is connected it is not always assigned to the same drive letter. I do not want to force the USB drive to be assigned to a specific drive letter.

    John

  9. #9
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Does UNC Drive Exist

    After you have run John's code, you will find that the environment variable <code>%drive%</code>
    has been defined with the drive letter that you need.

    You can then issue commands like
    <code>COPY C:folderfile.ext %drive%:folder</code>


    StuartR

  10. #10
    Platinum Lounger
    Join Date
    Jan 2001
    Location
    Quedgeley, Gloucester, England
    Posts
    5,333
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Does UNC Drive Exist

    Here is a shorter (and hopefully more intelligible!) version of the previous BATch file:<pre>@echo off
    setlocal
    set volname=TRAVELDRIVE
    set drive=

    :: test a reasonable set of drive letters
    for %%a in (C D E F G H) do (
    for /f "tokens=6" %%b in ('dir %%a: 2^>nul ^| findstr /c:"Volume in drive"') do (
    if /i "%%b" EQU "%volname%" set drive=%%a
    )
    )
    if defined drive echo %~n0: volume %volname% was found on drive %drive%

    :: alternatively...
    if defined drive goto drivefound</pre>

    You will see that I am testing drive C:, then D: then E... looking for a drive where a DIR gives me the required volume name, or not. If at the end the variable %drive% exists, then it contains the (current) drive letter for the volume TRAVELDRIVE.

    Stuart has shown how you can use it!

    John
    <font face="Script MT Bold"><font color=blue><big><big>John</big></big></font color=blue></font face=script>

    Ita, esto, quidcumque...

  11. #11
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Does UNC Drive Exist

    Thank you John and Stuart I got it to work.

    Regards,
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •