@echo off
setlocal EnableDelayedExpansion

echo Repair EJ
echo.

:: Get Date Option 1
ping bgcpitsd-mn01 -n 1 -w 1000 > nul
if errorlevel 1 ( goto :getdate) else (
 osql -UTRANSF -PFSNART -Sbgcpitsd-mn01 -dmaster -Q "set nocount on; select convert(varchar(10), getdate()-1, 120)" -h-1 -w150 > date.tmp
 set /p date=<date.tmp
 set yyyy=!date:~1,4!
 set mm=!date:~6,2!
 set dd=!date:~9,2!
 del date.tmp > nul
 goto :prepare
)

::Get Date Option2
:getdate
for /f %%i in ('powershell -command "((Get-Date).AddDays(-1).ToString('yyyy-MM-dd'))"') do set date=%%i
set yyyy=!date:~0,4!
set mm=!date:~5,2!
set dd=!date:~8,2!

:prepare
::Check Folder
if not exist D:\Journal ( mkdir D:\Journal ) else ( del /q D:\Journal\*.* )
type nul > D:\Journal\EJ_Fail_!mm!!dd!.txt
type nul > D:\Journal\Summary_Fail__Fail_!mm!!dd!.txt

::Read Target
if not exist %cd%\repair_log.txt ( echo repair_log.txt not found & pause & goto :end )
for /f "usebackq tokens=1 delims= " %%i in ("repair_log.txt") do call :process "%%i"
goto :eof

::Main Process
:process
set "st=%~1"
set "dt=!mm!!dd!"
echo Report Store: !st! Date: !dt!

::Check Store Skip
if "!st:~0,1!"=="6" ( echo !st! Skipping... & goto :next )
if "!st:~0,1!"=="8" ( echo !st! Skipping... & goto :next )
if "!st:~0,1!"=="2" ( echo !st! Skipping... & goto :next )
if "!st:~0,1!"=="3" ( echo !st! Skipping... & goto :next )
if "!st:~0,5!"=="11172" ( echo !st! Skipping... & goto :next )
if "!st!"==" " ( echo End & goto :end )
if "!st:~0,4!"=="----" ( echo End & goto :end )

::Check Connection
ping pth!st! -n 1 -w 1000> nul
if errorlevel 1 ( echo !st! Offline & goto :next ) 

:: Check EJ File
curl -u TRANSF:fsnart ftp://pth!st!/Journal/ > D:\Journal\temp.txt 2>nul
findstr /R /C:"EJ!st!!dt!" D:\Journal\temp.txt >nul
if errorlevel 1 ( echo Not Found EJ, Please GEN File & echo !st! EJ!!st!!dt! >> EJ_Fail_!mm!!dd!.txt & del /q D:\Journal\temp.txt > nul )

:: Check R File
curl -u TRANSF:fsnart ftp://pth!st!/Journal/Report/ > D:\Journal\temp.txt 2>nul
findstr /R /C:"R!st!!dt!" D:\Journal\temp.txt >nul
if errorlevel 1 ( 
    sqlcmd -UTRANSF -PFSNART -Spth!st! -Q "gen report"
    curl -u TRANSF:fsnart ftp://pth!st!/Journal/Report/ > D:\Journal\temp.txt 2>nul
    findstr /R /C:"R!st!!dt!" D:\Journal\temp.txt >nul
    if errorlevel 1 ( echo Not Found R, Please GEN File & echo !st! R!!st!!dt! >> Summary_Fail_!mm!!dd!.txt & del /q D:\Journal\temp.txt > nul )
)

:: Check T File
curl -u TRANSF:fsnart ftp://pth!st!/Journal/Report/ > D:\Journal\temp.txt 2>nul
findstr /R /C:"T!st!!dt!" D:\Journal\temp.txt >nul
if errorlevel 1 ( 
    sqlcmd -UTRANSF -PFSNART -Spth!st! -Q "gen report""
    curl -u TRANSF:fsnart ftp://pth!st!/Journal/Report/ > D:\Journal\temp.txt 2>nul
    findstr /R /C:"T!st!!dt!" D:\Journal\temp.txt >nul
    if errorlevel 1 ( echo Not Found T, Please GEN File & echo !st! T!!st!!dt! >> Summary_Fail__Fail_!mm!!dd!.txt & del /q D:\Journal\temp.txt > nul & goto :next )
)

::Download and Upload
::Get EJournal and Report from Server
:download_ej
(
echo user TRANSF
echo fsnart
echo binary
echo lcd D:\Journal
echo cd ..
echo cd ..
echo cd \JOURNAL
echo prompt
echo mget EJ!st!!dt!.*
echo cd Report
echo get R!st!!dt!.TXT
echo get T!st!!dt!.TXT
echo bye
) > ftpcmd.dat
ftp -n -s:ftpcmd.dat pth!st!% > nul
del ftpcmd.dat > nul

:: Check EJournal and Report
echo %windir%\explorer.exe D:\Journal
dir D:\Journal\EJ!st!!dt!.ZIP D:\Journal\R!st!!dt!.TXT D:\JOURNAL\T!st!!dt!.TXT

:: Upload EJournal to 10.4.7.71
(
echo user helpdesk P@ssw0rd24
echo binary
echo lcd D:\Journal
echo cd ..
echo cd ..
echo cd \File
echo prompt
echo put EJ%st%%dt%.ZIP
echo cd \Report
echo put R!st!!dt!.TXT
echo put T!st!!dt!.TXT
echo bye
) > ftpcmd.dat
ftp -n -s:ftpcmd.dat 10.4.7.71 > nul
del ftpcmd.dat > nul

:next
exit /b 0

:: Repair EJ
if not exist %cd%\EJ_Fail_!mm!!dd!.txt ( echo EJ_Fail_!mm!!dd!.txt not found & pause & goto :end )
for /f "usebackq tokens=1 delims= " %%i in ("EJ_Fail_!mm!!dd!.txt") do call :process "%%i"
goto :eof

sqlcmd -UTRANSF -PFSNART -Spth!st! -Q "gen ej"
curl -u TRANSF:fsnart ftp://pth!st!/Journal/ > D:\Journal\temp.txt 2>nul
findstr /R /C:"EJ!st!!dt!" D:\Journal\temp.txt >nul
if errorlevel 1 ( echo Not Found EJ, Please GEN File & echo !st! EJ!!st!!dt! >> EJ_Fail_!mm!!dd!.txt & del /q D:\Journal\temp.txt > nul & goto :next )

:end
:: Clean Up
del /q D:\Journal\*.*

exit

endlocal

ตรวจสอบ script ของฉัน