Identifying If File Or Directory

Posted at

@echo off

:loop
      ::-------------------------- has argument ?
      if ["%~1"]==[""] (
        echo done.
        goto end
      )
      ::-------------------------- argument exist ?
      if not exist %~s1 (
        echo not exist
      ) else (
        echo exist
        if exist %~s1\\NUL (
          echo is a directory
        ) else (
          echo is a file
        )
      )
      ::--------------------------
      shift
      goto loop
      
      
:end

pause


save it as identifier.cmd
it can identify an unlimited arguments (normally you are limited to %1-%9), just remember to wrap the arguments with inverted-commas, or use 8.3 naming, or drag&drop them over (it automatically does either of above).

this allows you to run the following commands:
identifier.cmd c:\\windows
and to get
exist
is a directory
done


identifier.cmd "c:\\Program Files (x86)\\Microsoft Office\\OFFICE11\\WINWORD.EXE"
and to get
exist
is a file
done


and multiple arguments (of course this is the whole-deal..)
identifier.cmd c:\\windows\\system32 c:\\hiberfil.sys "c:\\pagefile.sys" hello-world
and to get
exist
is a directory
exist
is a file
exist
is a file
not exist
done.


naturally it can be a lot more complex,
but nice examples should be simple and minimal.


also posted at StackOverflow:
', 'CMD Ninja - Unlimited Arguments Processing, Identifying If Exist In File-System