Testing if it is a file or folder?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Testing if it is a file or folder?

#1 Post by Squashman » 05 Jan 2015 15:51

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 ---------

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Testing if it is a file or folder?

#2 Post by penpen » 05 Jan 2015 19:31

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").
Last edited by penpen on 06 Jan 2015 10:42, edited 2 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Testing if it is a file or folder?

#3 Post by Squashman » 05 Jan 2015 20:13

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.

josephf
Posts: 11
Joined: 01 Feb 2014 23:22

Re: Testing if it is a file or folder?

#4 Post by josephf » 05 Jan 2015 21:11

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

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Testing if it is a file or folder?

#5 Post by Compo » 06 Jan 2015 02:28

Here's an example from a now little used newsgroup.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Testing if it is a file or folder?

#6 Post by npocmaka_ » 06 Jan 2015 04:15

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.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Testing if it is a file or folder?

#7 Post by trebor68 » 08 Jan 2015 20:07

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

Post Reply