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.
jdecariaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sirbountyConnect With a Mentor Commented:
My mistake - try this revised version:
 
@echo off
setlocal 
 
set folder=C:\snettemp
 
if exist "%folder%" (
  for /f %%a in ('dir %folder% /b /a-d') do echo del "%folder%\%%a"
  for /f %%a in ('dir %folder% /b /ad') do echo rmdir /s /q "%folder%\%%a"
  ) else (
  md "%folder%"
  echo Y|cacls "%folder%" /T /C /G administrators:C
  cacls "%folder%" /T /E /C /G "Domain Users":C
)

Open in new window

0
 
sirbountyCommented:
if exist c:\snettemp rmdir /s /q c:\snettemp
0
 
IKZCommented:
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

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jdecariaAuthor Commented:
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?
0
 
sirbountyCommented:
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

0
 
jdecariaAuthor Commented:
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.
0
 
sirbountyCommented:
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

0
 
Justin_W_ChandlerCommented:
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
0
 
jdecariaAuthor Commented:
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
0
 
sirbountyCommented:
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

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

Justin Chandler
0
 
jdecariaAuthor Commented:
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

0
 
jdecariaAuthor Commented:
works like a charm... thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.