Solved

getting script to run every hour

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

864 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

21 Experts available now in Live!

Get 1:1 Help Now