Solved

delete files in a specific folder on logoff

Posted on 2009-04-02
13
441 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now