PDA

View Full Version : Script help



Frankthetank1
04-14-2011, 07:34 PM
I need help creating a script for my work. Right now I have this script called move scan files.bat and I have it schedled to work at 5 pm every day.* Here is the script
*
@echo off
copy "Y:\Scanned\*.*" "Y:\scanned1\"
End
*
I want to create a new script that will delete files that are older then 7 days in that folder called scanned1.

anon
04-14-2011, 09:44 PM
http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days

Frankthetank1
04-15-2011, 02:53 PM
I tried it with and with out the first and last lines still get a windows compilation error. Just save it as a VBS file and run it to see if it works right? I guess, I don’t get run this in command prompt…. How?
*
'copy from here
*** Function DeleteOlderFiles(Y:\Old Scanned Doc's)
****** Dim fso, f, f1, fc, n, ThresholdDate
****** Set fso = CreateObject("Scripting.FileSystemObject")
****** Set f = fso.GetFolder(Y:\Old Scanned Doc's)
****** Set fc = f.Files
****** Set objArgs = WScript.Arguments
****** n = 0
****** If objArgs.Count=0 Then
********** howmuchdaysinpast = 0
****** Else
********** howmuchdaysinpast = -objArgs(0)
****** End If
****** ThresholdDate = DateAdd("d", howmuchdaysinpast, Date)**
****** For Each f1 in fc
**** If f1.DateLastModified<ThresholdDate Then
******* Wscript.StdOut.WriteLine f1
******* f1.Delete
******* n = n + 1***
**** End If
****** Next
****** Wscript.StdOut.WriteLine "Deleted " & n & " file(s)."
*** End Function
*
*** If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
***** WScript.Echo "USAGE ONLY IN COMMAND PROMPT: cscript DelOldFiles.vbs 15" & vbCrLf & "15 means to delete files older than 15 days in past."
***** WScript.Quit 0**
*** End If
*
*** DeleteOlderFiles(".")
'to here
*

tesco
04-15-2011, 10:54 PM
First of all, might be stupid of me to say, but don't copy the "'copy from here" or the "'to here".
Second, when he says "USAGE ONLY IN COMMAND PROMPT: cscript DelOldFiles.vbs 15", do it like this:
start -> run -> type "CMD" -> click OK -> then type "cscript DelOldFiles.vbs 15".
You might need to add the path to the file to that line.

edit: Just noticed you buggered up the code, here use this:



Function DeleteOlderFiles(whichfolder)
Dim fso, f, f1, fc, n, ThresholdDate
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(whichfolder)
Set fc = f.Files
Set objArgs = WScript.Arguments
n = 0
If objArgs.Count=0 Then
howmuchdaysinpast = 0
Else
howmuchdaysinpast = -objArgs(0)
End If
ThresholdDate = DateAdd("d", howmuchdaysinpast, Date)
For Each f1 in fc
If f1.DateLastModified<ThresholdDate Then
Wscript.StdOut.WriteLine f1
f1.Delete
n = n + 1
End If
Next
Wscript.StdOut.WriteLine "Deleted " & n & " file(s)."
End Function

If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
WScript.Echo "USAGE ONLY IN COMMAND PROMPT: cscript DelOldFiles.vbs 15" & vbCrLf & "15 means to delete files older than 15 days in past."
WScript.Quit 0
End If

DeleteOlderFiles(".")
Edit the very LAST line that says "DeleteOlderFiles(".")", replace the dot(.) with the folder name.

Frankthetank1
04-15-2011, 11:21 PM
yeah i am a noob when it comes to scripts :( I will try this Tuesday.

If this works i might try a script to do my timesheet at work so i wont even have to show up :)

Glaucon
04-23-2011, 11:05 PM
If you'd like I can patch you something up in C, although it might be overkill since this can easily be achieved with batch.

Frankthetank1
04-24-2011, 01:20 AM
it works well but i need to figure out how to run it in a scheduled task. i think i can figure it out though...I just need to put cscript in front of the name of the vbscript and save in a cmd file

anon
04-24-2011, 02:34 AM
The association for VBS files in the registry should look like this:

"%SystemRoot%\System32\WScript.exe" "%1" %*

Just replace %1 with the full path to your file (keep the quotes) and use that.

Glaucon
04-24-2011, 09:16 AM
it works well but i need to figure out how to run it in a scheduled task. i think i can figure it out though...I just need to put cscript in front of the name of the vbscript and save in a cmd file

If you are on Windows then try running taskschd.msc and setting it up there.