For some reason the path that the var share creates navigates to the UNC path in explorer that I'm trying to map to Drive Y: yet when using:
I'm not sure what you mean by "navigates to the UNC path". Do you mean it changes to that directory instead of creating a share?
Also, I'm not sure why you need to map the UNC share to delete its contents. Couldn't you just use the UNC path directly like below?
ECHO Deleting..... %share%
del "%share%\*" /F /S /Q
del "%share%\*" /F /Q
Also, I noticed your example is indented. Is that snippet from a condition/loop block enclosed in parenthesis? For example
IF EXIST "\\SERVER\d$" ( <your script lines here> ).
If the script is nested in a parenthesis block, you should use delayed expansion on the
share variable using exclamation points instead of percentage symbols like this:
IF EXIST "\\SERVER\d$" (
ECHO Deleting..... "!share!"
net use Y: "!share!"
cd /D Y:\
del * /F /S /Q
del * /F /Q
net use Y: /DELETE /YES
Otherwise, the share variable will always be the same value it was when the parenthesis block was entered into instead of the value you set it to.
And, as has already been stated in the comments, you need to quote the share in case
%USERNAME% has spaces in it.