0
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?

More than 3 years have passed since last update.

特定のフォルダ内に指定のファイルが存在することを確認するWindowsバッチ

Posted at

特定のフォルダ内に指定のファイルが存在することを確認するWindowsバッチを書いてみます。

たとえばこんなフォルダ(INFILE)と
batファイル(FileExistsCheck.bat)を用意して
image.png

INFILE内にこんなファイルを用意して……
image.png

これらの存在を確認してみます。

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

実行してみる。

image.png

いい感じ。
ディレクトリ名をINFILAにしてみたり
ファイル名を一部 hugahugaにすると…
image.png

image.png

ちゃんと反応してますね。

お疲れさまでした。

0
0
0

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
0
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?