Question Locked

Can some help me understand scripting for Windows?

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to get some type of automation setup that will monitor a folder and delete files that are more than 2 days old from the date the script runs which must be every day?

I am new to this and would appreciate help if you can. Or maybe you can point me in the direction of some other resources that can explain the fundamentals and how and where scripts are placed on the device and how they are created.

Thanks

10 replies

by 45geek

Excellent windows scripting, best way to manage systems.
You should find some help here if you already know something about scripting: http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

If you don't a good book to start with is "WSH and VBScript Programing, second Edition" by Jerry Ford (Thomson Course Technologies)
Also you can get some good info by taking the "Scripting Guys" tutorials free online just google "Scripting Guys" very helpful.
One other good one is the "Powershell" tutorial from microsoft. Interested in what your box can do "right out of the box" go to start > run > enter (wbemtest) then click on the go button when the box pops up change the highlighted portion to "\root\cimv2" without the quotes and then check the recursive box in the next window. That will list all the "Classes" available to you to script. not all are installed but with scripting you can access, add, edit anything with those boxes and that is just the begining....scripting a wonderful world...Have fun!!!!!

by Member 25348569

Don't just run it, though. Try and work out what it does. You would save this as, say, "Del2days.vbs" and run as a scheduled task. It deletes all files in a folder older than 2 days.

'-----------------------

Option Explicit

Dim objFSO, strFolderToClean, objFolder, colFiles, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get list of file details

strFolderToClean = "UNC path to your folder"

Set objFolder = objFSO.GetFolder(strFolderToClean)
Set colFiles = objFolder.Files

For Each objFile in colFiles
If DateDiff("D",objFile.DateLastModified,Now) > 2 Then
objFSO.DeleteFile objFile.Path
End If
Next

'-----------------------
Neil :)

To repeat. This script deletes files so find out what it's doing and test it!

by cclc

Thanks, for your feed back. I don't even know where to plave
this script, or how to get it to run. As I said my knowledge in
this area shallow at best.

by Member 25348569

You cut out the bit between the dashed lines, paste into Notepad and save it as del2days.vbs into the C:\Windows folder on the system on which you're going to run it - not necessarily on the system with the files to be deleted.

You change the line strFolderToClean = "UNC path to your folder" and replace "UNC path to your folder" with the path to the folder where you want to delete the files from i.e.

strFolderToClean = "\\server1\Logshare\Logfolder"

Create a scheduled task on the system where you saved the program to run the program at the interval and time that you want and run it under a user who has rights to delete files from the folder in question. The Scheduler runs on most systems and you get to it through Start | All Programs | Accessories | system tools on XP, for instance.

I can't really go any simpler than that.

If you have any specific issues or you get the script to run but it throws up an error (I do make typos sometimes) post again.

:)

by cclc

I have tried the script but an error is posted.
It designates line 10 char 20 syntax error.
This the the line with the UNC path.
This is the script below.

---------------------------------------------

Option Explicit

Dim objFSO, strFolderToClean, objFolder, colFiles, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get list of file details

strFolderToClean = \\charles-0079e7e\Documents and Settings\Administrator\My Documents\Files to delete

Set objFolder = objFSO.GetFolder(strFolderToClean)
Set colFiles = objFolder.Files

For Each objFile in colFiles
If DateDiff("D",objFile.DateLastModified,Now) > 2 Then
objFSO.DeleteFile objFile.Path
End If
Next

--------------------------------------------

by Member 25348569

strFolderToClean = [b]"[/b]\\charles-0079e7e\Documents and Settings\Administrator\My Documents\Files to delete[b]"[/b]

by Member 25348569

I'm a bit worried in hindsight about posting a script that deletes files.

Still, what's the worst that could happen?

by cclc

Not a problem, I'm testing this on dummy files and
folders.

Yes, as I said in my first post, I have now experience at all
with regard to programming or scripting. I would like to at
some point get a real ground up understanding of
scripting.

I am still getting an error, I appreciate your kelp and
patience.

Line 11 Char 1 Path Not Found

Option Explicit

Dim objFSO, strFolderToClean, objFolder, colFiles, objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get list of file details

strFolderToClean = "\\charles-0079e7e\Documents and
Settings\Administrator\My Documents\Files to delete"

Set objFolder = objFSO.GetFolder("\\charles-
0079e7e\Documents and Settings\Administrator\My
Documents\Files to delete")
Set colFiles = objFolder.Files

For Each objFile in colFiles
If DateDiff("D",objFile.DateLastModified,Now) > 2 Then
objFSO.DeleteFile objFile.Path
End If
Next

This discussion is closed to new replies.