1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

enabledelayedexpansionenabledelayedexpansionenabledelayedexpansion

Posted at

@echo off
setlocal enabledelayedexpansion

:: 日付フォーマットの設定
for /f "tokens=1-3 delims=/- " %%a in ('echo %date%') do set Today=%%a%%b%%c

:: 定数の定義
set "DEF_DIR=C:\FT\JO\DEF\file_check_target.txt"
set "PERIOD_DAY=3"
set "LOG_DIR=C:\FT\LOG"
set "LOG_FNAME=%LOG_DIR%\file_check_Result_LOG_%Today%.log"

set "FTP_FILE_CNT=0"
set "FTP_SUCCESS_CNT=0"
set "FTP_FAIL_CNT=0"

:: ログディレクトリの存在確認
if not exist %LOG_DIR% (
    echo ログディレクトリが見つかりません (%LOG_DIR%)
    exit /b 1
)

:: 処理開始ログの出力
echo %date% %time% [INFO]開始 >> %LOG_FNAME%

:: ファイル行の読み込みと処理
for /f "usebackq delims=" %%l in ("%DEF_DIR%") do (
    set "TARGET=%%l"
    
    :: ターゲットディレクトリの存在確認
    if not exist !TARGET! (
        echo %date% %time% [ERROR]対象ディレクトリが見つかりません (!TARGET!) >> %LOG_FNAME%
        echo %date% %time% [INFO]終了 >> %LOG_FNAME%
        exit /b 1
    )

    :: 指定日数を超えたファイルを検索し、直接パスを処理
    forfiles /P "!TARGET!" /D -%PERIOD_DAY% /C "cmd /c echo @path" 2>> "%LOG_FNAME%" | (
        for /f "usebackq delims=" %%f in ('more') do (
            set "status="
            set /p status=<%%f

            if not defined status (
                set /a FTP_FAIL_CNT+=1
                echo %date% %time% [WARNING]FTP失敗 : %%f >> %LOG_FNAME%
            ) else (
                set /a FTP_SUCCESS_CNT+=1
                echo %date% %time% [INFO]FTP成功 : %%f >> %LOG_FNAME%
            )
        )
    )
    set /a FTP_FILE_CNT=!FTP_SUCCESS_CNT! + !FTP_FAIL_CNT!
)

:: 処理対象のファイルが存在するかを確認
if %FTP_FILE_CNT%==0 (
    echo %date% %time% [INFO]結果: 対象ファイルが見つかりません >> %LOG_FNAME%
) else (
    echo %date% %time% [INFO]結果: FTPファイル - 成功:%FTP_SUCCESS_CNT% / 失敗:%FTP_FAIL_CNT% >> %LOG_FNAME%
)

:: 処理終了ログの出力
echo %date% %time% [INFO]終了 >> %LOG_FNAME%

:: FTP失敗の有無を確認
if %FTP_FAIL_CNT% NEQ 0 (
    exit /b 5
)

:: 正常終了
exit /b 0


1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?