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:?
Note: 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.

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 uploaded: 2017-07-06, 12:36