3

I've made this batch script to delete some things from local app data and the redirected user folders.

This whole thing works quite well apart from one little snag....

   set share=\\SERVER\d$\tsprofiles\%USERNAME%.V2\AppData\Roaming
   ECHO Deleting..... %share%
   net use Y: %share% 
   cd /D Y:\
   del * /F /S /Q
   del * /F /Q
   net use Y: /DELETE /YES

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:

net use Y: %share%

No dice. I'm sure its not a permissions issue as I am running this from a domain admin account and an admin command prompt....

  • whats that single double quote doing there? – jiggunjer Oct 6 '16 at 2:32
  • huh weird. Thanks for pointing that out, unfortunately it doesn't have the first " in my code. I'll update the post – TacticalKitchen Oct 6 '16 at 2:36
  • net use Y: "%share%"? Some error message? Add pause to see if any. – JosefZ Oct 6 '16 at 2:51
  • @Homey_D_Clown_IT set share=\\SERVER\tsprofiles$\%USERNAME%.V2\AppData\Roaming is what it at now (still a hidden share unfortunately). Still no dice "The system cannot find the file specified.... JosefZ also I put the var in " " marks. Still no joy. Also I've tried both of the paths and stuck them into powershell with a Test-Path returning true on both of the different UNC paths... – TacticalKitchen Oct 6 '16 at 3:00
  • can someone look into this? Thanks superuser.com/questions/1415868/… – 遊星不動 Mar 21 at 5:43
2

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?

set share=\\SERVER\d$\tsprofiles\%USERNAME%.V2\AppData\Roaming
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:

SETLOCAL ENABLEDELAYEDEXPANSION

IF EXIST "\\SERVER\d$" (
   set share=\\SERVER\d$\tsprofiles\%USERNAME%.V2\AppData\Roaming
   ECHO Deleting..... "!share!"
   net use Y: "!share!"
   cd /D Y:\
   del * /F /S /Q
   del * /F /Q
   net use Y: /DELETE /YES
)

ENDLOCAL

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.

0

This ended up being like me over complicating the hell out of it all. So based on @David Woodward's suggestion I've done a bit of a re-design

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?

set share=\\SERVER\d$\tsprofiles\%USERNAME%.V2\AppData\Roaming
ECHO Deleting..... %share%
del "%share%\*" /F /S /Q
del "%share%\*" /F /Q

So what I ended up with is a much more streamlined script that is a whole lot better to read and a whole lot faster. Thank you all so much!

SET dir=%~dp0
SET userfolder="\\SERVER\userfolders$\%USERNAME%\AppData\Roaming"
SET tsprofiles="\\SERVER\TSPROFILES$\%USERNAME%.V2\AppData\Roaming"
SET localdata="C:\Users\%USERNAME%\AppData\Local\Temp"
SET inetcache="C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\INetCache"
REM Start of script
@ECHO OFF
ECHO [(===============================================================)]
ECHO ^|^|                                                               ^|^|
ECHO ^|^|  Remove un-neccecary files from %USERNAME% on %COMPUTERNAME%  ^|^|
ECHO ^|^|                                                               ^|^|
ECHO [(===============================================================)]
ECHO Deleting.... %inetcache%
del "%inetcache%\*" /F /S /Q > null
del "%inetcache%\*" /F /Q > null
ECHO Deleting.... %localdata%
del "%localdata%\*" /F /S /Q > null
del "%localdata%\*" /F /Q > null
ECHO Deleting.... Using UNC path %USERFOLDER%
del "%userfolder%\*" /F /S /Q > null
del "%userfolder%\*" /F /Q > null
ECHO Deleting.... Using UNC path %tsprofiles%
del "%tsprofiles%\*" /F /S /Q > null
del "%tsprofiles%\*" /F /Q > null
ECHO Return to working DIR
cd /D %dir%

I've posted it above if anyone finds it useful

  • Why do you cd /D %dir%? Your script never does a CD to anywhere else. – DavidPostill Oct 6 '16 at 22:53
  • It was mostly because running it from a Shortcut that launches it as Administrator does some weird things when referencing the next login script from within the batch. For some reason it will depending on how it feels at the time either go back to the right place or mysteriously find itself back to System32 – TacticalKitchen Oct 7 '16 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.