I tried to make this as clear as possible, hope it's not confusing!
I have a vbs file that I want to do the following:
1) Open a previously created text file that has a listing of an ftp directory.
2) Begin Subroutine
3) Scan that text file to find a particular file name.
4) If the name exists, open cmd and use sendkeys to input the command to open my ftp program and download the file from the aforementioned ftp directory.
5) Determine if the file exists in the local directory and add the file name and whether it was downloaded (or a specific msg if not found in the remote directory) to a new line in a different text file.
6) Exit Subroutine
7) Repeat steps 2-6 for several files (about 15 files in all).
8) Read the second text file and display the information recorded in a msgbox.
Below is my code. So far everything is working great if I run it for only one file. I cannot think of a way to wait until the subroutine finishes for each file before it is called for the next. I've done quite a bit of online searching and have yet to come up with a solution.
I have to go the cmd -> ftp program route for security reasons, although I realize this is likely what's causing the hurdle. Of course, any suggestions on an alternative way to connect to ftp without passing login/password information would be considered. I haven't found anything yet that I think would work, but I am a self-taught jack-of-all-trades kind of programmer so there could very well be some way of which I'm unaware.
Thanks in advance!
I have a vbs file that I want to do the following:
1) Open a previously created text file that has a listing of an ftp directory.
2) Begin Subroutine
3) Scan that text file to find a particular file name.
4) If the name exists, open cmd and use sendkeys to input the command to open my ftp program and download the file from the aforementioned ftp directory.
5) Determine if the file exists in the local directory and add the file name and whether it was downloaded (or a specific msg if not found in the remote directory) to a new line in a different text file.
6) Exit Subroutine
7) Repeat steps 2-6 for several files (about 15 files in all).
8) Read the second text file and display the information recorded in a msgbox.
Below is my code. So far everything is working great if I run it for only one file. I cannot think of a way to wait until the subroutine finishes for each file before it is called for the next. I've done quite a bit of online searching and have yet to come up with a solution.
I have to go the cmd -> ftp program route for security reasons, although I realize this is likely what's causing the hurdle. Of course, any suggestions on an alternative way to connect to ftp without passing login/password information would be considered. I haven't found anything yet that I think would work, but I am a self-taught jack-of-all-trades kind of programmer so there could very well be some way of which I'm unaware.
Thanks in advance!
Code:
Dim wShell, fso, f, fc, fn, i, finis
Dim oRegEx, objf, strSS, cmatch, smatch
Dim cpath, mpath, opath, spath
Dim mo, da, yr, yrmoda
Dim txtstr, fl, multi, mArray(), j
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtstr = fso.CreateTextFile("C:\MC\tempdirfile\temp.txt", True)
txtstr.Close
Set fso = nothing
cpath = "C:\Program Files\coreftp\coreftp.exe"
mpath = "/ftp_path/"
da = Day(Date)
mo = Month(Date)
yr = Year(Date)
If mo < 10 Then
mo = "0" & mo
Else
mo = mo
End If
If da < 10 Then
da = "0" & da
Else
da = da
End If
yrmoda = yr & mo & da
Call downloadfiles("blahblahsomefile" & yrmoda, "blahblahsomefilepath", "blah File(s) not found in remote directory.")
Call downloadfiles("blahblahsomeotherfile" & yrmoda, "blahblahsomeotherfilepath", "blahblah File(s) not found in remote directory.")
Call downloadfiles("blahblahyetanotherfile" & yrmoda, "blahblahyetanotherfilepath", "blahblahblah File(s) not found in remote directory.")
spath = "C:\MC\tempdirfile\temp.txt"
MsgBox ReadTextFile(spath)
'''''Function to read 2nd text file and get output for above msgbox
Public Function ReadTextFile(strPath)
Dim fso, ts, strOutput
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(strPath)
Do Until ts.AtEndOfStream
strOutput = strOutput & ts.ReadLine & Chr(13)
Loop
ts.Close
ReadTextFile = strOutput
End Function
'''''Main subroutine
Public Sub downloadfiles(patho, pathf, outp)
Set oRegEx = CreateObject("VBScript.RegExp")
oRegEx.Pattern = patho
oRegEx.IgnoreCase = true
Set fso = CreateObject("Scripting.FileSystemObject")
Set objf = fso.OpenTextFile("C:\MC\tempdirfile\tempdirfile.txt", 1)
i = 0
multi = ""
Do Until objf.AtEndOfStream
strSS = objf.ReadLine
cmatch = oRegEx.Test(strSS)
If cmatch Then
i = i + 1
If multi = "" Then
multi = Right(strSS, Len(strSS) - (InStr(strSS, patho) - 1))
Else
multi = multi & "|" & Right(strSS, Len(strSS) - (InStr(strSS, patho) - 1))
End If
End If
Loop
Set fso = nothing
If i > 0 Then
Set wShell = CreateObject("WScript.Shell")
wShell.Run("cmd")
wShell.AppActivate("cmd")
WScript.Sleep 2000
If InStr(multi,"|") > 0 Then
mArray = Split(multi,"|")
For j = 0 to UBound(mArray)
If InStr(mArray(j),".txt") > 0 Then
opath = mpath & mArray(j)
wShell.SendKeys(Chr(34) & cpath & Chr(34) & " -s -O -site PCNS -d " & opath & " -p " & pathf & " {ENTER}")
msgbox(cpath & chr(34) & opath & chr(34) & pathf)
Set fso = CreateObject("Scripting.FileSystemObject")
fn = pathf & mArray(j)
If fso.FileExists(fn) Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 8, True)
fl.WriteLine(mArray(j) & " was downloaded.")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 1)
WriteLineToFile = fl.ReadAll
Set fso = nothing
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 8, True)
fl.WriteLine(mArray(j) & " was not downloaded.")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 1)
WriteLineToFile = fl.ReadAll
Set fso = nothing
End If
Set fso = nothing
Set wShell = nothing
End If
Next
Else
opath = mpath & multi
wShell.SendKeys(Chr(34) & cpath & Chr(34) & " -s -O -site PCNS -d " & opath & " -p " & pathf & " {ENTER}")
Set fso = CreateObject("Scripting.FileSystemObject")
fn = pathf & multi
If fso.FileExists(fn) Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 8, True)
fl.WriteLine(multi & " was downloaded.")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 1)
WriteLineToFile = fl.ReadAll
Set fso = nothing
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 8, True)
fl.WriteLine(multi & " was not downloaded.")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 1)
WriteLineToFile = fl.ReadAll
Set fso = nothing
End If
Set fso = nothing
Set wShell = nothing
End If
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 8, True)
fl.WriteLine(outp)
Set fl = fso.OpenTextFile("C:\MC\tempdirfile\temp.txt", 1)
WriteLineToFile = fl.ReadAll
Set fso = nothing
End If
End Sub