:ConnectNetUnc
 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 Beschreibung=%1
 set LW=%2
 set UNC=%3
 set UNC=%UNC:"=%
 set Us=%4
 set Us=%Us:"=%
 set Pw=%5

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

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

 :LWno
   if not "%US%"=="." net use "%UNC%" /user:%US% %Pw% 1>nul 2>nul
   if     "%US%"=="." 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%"=="false" call :LwDoConnect& goto :LwConnectionPrintResult
   call :LwConnectionIsOkCheck

   rem Fall: Laufwerk ist falsch verbunden !!
   if /I "%LwExist%"=="true" if /I "%LwConnectionIsOk%"=="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%"=="." net use %LW% "%UNC%" /user:%US% %Pw% 1>nul 2>nul
    if     "%US%"=="." 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%"=="0" set LwConnectionIsOk=true
    goto :eof

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

 :ConnectNetUncEnd
 endlocal& set %6=%MyResult%




