Solved

getting script to run every hour

Posted on 2011-03-07
6
568 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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