0

Recently I got a script to query the reg set a value and then delete folders under the folder I puled from the query. Looks like this:

pause

FOR /F "TOKENS=2*" %%I IN ('REG QUERY "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\14.0\Outlook\Security" /V OutlookSecureTempFolder') Do SET "ValueData=%%J"

pause

echo Delete Outlook Temp. Files???
echo Enter to continue or Ctrl+C to cancel.

pause

del /q /f /s "%valuedata%\*.*"

del /q /f /s "%systemdrive%\Jacob'sTemp"

pause
echo --------------------------------------------------------------------------------
echo Complete!  Goodbye!
echo --------------------------------------------------------------------------------


timeout /t 3

Works great when used locally. So I set up a robo copy to copy a folder with this script in it then use psexec to execute it remotely and it looks like this:

set /p cpu=

robocopy "\\nmcfs01\software\scripts\Jacob's Awesome Outlook Scripts" \\%cpu%\c$\Jacob'sTemp

pause

psexec \\%cpu% -u administrator "%systemdrive%\Jacob'sTemp\outlooktempdelete.bat"


pause

Now it works and it will run but here is the kicker when it goes back to the reg query batch to do the reg query it will run but it skips the 1st pause following the query and it always says it can not find the registry key but I can follow the path and it is there. The worst part is once I end the script there it wipes the computer of everything the user has access to. Not folders but all files/subfiles everywhere. Any insight is greatly appricated!

Leavii
  • 82
  • 2
  • 12
  • Sounds interesting. Did you check for any random spaces that shouldn't be there or any misplaced characters in the code? I'm feeling skeptical about running this on my Pc... – Jonas Sep 16 '16 at 19:44
  • you can run the reg query on your pc and it is fine as long as the key is there. I think I need an IF in there so if the key/ registry isn't there it will stop it. Each time it has wipped a computer it says the reg key is not there. But yeah I have checked the script several times and looks fine to me but I am quite new to making scripts and idk jack about them really. Just copy, paste, cross fingers? lol – Leavii Sep 16 '16 at 20:01
  • You might need to add some permissions to that robocopy. – Jonas Sep 16 '16 at 20:11
  • The robo copy is fine it copies the files over no problem it doesn't wipe the computer till it hits the reg query. – Leavii Sep 16 '16 at 20:27

1 Answers1

1

Here's a quick tidy up of your script:

@Echo Off
SetLocal EnableExtensions

(Set OV=14.0)

Choice /C YN /M "Delete Outlook Temp Files?"
If ErrorLevel 2 Exit/B
Set "BK=HKCU\SOFTWARE\Microsoft\Office\"
Set "EK=\Outlook\Security"
Set "VN=OutlookSecureTempFolder"
For /F "Tokens=2*" %%I In ('Reg Query "%BK%%OV%%EK%" /V %VN%') Do Set "VD=%%J"
PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD
REM The below commands will empty Jacob'sTemp:
If Exist "%SystemDrive%\Jacob'sTemp" (PushD "%SystemDrive%\Jacob'sTemp" && (
    (RD/S/Q "%SystemDrive%\Jacob'sTemp" 2>Nul) && PopD
REM The below commands without the first two characters will remove Jacob'sTemp
::If Exist "%SystemDrive%\Jacob'sTemp" (RD/S/Q "%SystemDrive%\Jacob'sTemp"
Pause
Echo(------------------------------------------------------------------------------
Echo( Complete!  Goodbye!
Echo(------------------------------------------------------------------------------
Timeout 5 >Nul

Give that a try exactly as it is just to see if it makes any difference.

I left the lonely but editable 'set' at the top because this is the only bit that need's changing to cater for earlier or later editions of outlook.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • So I may be missing it but where in there does it delete to files under the OutlookSecureTempfolder? That is what I am aiming for. I just thought in the one script I could copy the folder with the script in it to delete the files remotely but locally on the computer I am targeting. The Jacob's temp is not on every computer it was only coming from the one script that made it then removed after the reg was deleted. I am currently reimaging the laptop I tested mine on so as soon as it is done I'll give this a shot though. – Leavii Sep 16 '16 at 20:23
  • `PushD "%VD%" && (RD/S/Q "%VD%" 2>Nul) && PopD` What it is supposed to do is to recursively remove the OutlookSecureTempFolder and all its Sub Folders. The trick in this case is that the script moves into the OutlookSecureTempFolder, preventing that from actually being removed, *but all of its content should be*. – Compo Sep 16 '16 at 20:32
  • And the OutlookSecureTempFolder is the last folder in the path that it gives in the registry? When I look in my reg it gives me the path of C:\Users\jaj\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook\8R7ATMAB and the last folder 8R7ATMAB is what I am wanting to clean out just to clarify. You are probably right but so I do not screw anything else up I just want to make sure I am understanding correctly lol. – Leavii Sep 16 '16 at 20:44
  • Also I just got a PC up that I can test this on. Should it make a difference if I use it remotely with psexec or locally on the PC? – Leavii Sep 16 '16 at 20:47
  • If that's the path ouput by the Reg Query, then that's where it's moving to and subsequently emptying. Also the main difference between the remote use and local use will probably be the running/current directory. – Compo Sep 16 '16 at 20:48
  • It did not wipe the PC so that is a start but the switch it is hooked to for internet is not on our network and I'm heading out so I will try it remotely Monday and get back on this post if something went wrong. Thanks again Compo!!!! – Leavii Sep 16 '16 at 21:02
  • So I tried the script remotely using psexec and it will execute the batch file but it says, " The system was unable to find the specified registry key or value." Error code 255. But if I double click the script from where I copied it over it runs and cleans out the file and works fine. – Leavii Sep 19 '16 at 15:54
  • Also it runs through the script and passes the pause and the timeout. Here is what I use for running it remotely. psexec \\%cpu% -u administrator -i -s "%systemdrive%\Jacob'sTemp\compo.bat" – Leavii Sep 19 '16 at 17:22
  • Can I just check that you are sure you're not supposed to be running `"%%systemdrive%%\Jacob…` – Compo Sep 19 '16 at 17:58
  • Oh you mean %%systemdrive%% not just %systemdrive%. I will give it a shot – Leavii Sep 19 '16 at 18:22
  • That did not make a difference I posted a new question on how to get it to work remotely. http://stackoverflow.com/questions/39580637/reg-query-in-script-not-working-when-executed-remotely-with-psexec I'm such a noob when it comes to this site and giving links and greying out things -.- – Leavii Sep 19 '16 at 21:51