Solved

getting script to run every hour

Posted on 2011-03-07
6
563 Views
Last Modified: 2012-05-11
hi,

i have this script and i was wondering if there was a way to have it running every hour?

'------------------------------------------------------------------------------
Const appName = "FTP Upload Utility"
'------------------------------------------------------------------------------

Const Hostname = "myhostname"
Const Port = 21
Const Username = "myusername"
Const Password = "xxxxxxxxx"
Const RemoteDir = "/fm104"
Const strExt = "mp3"
Const LocalDir = "c:\mp3"
Const MaxAge = 10

SET objFSO = CreateObject("Scripting.FileSystemObject")

Set ObjFolder = objFSO.GetFolder(LocalDir)
FOR EACH objFile in ObjFolder.Files
  if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
    if datediff("s", objFile.DateLastModified, Now) > 40 Then
      Upload hostname, port, username, password, objFile.Name, localDir, remoteDir
      'objFile.Delete  
    End If
  End If
Next

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

Sub Upload(hostname, port, username, password, localFile, localDir, remoteDir)
  Set shell = CreateObject( "WScript.Shell" )
  Set fso = CreateObject("Scripting.FileSystemObject")

  tempDir = shell.ExpandEnvironmentStrings("C:\temp")
  ' temporary script file supplied to Windows FTP client
  scriptFile = tempDir & "\" & fso.GetTempName
  ' temporary file to store standard output from Windows FTP client
  outputFile = tempDir & "\" & fso.GetTempName


  'input script
  script = script & "lcd " & """" & localDir & """" & vbCRLF
  script = script & "open " & hostname & " " & port & vbCRLF
  script = script & "user " & username & vbCRLF
  script = script & password & vbCRLF
  script = script & "cd " & """" & remoteDir & """" & vbCRLF
  script = script & "binary" & vbCRLF
  script = script & "prompt n" & vbCRLF
  script = script & "put " & """" & localFile & """" & vbCRLF
  script = script & "quit" & vbCRLF

  Set textFile = fso.CreateTextFile(scriptFile, True)
  textFile.WriteLine(script)
  textFile.Close
  Set textFile = Nothing

  ' bWaitOnReturn set to TRUE - indicating script should wait for the program
  ' to finish executing before continuing to the next statement
  shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE
  Wscript.Sleep 500
  ' open standard output temp file read only, failing if not present
   Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2)
 If Not textFile.AtEndOfStream Then
    Results = textFile.ReadAll
 End If
 textFile.Close
  If InStr(results, "550") > 0 And (InStr(results, "226") or Instr(results, "221")) Then
    fso.DeleteFile(scriptFile)
    fso.DeleteFile(outputFile)
    'Msg ="WARNING: Could not change to destination directory on host!" & _
    '  vbCRLF & "File however appears to have been uploaded to default " & _
    '  "FTP directory associated with user on host."
    'MsgBox Msg, vbExclamation, appName

  ElseIf (InStr(results, "226")  or Instr(results, "221")) > 0 Then
    'MsgBox "File Uploaded Successfully.", vbInformation, appName
    fso.DeleteFile(scriptFile)
    fso.DeleteFile(outputFile)
  Else
    If InStr(results, "530") > 0 Then
      'Msg ="ERROR: Invalid Username/Password"
    ElseIf InStr(results, "550") > 0 Then
      'Msg ="ERROR: Could not change to destination directory on host"
    ElseIf InStr(results, "553") > 0 Then
      'Msg ="ERROR: Could not create file on host"
    ElseIf InStr(results, "Unknown host") > 0 Then
      'Msg ="ERROR: Unknown host"
    ElseIf InStr(results, "File not found") > 0 Then
      'Msg ="ERROR: Local File Not Found"
    Else
      'Msg ="An ERROR may have occurred."
    End If

    'Msg = Msg & _
    '  vbCRLF & "Script file leveraged: " & scriptFile & _
    '  vbCRLF & "FTP Output file: " & outputFile & _
    '  vbCRLF & _
    '  vbCRLF & "Ensure the above files are manually deleted, as they may " & _
    ' "contain sensitive information!"
    'MsgBox Msg, vbCritical, appName
  End If
  Set shell = Nothing
  Set fso = Nothing
End Sub
0
Comment
Question by:jonathanduane2010
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 35055207
Just schedule a Windows task to run everyhour.
0
 
LVL 33

Expert Comment

by:jppinto
ID: 35055214
I don't know wich version of Windows you have.

If it's XP:

http://support.microsoft.com/kb/308569

If it's Windows 7:

http://windows.microsoft.com/en-US/windows7/schedule-a-task

jppinto
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35055222
BTW have posted a requestattention to have your password removed from script.... was able to logon :-)
0
 

Author Comment

by:jonathanduane2010
ID: 35055232
thank you so much!

i dunno what i was thinking!
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 35055420
I would schedule the script using scheduled tasks too personally but you could also have the one script keep running if you preferrred, e.g. this bit:

FOR EACH objFile in ObjFolder.Files
  if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
    if datediff("s", objFile.DateLastModified, Now) > 40 Then
      Upload hostname, port, username, password, objFile.Name, localDir, remoteDir
      'objFile.Delete  
    End If
  End If
Next

You could wrap in a Do .... Loop which loops for ever with a pause of 1 hour, e.g.

Do
FOR EACH objFile in ObjFolder.Files
  if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
    if datediff("s", objFile.DateLastModified, Now) > 40 Then
      Upload hostname, port, username, password, objFile.Name, localDir, remoteDir
      'objFile.Delete  
    End If
  End If
Next
' Wait 1 hour = 36,000ms
wscript.sleep 1000 * 60 * 60
Loop

If you change this line or any other log bits you want to see

'MsgBox Msg, vbCritical, appName

to something like this:

wscript.echo Msg

then it will report to a console window when you run it on a schedule it as  cscript //nologo yourscript.vbs

The disadvantage of doing it this way is if the script gets stopped, e.g. due to an error then it doesn't restart, you always have the script running etc. Advantage is you have a window open you can see log details in.  All depends whether you are running this unattended or visible to someone etc.

Steve
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now