Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete files at logoff/shut down 1

Status
Not open for further replies.

bgfs

Technical User
Apr 6, 2005
130
GB
I need to delete files from a particular folder during log off or shut down. anyone no the simplest way and how to do it? Script or batch
 
cricket
I would also be interested to know how its done, can you provide an example and how to fire it up (presume during machine boot sequence?)


If IT ain’t working Binnit and Reboot
 
Group policy? (run gpedit.msc). Options to run jobs at logoff (user configuation) or shutdown (computer configuration) under Windows Settings, Scripts. Supply batch file/script to do what you want.
 
to set up login/logoff scripts, first go here:


and follow the directions for getting the Group Policy Object going.

Next, expand Window Settings and select Scripts (Login/Logoff). Double click on either Logon or Logoff (whichever you want to set up) and add your script.

Another URL that may be handy is:

 
A related question: How can you programmatically determine within a VBScript whether the PC is in the process of (A) a logoff/shutdown or (B) a startup operation? In other words, how can a script identify the conditions (logoff, shutdown, startup) under which it has been invoked?
 
sbrews
These links are very helpful and clarify how and where to put the scripts, what I think I need know is the actual scripting commands/syntax etc.

Do you have any further links for learning the script?

For example, how would you write a script to empty the Temporary internet files from the user account being logged off? (I know there are settings that do this with IE)

What sort of file would this be .txt

Where would you store it before putting a reference to it in the GPolicy?

Apologies if I'm being dumb!


If IT ain’t working Binnit and Reboot
 
Binnit,

You can do this sort of thing with both batch files (text files ending with .BAT or .CMD file extensions that are processed by XP's built-in commandline interpreter) and scripts (text files ending with .VBS or .JS that are processed by XP's built-in Windows Scripting Host [WSH] interpreter).

For example, if you wanted to delete the contents of a folder called 'Temp' in the root of C: then it may be easier to force the deletion of the folder itself (which would include its contents) then recreate it.

Try this:

1) In the root of C:, create a folder called 'Temp'.
2) Copy (not 'Move') some files to this newly created folder.
3) 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.

Code:
' Set up the scripting environment
Option Explicit
Const oSrc = "C:\Temp"
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()

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

5) 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).

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

7) Click on the 'Browse' button.

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

More info about scripting (batch/cmd/wsh) can be found here on Tek-Tips in the scripting forums.

Hope this helps...
 
Rick998
This is a really helpful start, I want to explore more in to this and it will definately provide me with a starting point. I will check out the scripting forum too.

Many thanks

If IT ain’t working Binnit and Reboot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top