Rob van der Woude's Scripting Pages

Batch How To ...

Read File Properties

Many times we need to check a file's size, its last-modified date, its location, or we may require its fully qualified path in short (8.3) notation.

Arguably CMD's most versatile internal command to the rescue: FOR.

FOR %%? IN (file_to_be_queried) DO (
	ECHO File Name Only       : %%~n?
	ECHO File Extension       : %%~x?
	ECHO Name in 8.3 notation : %%~sn?
	ECHO File Attributes      : %%~a?
	ECHO Located on Drive     : %%~d?
	ECHO File Size            : %%~z?
	ECHO Last-Modified Date   : %%~t?
	ECHO Parent Folder        : %%~dp?
	ECHO Fully Qualified Path : %%~f?
	ECHO FQP in 8.3 notation  : %%~sf?
	ECHO Location in the PATH : %%~dp$PATH:?
)
Notes: 1: Not all of these properties can be read this way in every Windows version.
With every new Windows version, more options became available.
Type FOR /? for more details.
  2: These properties can only be read if the file exists and can be found.
Open a command prompt and go to a directory, assuming D:\ for the sake of this explanation.
Now try this command: FOR %%A IN (notepad.exe) DO ECHO.%%~$PATH:A
It will show you notepad.exe's fully qualified path regardless of the current directory (because notepad.exe is located in a folder that is in the PATH).
Now try FOR %%A IN (notepad.exe) DO ECHO.%%~fA
This will show you a fully qualified path as if notepad were located in the current directory, e.g. D:\notepad.exe
So make sure the file to be queried is either in the current directory, or use its fully qualified path, or use %%~$PATH:? if it is located in the PATH.

Many of these properties can be combined, as is shown for the s option (short, or 8.3 notation).

Play with it, experiment, and learn. Have fun.


page last modified: 2017-08-25; loaded in 0.0013 seconds