Need help to FTP data automatically for the day using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dosnovis
Posts: 3
Joined: 06 Nov 2009 15:22

Need help to FTP data automatically for the day using batch

#1 Post by dosnovis » 10 Nov 2009 15:12

Hi there,
I need to develop a batch file which will automatically FTP data for the previuos day. The data in the server is in the format DYYYYMMDD*.csv where * represents a long string. The important part is YYYYMMDD. The batch file would first get the date from the system and then deduct one day from the current day (will add leading zero if the result date is within 1 and 9). If the current date is 1st day of any month then it must get back to the last day of the previous month, and if the date is 1st day of January then it must go back to 31st December of the previous year. 1st day of March also requires special attention as going back to February means we need to take into account Leap Year or not. For months 1 to 9 a leading zero must be added to have MM format.
The whole logic is following (no specific scripting language, just general)-
1. IF MM==1 and DD==1 (
SET M=12, Y=%YYYY%-1, D=31
SET DT=%Y%%M%%D%
)

2. IF DD==1 and MM in (Feb, Apr, Jun, Aug, Sep, Nov)
SET M=%MM%-1, D=31, Y=%YYYY%
SET DT==%Y%%M%%D%

3. IF DD==1 and MM in (May, Jul, Oct, Dec)
SET M=%MM%-1, D=30, Y=%YYYY%
SET DT==%Y%%M%%D%

4. IF DD==1 and MM==3 (
IF %YYYY% mod 4==0 (
SET M=02, D=29, Y=%YYYY%
SET DT==%Y%%M%%D%) else (
SET M=02, D=28, Y=%YYYY%
SET DT==%Y%%M%%D%)

I tried the following batch file but the second SET with any IF statement is not working. I am sure there is much easier way to handle my requirement but as a NOVIS user of batch file I need help from you guys to complete this one.

My batch file:
-----------------
@echo off

:: ---Get current date-----
FOR /F "TOKENS=1,2* DELIMS=/. " %%A IN ('DATE/T') DO SET CDATE=%%A%%B%%C

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('DATE/T') DO SET dd=%%A
FOR /F "TOKENS=1-3 DELIMS=/ " %%A IN ('DATE/T') DO SET yyyy=%%C


set MM=%mm%


set /a "M=%mm%"
set /a "D=%dd%"
set /a "Y=%yyyy%"

set /a "mod=%Y%%%4"



:: 1st January----For The 1st day of January it will collect data from last year's 31st December-----
if %D%==1 (
if %M%==1 (
set /a "Y1=%Y%-1"
set "dt=%Y1%1231"
)
)

:: 1st February----For The 1st day of February it will collect data from last day of January-----
if %D%==1 (
if %M%==2 (
set "dt=%Y%0131"
)
)

:: 1st March---For 1st March it will collect data from last day of February depending on Leap Year or Non Leap Year----
if %D%==1 (
if %M%==3 (if %mod%==0 (
set dt=%Y%0229) else (
set dt=%Y%0228))
)

:: 1st April----For The 1st day of April it will collect data from last day of March-----
if %D%==1 (
if %M%==4 (
set "dt=%Y%0331"
)
)

:: 1st May----For The 1st day of May it will collect data from last day of April-----
if %D%==1 (
if %M%==5 (
set "dt=%Y%0430"
)
)

:: 1st June----For The 1st day of June it will collect data from last day of May-----
if %D%==1 (
if %M%==6 (
set "dt=%Y%0531"
)
)

:: 1st July----For The 1st day of July it will collect data from last day of June-----
if %D%==1 (
if %M%==7 (
set "dt=%Y%0630"
)
)

:: 1st August----For The 1st day of August it will collect data from last day of July-----
if %D%==1 (
if %M%==8 (
set "dt=%Y%0731"
)
)


:: 1st September----For The 1st day of September it will collect data from last day of August-----
if %D%==1 (
if %M%==9 (
set "dt=%Y%0831"
)
)

:: 1st October----For The 1st day of October it will collect data from last day of September-----
if %D%==1 (
if %M%==10 (
set "dt=%Y%0930"
)
)

:: 1st November----For The 1st day of November it will collect data from last day of October-----
if %D%==1 (
if %M%==11 (
set "dt=%Y%1031"
)
)

:: 1st December----For The 1st day of December it will collect data from last day of November-----
if %D%==1 (
if %M%==11 (
set "dt=%Y%1130"
)
)

:: For Dates ranging from 2 to 10
if %D% GEQ 2 (
if %D% LEQ 10 (
set /a D1=%D%-1
set dt=%Y%%MM%0%D1%
)
)

:: For Dates ranging from 11 to last day of the month
if %D% GEQ 11 (
set /a D1=%D%-1
set dt=%Y%%MM%%D1%
)

::echo.dt=%dt% --to check the output which will be used in FTP script

:: --FTP Script----
@ftp -i -s:"%~f0"&GOTO:EOF
open example
username
password

lcd c:\ftproot\I2000_Raw_Data
cd /opt/abc/mn

mget D%dt%*

bye
EOF

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#2 Post by ghostmachine4 » 10 Nov 2009 18:54

don't use batch for date addition and subtraction like that... since you have doing a more complicated task, i suggest to you to learn and use a better language such as Perl or Python with good FTP support,, as well as date manipulation support.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 11 Nov 2009 20:12

dosnovice

You can use teh julan days conversion to get the YYYY MM DD from yesterday:
@ECHO OFF
call:jdate JD
set /a JD-=1
call:jdate2date JD YYYY MM DD
echo.D%YYYY%%MM%%DD%*.csv

GOTO:EOF

rem Functions go below here
rem copy :jdate from http://www.dostips.com/DtCodeCmdLib.php#Function.jdate
rem copy :jdate2date from http://www.dostips.com/DtCodeCmdLib.php ... jdate2date

Output:
D20091110*.csv


DosItHelp?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 13 Nov 2009 00:50

dosnovice,

Here is a batch with embedded FTP script that gets a file with yesterday's time stamp.

@ECHO OFF
call:jdate JD
set /a JD-=1
call:jdate2date JD YYYY MM DD
call:ExtractSimple :StartFtp >"%temp%\%~n0.ftp"
notepad "%temp%\%~n0.ftp"
rem ftp -i -s:"%temp%\%~n0.ftp"
GOTO:EOF


:StartFtp
open example.com
username
password
!:--- FTP commands below here ---
cd public_html/%COMPUTERNAME%
binary
hash on
mget D%YYYY%%MM%%DD%*.csv
disconnect
bye


rem functions go below here
rem copy :jdate from http://www.dostips.com/DtCodeCmdLib.php#Function.jdate
rem copy :jdate2date from http://www.dostips.com/DtCodeCmdLib.php ... jdate2date
rem copy :ExtractSimple from http://www.dostips.com/DtCodeCmdLib.php ... ractSimple


Hope this helps.

JerzyPiotr
Posts: 1
Joined: 13 Nov 2009 01:04

#5 Post by JerzyPiotr » 13 Nov 2009 02:24

Output:
D20091110*.csv


Is there any error in the function jdate?
I get always D20001113*.csv as output (even if I change the system date for example to 2010).
The OS system is Windows_NT and system locale is PL.
Is there any explanation or maybe I'm doing something wrong :?:
I changed nothing in the cited code.
rgds
Jerzy

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#6 Post by DosItHelp » 14 Nov 2009 23:18

JerzyPiotr,

I suspect the :jdate function doesn't work correctly for systems with PL as system locale.
The :jdate function uses the date command to retrieve the year, month and day. On a US system the date function output looks like this:

Code: Select all

C:\>date
The current date is: Sat 11/14/2009
Enter the new date: (mm-dd-yy)
                           ^^

I would be interested in the output of the date command on a system with PL locale. May be I can fix it.
I would guess the "%yy%" substitution variable in line 12 of the :jdate function needs to be changed to match the two letter year formatter outputted by the date command.

Code: Select all

set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
                 ^^

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Need help to FTP data automatically for the day using b

#7 Post by Ed Dyreen » 05 Jun 2011 21:03


Ok, can someone help me deciphering ? I was a little stumbed when seeing this. Does set accepts a modulo :?:

Code: Select all

set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
                 ^^

I've tried it in the terminal an the output was even weirder:

set /a "y=1005 %100
result:
5
:|
set /a "y=1005 %120
result:
45
:?
set /a "y=1005 %127
result:
116
:shock:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Need help to FTP data automatically for the day using b

#8 Post by dbenham » 05 Jun 2011 21:47

Yes, % is the modulo operator (remainder after integral division)

Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Need help to FTP data automatically for the day using b

#9 Post by Ed Dyreen » 05 Jun 2011 22:09

Finally an expert response :D
integral division

Euh, I fell asleep during math class, what is an integral division ?

If I divide 1005 with 100 the result = 10.05 round = 10, not 45

set /a "y=1005 %120
result:
45

set /a y = 1005 /120
result:
8

I am getting the feeling someone is going to laugh very loud :oops:

Ah do you mean 105 MOD 10 = 5 oops SORRY, I'm Dutch.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Need help to FTP data automatically for the day using b

#10 Post by orange_batch » 06 Jun 2011 00:49

...

1005 / 100 = 10 remainder 5

1005 / 120 = 8 remainder 45

Post Reply