特定のフォルダ内に指定のファイルが存在することを確認するWindowsバッチを書いてみます。
たとえばこんなフォルダ(INFILE)と
batファイル(FileExistsCheck.bat)を用意して
これらの存在を確認してみます。
FileExistsCheck.batへ次のように書きます。
FileExistsCheck.bat
@echo off
rem >> 標準出力を停止
rem ◎指定フォルダ・指定ファイルの存在を確認する
rem ------------------------------------------------------------------------
setlocal enabledelayedexpansion
rem >> 遅延変数を使う場合の指定
cd %~dp0
rem >> このbatファイルが置かれているディレクトリをカレントディレクトリとする
rem ------------------------------------------------------------------------
rem >> 日付取得
echo 日付 %date%
echo 時刻 %time%
echo !date! !time! --- begin ---
rem >> ディレクトリ名、ファイル名、変数の定義
set targetdirin=INFILE
set targetinfile01=%targetdirin%\11111hogehoge.csv
set targetinfile02=%targetdirin%\22222hogehoge.csv
set targetinfile03=%targetdirin%\33333hogehoge.csv
set targetinfile04=%targetdirin%\44444hogehoge.csv
set targetinfile05=%targetdirin%\55555hogehoge.csv
set dirinerr=0
set csvinerr=0
rem >> 動作指定
rem 01.指定ディレクトリ配下のファイルの存在確認
if not exist "%targetdirin%" (
echo ディレクトリ %targetdirin% は存在しません。
set dirinerr=1
) else (
if not exist %targetinfile01% (
echo ディレクトリ %targetdirin% 配下に %targetinfile01% が存在しません。
set csvinerr=1
)
if not exist %targetinfile02% (
echo ディレクトリ %targetdirin% 配下に %targetinfile02% が存在しません。
set csvinerr=1
)
if not exist %targetinfile03% (
echo ディレクトリ %targetdirin% 配下に %targetinfile03% が存在しません。
set csvinerr=1
)
if not exist %targetinfile04% (
echo ディレクトリ %targetdirin% 配下に %targetinfile04% が存在しません。
set csvinerr=1
)
if not exist %targetinfile05% (
echo ディレクトリ %targetdirin% 配下に %targetinfile05% が存在しません。
set csvinerr=1
)
)
if %dirinerr% == 1 (
echo INファイルの格納先フォルダに問題があるため処理を中止します。
)
if %csvinerr% == 1 (
echo INファイルの格納状況に問題があるため処理を中止します。
)
if %dirinerr% == 0 (
if %csvinerr% == 0 (
echo INファイルの格納状況に問題はありません。
)
)
echo !date! !time! --- end ---
timeout /t 5 /nobreak
実行してみる。
いい感じ。
ディレクトリ名をINFILAにしてみたり
ファイル名を一部 hugahugaにすると…
ちゃんと反応してますね。
お疲れさまでした。