Need help to FTP data automatically for the day using batch
Posted: 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
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