?
Solved

getting script to run every hour

Posted on 2011-03-07
6
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 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