Page 1 of 1

Testing if it is a file or folder?

Posted: 05 Jan 2015 15:51
by Squashman
I have seen this question asked several times. And Dave even updated his post on SO last month.

How did none of us ever see this?
http://ss64.com/nt/syntax-args.html
Use %~a1 to display the Extended Attributes of a file.
FOR's %%~aI recognizes 9 NTFS file attributes. The expansion of a file attribute produces a series of 9 dashes, with each recognized attribute replacing a dash with a letter. A file with no recognized attributes or with none set will expand to 9 dashes like this: ---------

Attribute Expansion
FILE_ATTRIBUTE_DIRECTORY d--------
FILE_ATTRIBUTE_READONLY -r-------
FILE_ATTRIBUTE_ARCHIVE --a------
FILE_ATTRIBUTE_HIDDEN ---h-----
FILE_ATTRIBUTE_SYSTEM ----s----
FILE_ATTRIBUTE_COMPRESSED -----c---
FILE_ATTRIBUTE_OFFLINE ------o--
FILE_ATTRIBUTE_TEMPORARY -------t-
FILE_ATTRIBUTE_REPARSE_POINT --------l
FILE_ATTRIBUTE_NORMAL ---------

Re: Testing if it is a file or folder?

Posted: 05 Jan 2015 19:31
by penpen
Squashman wrote:How did none of us ever see this?
I knew and used it, and i'm sure i wasn't the only one, although i didn't read it on ss64; for example see:
- http://www.dostips.com/forum/viewtopic.php?p=28311#p28311 just search for "%%~at"
- http://www.dostips.com/forum/viewtopic.php?p=27548#p27548 just search for "arg=%%~ac"

I only missed, that you were looking for something like that.

penpen

Edit: I'm not sure but I think, i've seen such a table somewhere within the MS C++ documentation, but i can't find it (the names used above are defined in "win32.h").

Re: Testing if it is a file or folder?

Posted: 05 Jan 2015 20:13
by Squashman
You would think the "a" parameter would be documented in the help for CMD.exe or the FOR command. I can't find it documented in any of the help.

Re: Testing if it is a file or folder?

Posted: 05 Jan 2015 21:11
by josephf
Squashman wrote:I have seen this question asked several times. And Dave even updated his post on SO last month.

How did none of us ever see this?


Here is an archived snapshot from Sept 10th, 2013
https://web.archive.org/web/20130910183727/http://ss64.com/nt/syntax-args.html
There once was a time when the expansion was described on ss64 as simply:
%~a1 Display the file attributes of %1

Re: Testing if it is a file or folder?

Posted: 06 Jan 2015 02:28
by Compo
Here's an example from a now little used newsgroup.

Re: Testing if it is a file or folder?

Posted: 06 Jan 2015 04:15
by npocmaka_
will this if exist %1\ work on FAT32 (I have a memory that it will not , but I'm not sure)?
Also file attributes on ss64 are pointed as NTFS file attributes.

Re: Testing if it is a file or folder?

Posted: 08 Jan 2015 20:07
by trebor68
Please see more informations: CALL /?

Here a small batch file:

Code: Select all

@echo off
setlocal
:: echo %1   %~a1
set test=%~a1
set test=%test:~0,1%

if %test%==d (echo %1   is a directory) else echo %1   is not a directory


Tested in Win7.

Here some examples:

Code: Select all

D:\CMDVerz>exattr InRegEx5.bat
InRegEx5.bat   is not a directory

D:\CMDVerz>exattr UniCode
UniCode   is a directory

D:\CMDVerz>exattr "findstr info.txt"
"findstr info.txt"   is not a directory