• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 17311
  • Last Modified:

How do I pass Parameters to a vbscript program?

I have a small vbscript program that I inherited that is used to delete archive and log files from a directory based on date.  All the directories and "older than"  numbers are hard coded.  I would like to pass parameters to this file but can't figure out how to do it.  %1 doesn't work.  I ordered two vbscript books but they won't be in for awhile and I'd like to keep going.  Can I set a variable in a calling program and read it?  How do I accomplish this?   I want to pass two things.  A directory name and a number that represents the max number of days to retain.  The code is included below.  

myDir = "F:\oracle\backups\NEDARCD\Recent_Exports\"
WScript.Echo " Current Directory is F:\oracle\backups\NEDARCD\Recent_Exports\"

SET filesystem = CreateObject("Scripting.FileSystemObject")
SET folder = filesystem.GetFolder(myDir)

SET filecollection = folder.Files

FOR Each file IN filecollection
     IF DateDiff("d", CreationDate(myDir & file.name), Now()) > 6 THEN
       WScript.Echo DateDif("d", CreationDate(myDir & file.name),Now()) & " " & file.name & " " & CreationDate(myDir & file.name)
       DeleteFiles(myDir & file.name)
     END IF

SET filesystem=nothing
SET folder=nothing
SET filecollection=nothing

FUNCTION CreationDate(filespec)
  Dim fso, f
  SET fso = CreateObject("ScriptINg.FileSystemObject")
  SET f = fso.GetFile(filespec)
  CreationDate = f.DateCreated
END Function

FUNCTION DeleteFiles(filespec)
  DIM fso
  SET fso = CreateObject("Scripting.FileSystemObject")
END Function

  • 2
  • 2
1 Solution
Add this

If (WScript.Arguments.Count > 0) Then
      FilesOlderThan = WScript.Arguments(0)
      FilesOlderThan = 6
End If

and then later use FilesOlderThan in place of 6.
DonFreemanAuthor Commented:
So wscript.arguments(0) would be my first argument and that would be my directory name and then wscript.arguments(1) would be my second one right?.   I am quitting for the day but I'll get back on it Monday and when I get it working I'll award your points.  It looks like a solid solution.  Thanks!
Yes. WScript.Arguments.Count is the count of arguments passed. so you can check that too to make sure 2 arguments were indeed passed.
DonFreemanAuthor Commented:
I am still working on this...

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now