ftp automation

ScriptFTP

The professional tool to automate FTP, SFTP, FTPS and schedule FTP batch jobs

The forum is now read only. Please, go to the the main ScriptFTP website if you need help.
Need help writing a script? Have any question about ScriptFTP?
I have created a script that downloads a file that is named one day in the future.

The system I am getting the file from uses the following format. For 10/31/2016 the file is named 161031.txt.
And for single digit days it will be a single digit with a leading zero. 10/1/2016 is 161001.txt

So I patted myself on the back for getting this going, it took a bit of head scratching and testing but it worked until it tried to download the file for 10/32/16, after all 31 plus 1 equals 32. :o So a bit of a flaw in the code.

Any way to factor in the number of days in a given month? I can live with it being wrong once a month if need be. Still saves a lot of manual d-loads.

Thanks for any help.

Matt

My code.
FTP Script
  1. $year=GETTIME(YEAR)
  2. $month=GETTIME(MONTH)
  3. $day=GETTIME(DAY)
  4. $myyear=$year-2000
  5. $myday=$day+1
  6. $D= $myday<10
  7. IF($D)
  8. $E="0".$myday
  9. $E=$myday
  10. #PRINT($D)
  11. #PRINT($E)
  12. #PRINT($year)
  13. #PRINT($month)
  14. #PRINT($day)
  15. #PRINT($myday)
  16. #PRINT($myyear)
  17. $mydate=$myyear.$month.$E.".txt"
  18. PRINT($mydate)
  19.  
  20. OPENHOST("***.***.*.**","login","PW")
  21. LOCALCHDIR("C:\WFWA Logs")
  22. GETFILE($mydate)
After a little more thought I decided on a brute force approach that should be wrong once every four years (leap year).

I hard coded the number of days in each month and then do a compare to decide if the file date falls into the next month and is therefore equal to one. Seems simple and I think it will work.
FTP Script
  1. $year=GETTIME(YEAR)
  2. $month=GETTIME(MONTH)
  3. $day=GETTIME(DAY)
  4. $myyear=$year-2000
  5. # declare number of days in each month
  6. IF($month==01)
  7. $monthday=31
  8. IF($month==02)
  9. $monthday=28
  10. IF($month==03)
  11. $monthday=31
  12. IF($month==04)
  13. $monthday=30
  14. IF($month==05)
  15. $monthday=31
  16. IF($month==06)
  17. $monthday=30
  18. IF($month==07)
  19. $monthday=31
  20. IF($month==08)
  21. $monthday=31
  22. IF($month==09)
  23. $monthday=30
  24. IF($month==10)
  25. $monthday=31
  26. IF($month==11)
  27. $monthday=30
  28. IF($month==12)
  29. $monthday=31
  30. # declare and compare $myday
  31. $myday=$day+1
  32. IF($myday>$monthday)
  33. $myday=1
  34. # add leading zero if needed
  35. $D=$myday<10
  36. IF($D)
  37. $E="0".$myday
  38. $E=$myday
  39. #
  40. PRINT("monthday = ".$monthday)
  41. PRINT("less than 10 = ".$D)
  42. PRINT("add leading zero = ".$E)
  43. PRINT("year = ".$year)
  44. PRINT("month = ".$month)
  45. PRINT("day = ".$day)
  46. PRINT("myday = ".$myday)
  47. PRINT("myyear = ".$myyear)
  48. # Construct file name to download
  49. $mydate=$myyear.$month.$E.".txt"
  50. #
  51. PRINT("mydate = ".$mydate)
  52. #
  53. OPENHOST("***.***.*.**","name","pw")
  54. LOCALCHDIR("C:\WFWA Logs")
  55. GETFILE($mydate)
Thanks for posting the solution you found. We were very busy with the release of the new version and could not come here to reply on time.

Regarding to your initial question, I think there is not a simple way to get the days of a given month. At least not within ScriptFTP, it would require an external program.
Realise this probably comes a little late, but thought I'd post this to help others in the future.

I use ScriptFTP to automate connecting to my customers' firewalls and downloading logfiles. I wanted to exclude both today's date, and yesterday's date (to ensure I wasn't re-downloading logfiles) and to keep the firewall trim, I wanted to delete any folder on the firewall older than 5 days.

There was a solution posted on here that suffered the same problem you encountered, in that the solution didn't take into consideration moving from one month to another.

This is my solution, there should be enough there to make it work into the future I would imagine?
FTP Script
  1.     # === CALCULATE TODAY'S DATE & TIME:
  2.         $current_date_time=GETDATE(FORMAT0)
  3.        
  4.     # === CALCULATE YESTERDAY'S DATE & TIME BY SUBTRACTING A DAY'S WORTH OF SECONDS:
  5.         $yesterday_date_time=$current_date_time-(1*24*60*60)
  6.        
  7.     # === CALCULATE DATE, 5 DAYS AGO BY SUBTRACTING 5 DAYS' WORTH OF SECONDS:
  8.         $five_days_date_time=$current_date_time-(5*24*60*60)
  9.        
  10.     # === CALCULATE TODAY'S DATE:
  11.         $TodayDate=GETTIME(YEAR)."".GETTIME(MONTH)."".GETTIME(DAY)
  12.        
  13.     # === CALCULATE & FORMAT YESTERDAY'S DATE IN YYYYMMDD FORMAT:
  14.         $YesterdayDate=TEXTCUT($yesterday_date_time,1,4)."".TEXTCUT($yesterday_date_time,6,2)."".TEXTCUT($yesterday_date_time,9,2)
  15.  
  16.     # === CALCULATE & FORMAT FIVE DAYS AGO IN YYYYMMDD FORMAT:
  17.         $FiveDays=TEXTCUT($five_days_date_time,1,4)."".TEXTCUT($five_days_date_time,6,2)."".TEXTCUT($five_days_date_time,9,2)
It's really hard code. He not only does not account for leap years. He is also a shift in the next month or year does not take into account ... And there is also a great idea to Julian date:
FTP Script
  1. #The number of days to be added (possibly negative)
  2. $DaysToAdd = 1
  3.  
  4. #Calculation of Julian day number (JDN) on the date of the Gregorian calendar
  5. $a = (14-GETTIME(MONTH))/12
  6. $y = GETTIME(YEAR)+4800-$a
  7. $m = GETTIME(MONTH)+12*$a-3
  8. $jdn = GETTIME(DAY)+(153*$m+2)/5+365*$y+$y/4-$y/100+$y/400-32045
  9.  
  10. # Adding days
  11. $jdn = $jdn+$DaysToAdd
  12.  
  13. # Calculation of the Gregorian calendar date of the known number of the Julian day (JDN)
  14. $a = $jdn+32044
  15. $b = (4*$a+3)/146097
  16. $c = $a-146097*$b/4
  17. $d = (4*$c+3)/1461
  18. $e = $c-1461*$d/4
  19. $m = (5*$e+2)/153
  20. $day = $e-(153*$m+2)/5+1
  21. $month = $m+3-12*($m/10)
  22. $year = 100*$b+$d-4800+$m/10
  23.  
  24. # Add zeros if necessary
  25. IF($day < 10)
  26.   $day = "0".$day
  27.  
  28. IF($month < 10)
  29.   $month = "0".$month
  30.  
  31. # We need the last 2 digits of the year
  32. IF(TEXTLENGTH($year) > 2)
  33.   $year = TEXTCUT($year, TEXTLENGTH($year)-1, 2)
  34.  
  35. # Searched file name
  36. PRINT($year.$month.$day.".txt")
Sorry for my English, I am using Google translator.
WAndrey,

Thanks so much for that. I will play with that a bit and see what I can make work.