Solved

VBScript to download last 7 days of files from FTP

Posted on 2011-03-05
12
353 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:totalimpact
[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
  • 6
  • 5
12 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35045790
Does it have to scan multiple subfolders?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35046180
How are the subfolders and files named?

~bp
0
 

Author Comment

by:totalimpact
ID: 35047029
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
 
0
Industry Leaders: 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!

 
LVL 54

Expert Comment

by:Bill Prew
ID: 35060181
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
0
 

Author Comment

by:totalimpact
ID: 35063407
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?
0
 

Author Comment

by:totalimpact
ID: 35063448
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?
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35063644
See if this gets closer to what you need.  I added logic to create the local date folder if needed, and then used that path in the FTP routine.  Removing the Wscript.Echo in front of that function will elliminate the -1 display, that was showing the return from the function call.

Set objFSO = CreateObject("Scripting.FileSystemObject")
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)
   strDestDir = "C:\temp\" & strFetchDate & "\"
   If Not objFSO.FolderExists(strDestDir) Then objFSO.CreateFolder(strDestDir)
   FTPDownload("ftp.mysite.com", "myaccount", "mypassword", strDestDir, strFetchDate, "*")
Next

Open in new window

~bp
0
 

Author Comment

by:totalimpact
ID: 35063756
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
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35063874
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
0
 

Author Comment

by:totalimpact
ID: 35064267
I learn more about vbs everyday ;) Thats perfect, thanks for all your help Bill.
0
 

Author Closing Comment

by:totalimpact
ID: 35064275
Bill did a great job, was very thorough and speedy.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35068411
Great, glad that was helpful, thanks.

~bp
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This video will demonstrate how to customize windows, tools, and control bars, and save them as screen sets. Open and resize windows: Customize the toolbar: Customize the control bar: Customize your tool selections: Your screen set is alread…
Viewers will learn how to use LFOs to modulate the sound of their Sampler instruments. Click the Modulation tab in Sampler: Choose one (or more) of the three available LFOs, and click the respective button to turn it on: Select a waveform, an LF…

734 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