1.

Solve : FTP Filedownload?

Answer»

I am creating this BATCH file to download an file which is one day older to today's date and I am not able to execute the file because of some errors. can you please follow the below program and LET me know where I am going wrong.

Cd..
Cd..
A:
CD A:\DATA

@echo off
:: This section gets yesterday's date
:: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SETLOCAL ENABLEEXTENSIONS

:: Get current date in YYYYMMDD format
FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :FIXDATE %%G %%H %%I %%J)
goto :GETDATE

:FIXDATE
if "%1:~0,1%" GTR "9" shift
FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
set %%G=%1&set %%H=%2&set %%I=%3)
goto :EOF

:GETDATE
:: Convert to MODIFIED Julian date
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
set /a dd=100%dd%%%100,MM=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2432046
set MJD=%j%

:: Subtract a day
set /a MJD=%MJD% - 1

:: Convert back to YYYYMMDD format
set /a a=%MJD%+2432045,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set datedsub=%yy%%mm%%dd%&set y=%yy%&set m=%mm%&set d=%dd%

echo %datedsub%

Please guide me in the right direction.Just as a note, don't use double colons for Remarks, instead use the REM command.why not ? i allways user them, you just cant use them in for loop

@gurusanjay

can you tell us what errors you get ? quick glance on your code:
if "%1:~0,1%" GTR "9" shift
- logic error eg.if "1" gtr "-2" (echo bigger) else echo smaller
- and you can't do substring on argument %1, you have to set it to variable first

if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
yy should be 4 digits, another logic error.

FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :FIXDATE %%G %%H %%I %%J)
it's better to use for .... ('echo %date:~-10%') do call::FIXDATE %%G %%H %%I

try this and let me know the result, the code is quite similar to your's, so i believe you will quickly understand the inner-working of the code:
Code: [Select]::source: http://en.wikipedia.org/wiki/Julian_day
@echo off & setlocal
if "%~1"=="" echo USAGE: %0 [interval] & goto:eof

for /f "skip=1 tokens=2-4 delims=(./-)" %%a in ('echo.^|date') do (
for /f "tokens=1-3 delims=./- " %%A in ('echo %date:~-10%') do (
set %%a=%%A& set %%b=%%B& set %%c=%%C
))
::echo Today :%yy%%mm%%dd%
set/a mm=1%mm%-100,dd=1%dd%-100

::jd
set/a a=(14-mm)/12, y=yy+4800-a, m=mm+12*a-3
set/a jd=dd+ (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 - 32045
::date interval
set/a jd+=%~1

::gd
set/a s1=jd+68569, n=4*s1/146097, s2=s1-(146097*n+3)/4, i=4000*(s2+1)/1461001
set/a s3=s2-1461*i/4+31, q=80*s3/2447, s4=q/11, e=s3-2447*q/80
set/a yy=100*(n-49)+i+s4, mm=q+2-12*s4, dd=e, gd=yy*10000+mm*100+dd
::echo %1 :%gd%

echo %gd%
exit/b %gd%

sample output:
Code: [Select]C:\>jd -1
20090407

C:\>echo %errorlevel%
20090407if you are not restricted to choice of language, here's a Perl script example. It should make your life easier when dealing with things like date manipulation.
Code: [Select]use Net::FTP;
use Date::Calc qw( Today_and_Now Add_Delta_DHMS Month_to_Text );
my @date_time = Add_Delta_DHMS( Today_and_Now(), -1, 0, 0, 0 );
my $yest_month = substr(Month_to_Text($date_time[1]),0,3);
my $yest_day = $date_time[2];
my $file_to_get = "file.txt"; ## file to get.
$ftp = Net::FTP->new("127.0.0.1", Debug => 1) or die "Cannot connect to localhost: [emailprotected]";
$ftp->login("user",'password') or die "Cannot login ", $ftp->message;
$ftp->cwd("somedir");
my @listing = $ftp-> dir($file_to_get);
my @list = split / +/, $listing[0];
my $month = $list[5]; my $day = $list[6] ;
if (( $yest_day + 0 == $day + 0 ) && ( $yest_month == $month ) ) {
print "File is one day older than today...ok\n";
$ftp->get($file_to_get) or die "get failed ", $ftp->message;
}
$ftp->quit;



Discussion

No Comment Found