Link to home
Start Free TrialLog in
Avatar of totalimpact
totalimpact

asked on

VBScript to download last 7 days of files from FTP

My FTP servers logs certain data in subfolders named by the date, like 20110306. I am trying to write a vbscript that will download the last 7 days worth of files from those subfolders. I found this ftp code, but I am not sure how to do the date part for the folders.
Wscript.Echo FTPDownload("ftp.mysite.com", "myaccount", "mypassword", "C:\temp\1\", "\", "*")

Open in new window

Avatar of prashanthd
prashanthd
Flag of India image

Does it have to scan multiple subfolders?
Avatar of Bill Prew
Bill Prew

How are the subfolders and files named?

~bp
Avatar of totalimpact

ASKER

There is a subfolder for each day - named like 20110306

There are about a hundred folders there, and I want just the last 7 days worth of folders = 7 folders, and 2-5 files in each folder.

Here is an example dir listing:
/
  20110301/many-csvfiles.csv
  20110302/many-csvfiles.csv
  20110303/many-csvfiles.csv
  20110304/many-csvfiles.csv
 
Here is an example of how you could look for the prior 7 days as the folder names to xfer from using the function you mentioned above.

For intDays = 0 To -7 Step -1
   datFetchDate = DateAdd("d", intDays, Now())
   strFetchDate = Year(datFetchDate) & Right("0" & Month(datFetchDate), 2) & Right("0" & Day(datFetchDate), 2)
   Wscript.Echo strFetchDate
   Wscript.Echo FTPDownload("ftp.mysite.com", "myaccount", "mypassword", "C:\temp\1\", strFetchDate, "*")
Next

Open in new window

~bp
Thanks Bill - that worked, but I was hoping to keep the folder structure, It just gets all the files, and they are together without folders, sometimes theres 20-30 files in a folder, so this gets messy.

 do you think I should adjust the ftp command somehow, or just have vb create the folders?
also, I need to get rid of the msgboxes - I got rid of the strFetchDate, but it keeps echoing -1 for each file it downloads, and I dont see this in the code, I guess its part of the function?
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That gets all the right files and dirs.

 I figured what you said on the -1, but...if I remove Wscript.Echo, it throws an error:

Cannot use parentheses when calling a Sub
Ah yes, sorry, should have mentioned that.  You can work around that in one of two ways.  First, keep it as a function and just assign the return to a variable, rather than echo it.  For example:

intReturn = FTPDownload("ftp.mysite.com", "myaccount", "mypassword", strDestDir, strFetchDate, "*")

Open in new window

The other approach if you really don't need the return is to change the call to work like a subroutine rather than a function.  In this way no value if returned by the call.  For example:

FTPDownload "ftp.mysite.com", "myaccount", "mypassword", strDestDir, strFetchDate, "*"

Open in new window

I'd recommend the first though.

~bp
I learn more about vbs everyday ;) Thats perfect, thanks for all your help Bill.
Bill did a great job, was very thorough and speedy.
Great, glad that was helpful, thanks.

~bp