:ConnectNetUnc
 rem #LUp 06.11.2016 / J.Fes
 rem Example: connect to share no disk
 rem call ConnectNetUnc "Info Description" .  \\HostName\Share Domain\User UserPassword  ResultConnect

 rem Example: connect to share with disk
 rem call ConnectNetUnc "Info Description" S: \\HostName\Share Domain\User UserPassword  ResultConnect

 rem Example: connect to share no disk not using User or password
 rem call ConnectNetUnc "Info Description" .  \\HostName\Share . .  ResultConnect

 rem Example: connect to share with disk not using User or password
 rem call ConnectNetUnc "Info Description" S: \\HostName\Share . . ResultConnect

 rem Example: connect to share with disk not using User or password
 rem call ConnectNetUnc "Info Description" S: "\\HostName\Share\sub folder with blank"  . . ResultConnect

 rem Parameter 6 is the Variable to get the result off the connetion
 rem             if every thing ist OK  value will be ResultConnect=Ok
 rem             if an error comes up value will be ResultConnect=Err

 setlocal
 set Description=%~1
 set LW=%~2
 set UNC=%~3
 set Us=%~4
 set Pw=%~5

 Echo Connect : %Description% [%LW% %UNC% via:%US%]

 if     "%LW%" equ "." goto LWNo
 if not "%LW%" equ ""  goto LWYes

 :LWno
   if not "%US%" equ "." net use "%UNC%" /user:%US% %Pw% 1>nul 2>nul
   if     "%US%" equ "." net use "%UNC%" 1>nul 2>nul
   set LwConnectionErrorlevel=%ErrorLevel%
   if not exist %UNC% Echo - Error : %UNC% !! NOT !! connected ErrCode=[%LwConnectionErrorlevel%]
   if     exist %UNC% Echo + OK    : %LW% succesful connected with %UNC%& set MyResult=Ok
   goto :ConnectNetUncEnd

 :LWYes
   set LwExist=false
   if exist %LW% set LwExist=true
   rem Fall: Laufwerk ist nicht verbunden !
   if /I "%LwExist%" equ "false" call :LwDoConnect& goto :LwConnectionPrintResult
   call :LwConnectionIsOkCheck

   rem Fall: Laufwerk ist falsch verbunden !!
   if /I "%LwExist%" equ "true" if /I "%LwConnectionIsOk%" equ "false" call :LwDoDisConnect& call :LwDoConnect& goto :LwConnectionPrintResult

   rem wenn keine der anderen Fälle eintritt dann PrintResults
   goto :LwConnectionPrintResult

   :LwDoDisConnect
    Echo Info    : %LW% has first to be disconnectet because %LW% is connected with the wrong unc path
    net use %LW% /d /y 1>nul 2>nul
    goto :eof

   :LwDoConnect
    Echo Info    : %LW% connecting now via "net use .."
    if not "%US%" equ "." net use %LW% "%UNC%" /user:%US% %Pw% 1>nul 2>nul
    if     "%US%" equ "." net use %LW% "%UNC%" 1>nul 2>nul
    goto :eof


   :LwConnectionIsOkCheck
    set LwConnectionIsOk=false
    net use %LW% |findstr /i /c:"%UNC%" 1>nul 2>nul
    set LwConnectionErrorlevel=%Errorlevel%
    if "%LwConnectionErrorlevel%" equ "0" set LwConnectionIsOk=true
    goto :eof

   :LwConnectionPrintResult
    Call :LwConnectionIsOkCheck
    if "%LwConnectionIsOk%" equ "false" Echo - Error : %LW% !! NOT !! connected with %UNC% ErrCode=[%LwConnectionErrorlevel%]& set MyResult=Err
    if "%LwConnectionIsOk%" equ "true"  Echo + OK    : %LW% succesful connected with %UNC%& set MyResult=Ok
    goto :ConnectNetUncEnd

 :ConnectNetUncEnd
 endlocal& set %6=%MyResult%




