[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

delete files in a specific folder on logoff

Posted on 2009-04-02
13
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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