【続々々々々】ディレクトリかどうかの判断

半年ぶりの続きである。なかなか奥が深い。約1年前に、このような表を作った。

if exist FILEif exist DIRif exist NONE
if exist FILE\if exist DIR\if exist NONE\
if exist FILE\.if exist DIR\.if exist NONE\.
if exist FILE\\if exist DIR\\if exist NONE\\
if exist FILE\.\if exist DIR\.\if exist NONE\.\

ここでFILEは普通のファイル、DIRはディレクトリ、NONEはファイルとして存在しない名前。

しかし、対象がネットワーク共有名またはネットワークドライブの内部だとこの通り行かない。

if exist FILEif exist DIRif exist NONE
if exist FILE\if exist DIR\if exist NONE\
if exist FILE\.if exist DIR\.if exist NONE\.
if exist FILE\\if exist DIR\\if exist NONE\\
if exist FILE\.\if exist DIR\.\if exist NONE\.\

ここでFILEやDIRは実際には "\\ホスト名\共有名\FILE" またはネットワークドライブ X: に対して "X:\FILE" 等である。

つまり、ファイルとディレクトリの区別が付かない。また、\NUL を付加する方法でも駄目である。

したがって、ネットワーク共有先も含めて考えるなら、やはり if exist では駄目で、前回書いたように、

dir /b/ad FILE >NUL 2>NUL && echo ディレクトリ
if exist FILE dir /b/ad FILE >NUL 2>NUL || echo 普通のファイル

のような方法を取る必要がある。