Link to home
Start Free TrialLog in
Avatar of jdecaria
jdecariaFlag for Canada

asked on

delete files in a specific folder on logoff

We use software at the office that creates various files in a specific folder. In order to comply with privacy legislation, we can't have these files remaining on the local machine in case the computer is lost or stolen. I want to create a script that i will assign to run at logoff that checks to see if  "c:\snettemp" exists. If it exists, then premanently delete all subdirectories and files.
This script will run at logoff for all users, but the computer they might be logged on to might not have this directory, so I need to account for that. I'm just not sure of the syntax.
Avatar of sirbounty
sirbounty
Flag of United States of America image

if exist c:\snettemp rmdir /s /q c:\snettemp
Copy/paste the following into Notepad then save as 'DeleteTemp.vbs'. You can save this file anywhere but it will make it easier for this example to save it to the 'C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown' folder.

Open the Group Policy Editor (run 'gpedit.msc').

If you want the 'DeleteTemp.vbs' script to run on startup or shutdown then navigate to 'Computer Configuration > Windows Settings > Scripts(Startup/Shutdown). Alternatively, if you want the 'DeleteTemp.vbs' script to run on logon or logoff then navigate to 'User Configuration > Windows Settings > Scripts(Logon/Logoff).

Select (double-click on) the event you want to run the script (Startup/Shutdown/Logon/Logoff) then click on the 'Add' button.

Click on the 'Browse' button.

Select the script you want to run then click on the 'Open' then 'OK' buttons.


' Set up the scripting environment
Option Explicit
Const oSrc = "C:\snettemp"
Dim Text, Title
Dim WSH, FSO, oFolders, oFolder, oSubFolders ' Object variables
Dim oFileCount, oFolderCount, oFiles, oCount
Text = "Folders" & vbCrLf & vbCrLf
' Dialog title (inc. vanity bit)
Title = "Temp File Deleter"
 
' Create Windows Scripting Host Shell object
Set WSH = WScript.CreateObject("WScript.Shell")
' Create FileSystem object to access the file system.
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
 
'Delete the folder (forcibly, to handle read-only attributes) then re-create it
FSO.DeleteFolder oSrc,True
FSO.CreateFolder oSrc
 
'Free memory used by objects
Set WSH = Nothing
Set FSO = Nothing
 
WScript.Quit()

Open in new window

Avatar of jdecaria

ASKER

IKZ: i'm not that well-versed in VBS scripting - will this script remove the c:\snettemp\ folder or just delete it's contents? I need the folder to remain, just its contents permanently deleted. Is this perferable to using a batch script?
Basically does the same thing as this - recreates the folder after removing it..
if exist c:\snettemp rmdir /s /q c:\snettemp & md c:\snettemp

Open in new window

This is for users on a domain. The security parameters are set up such that only domain users have read/write access to this folder. If I delete this folder and recreate it, i don't think it will be recreated with the same NTFS permissions.
This will leave the folder intact.
Double-check the code by launching as-is first.
Once satisfied, remove the 'echo' from both lines...and it will delete/remove the folders & files.
set fld=c:\snettemp
for /f %%a in ('dir %fld% /b /a-d') do echo del "%fld%%%a"
for /f %%a in ('dir %fld% /b /ad') do echo rmdir /s /q "%fld%%%a%"

Open in new window

Put these two lines in a batch file and assign it to run at logoff: The first deletes all files in the root of snettemp and the second line removes any subdirectories. This will also leave the snettemp folder intact.

del /f /q c:\snettemp\*.*
for /f "tokens=*" %%A in ('dir /ad /b c:\snettemp') do rd /s /q "%%A"

Justin Chandler
Sirbounty:
not all of the computers the users might log on to have this folder only computers that have a terminal emulation program installed. This script needs to make sure that the folder exists before running these commands
just one line needed for that..
set fld=c:\snettemp
if not exist %fld% goto :eof
for /f %%a in ('dir %fld% /b /a-d') do echo del "%fld%%%a"
for /f %%a in ('dir %fld% /b /ad') do echo rmdir /s /q "%fld%%%a%"

Open in new window

It's the same either way -- because if the folder does not exist it won't do anything.

Justin Chandler
i'm trying to combine your script with some commands that check to see if c:\snettemp has files and/or in it, and if it does to delete them.  it's not deleting the files...  these scripts work when run separately... am i missing a command somewhere?
@echo off
setlocal
 
set folder=c:\snettemp
 
if not exist "%folder%" (
 md "%folder%"
 echo Y|cacls "%folder%" /T /C /G administrators:C
 cacls "%folder%" /T /E /C /G "Domain Users":C
)
 
if not exist %folder% goto :eof
for /f %%a in ('dir %folder% /b /a-d') do del "%folder%%%a"
for /f %%a in ('dir %folder% /b /ad') do rmdir /s /q "%folder%%%a%"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
works like a charm... thanks!