@Echo off

 rem #New: 08.06.2020/J.Fes
 rem #Doc: Checks if Users has ReadWrtie access to Directory
 rem #Doc: P1 = Disk / or root path
 rem #Doc: P2 = SubPath on Disk
 rem #Doc: P3 = EnvVar contains SubDir on success
 rem #Doc: If EnvVar P3 is defined no check is done
 rem #Exa: "DirChkCreate.cmd" D: "JobMgrL_T" JobMgr_RootL_DP

 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%
 set Path=%CmdCurRoot_DP%;%Path%

  if "%~1" equ "" goto :myEndError

 rem get Disk
 set FolderLinkTo_DP=%~1
 set FolderLinkPlaceTo_TP=%~nx1

 set FolderLinkPlaceToTmp_DP=%CmdCurRoot_DP%
 set FolderLinkPlaceTo_DP=%FolderLinkPlaceToTmp_DP%\%FolderLinkPlaceTo_TP%

REM  if not exist "%FolderLinkPlaceToTmp_DP%" md "%FolderLinkPlaceToTmp_DP%"

REM  Echo FolderLinkPlaceToTmp_DP:%FolderLinkPlaceToTmp_DP%
REM  Echo FolderLinkPlaceTo_TP:%FolderLinkPlaceTo_TP%
REM  Echo FolderLinkPlaceTo_DP:%FolderLinkPlaceTo_DP%

 Call :LinkCreate "Create Folder Link to:'%FolderLinkTo_DP%'"^
 "%FolderLinkTo_DP%"^
 "%FolderLinkPlaceTo_DP%"^
 ""^
 ""^
 ""^
 ""^
 "FolderLink"

 pause
 start "" Explorer "%FolderLinkPlaceToTmp_DP%"

goto :eof

:LinkCreate
 rem #New: 01.03.2021/J.Fes
 rem #LUp: 17.03.2023/J.Fes P9 = delete link
 rem #Doc: Create the Link via

 setlocal

REM  rem Doku:
REM  jfShellLinkCreate /LinkFile="c:\path\to\linkfile.lnk"
REM                   /LinkDest="c:\path\to\destfile.exe"
REM                  [/Arguments="/Option1 /Option2"]
REM                  [/WorkDir="c:\path\to\"]
REM                  [/IconFile="c:\path\to\iconfile.ext"]
REM                  [/DisplayMode="Normal|Max|Min"]
REM                  [/Description="Kommentar"]
REM                  [/Quiet] (keine Konsolenausgaben)

REM   Echo On
 set Description=%~1
 set Linkto_DP=%~2
 set LinkToSaveTo_FolderLnk=%~3
 set LinkFile_NE=%~nx3
 set IconFile_DPNE=%~4
 set Arguments=%~5
 set DisplayMode=%~6
 set WorkDir=%~7
 set FolderLink=%~8


 rem 17.03.2023
REM  if "%Arguments%" equ "" goto :Next
REM  Echo Arguments before=%Arguments%
REM  set Arguments=%Arguments:''="%
REM  Echo Arguments after=%Arguments%
REM  pause

:Next
 rem set /p= - Check File exist .............: %~1 <nul
REM  Echo - Create Folder Link for........: %Description%
 Echo.
 Echo.
 Echo.
 Echo.
 Echo - Create Folder Link to Dir.....: %Linkto_DP%
 Echo - Save Folder Link to...........: %LinkToSaveTo_FolderLnk%
 rem Echo - IconFile......................: %IconFile_DPNE%
 if not exist "%Linkto_DP%" goto :ErrExit

 set toRun=jfShellLinkCreate.exe /Quiet
 set toRun=%toRun% /LinkFile="%LinkToSaveTo_FolderLnk%"
 set toRun=%toRun% /LinkDest="%Linkto_DP%"
 if "%IconFile_DPNE%" neq "" set toRun=%toRun% /IconFile="%IconFile_DPNE%"
 if "%Arguments%"     neq "" set toRun=%toRun% /Arguments="%Arguments%"
 if "%DisplayMode%"   neq "" set toRun=%toRun% /DisplayMode="%DisplayMode%"

 if "%WorkDir%"       equ "" goto :next
 if "%WorkDir:~0,1%"  equ "#" Call :WorkDirSetValue
 if "%WorkDir%"       neq "" set toRun=%toRun% /WorkDir="%WorkDir%"
 :Next
 if "%Description%"   neq "" set toRun=%toRun% /Description="%Description%"
 if "%FolderLink%"    neq "" set toRun=%toRun% /FolderLink

 %toRun%
 goto :MyExit

 :ErrExit
 Echo - ERR Create Link file/dir nf...: '%Linkto_DP%'
 Echo - please check..................: Installation
 Pause
 :MyExit
 endlocal
goto :eof

:WorkDirSetValue
 set WorkDir=%WorkDir:#=%
 set PChar=%%%
 set WorkDir=%PChar%%WorkDir%%PChar%
 goto :eof


 rem
REM  if defined %~3 goto :myEnd
REM  rem exist Disk?
REM  if not exist "%FolderLinkTo_DP%" goto :myEnd
REM  set DirChkCreate_DP$tmpTest=%DirChkCreate_DP$tmp%\zzTempReadWrtieAccess%UserName%
REM
REM  rem Check create Dir
REM  if not exist "%DirChkCreate_DP$tmpTest%" md "%DirChkCreate_DP$tmpTest%"
REM  rem Check delete SubDir
REM  if exist "%DirChkCreate_DP$tmpTest%" rd /q "%DirChkCreate_DP$tmpTest%"
REM  rem if delete SubDir OK then set
REM  if "%errorlevel%" equ "0" if not exist "%DirChkCreate_DP$tmpTest%" set %~3=%DirChkCreate_DP$tmp%
 goto :myEnd

:myEndError
 Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----
 Color 04
 Echo Error an call...................: %0
 Echo ISSUE at leased one parameter...: is empty
 Echo #Value P1.......................: '%~1'
 Echo #Value P2.......................: '%~2'
 Echo #Value P3.......................: '%~3'
 Echo Please Check Call
 pause
 goto :myEnd

:myEnd
 set FolderLinkTo_DP=
 set DirChkCreate_DP$tmp=
 set DirChkCreate_DP$tmpTest=
 goto :eof

