Solved

delete files in a specific folder on logoff

Posted on 2009-04-02
13
445 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:jdecaria
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24053249
if exist c:\snettemp rmdir /s /q c:\snettemp
0
 
LVL 6

Expert Comment

by:IKZ
ID: 24053275
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
 

Author Comment

by:jdecaria
ID: 24053458
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 67

Expert Comment

by:sirbounty
ID: 24053476
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
 

Author Comment

by:jdecaria
ID: 24053518
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24053624
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
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24053635
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
 

Author Comment

by:jdecaria
ID: 24054117
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24054135
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
 
LVL 6

Expert Comment

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

Justin Chandler
0
 

Author Comment

by:jdecaria
ID: 24060732
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24061223
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
 

Author Closing Comment

by:jdecaria
ID: 31565935
works like a charm... thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question