Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need to tell a directory apart from a file

17 views
Skip to first unread message

Todd

unread,
Oct 15, 2010, 5:26:46 PM10/15/10
to
Hi All,

How can I test if a name on the hard drive is
a file or a directory?

In bash script, this would be:

-d file
True if file exists and is a directory.

-f file
True if file exists and is a regular file.

Many thanks,
-T

billious

unread,
Oct 15, 2010, 5:46:53 PM10/15/10
to

"Todd" <To...@invalid.com> wrote in message
news:i9agup$22c$1...@news.eternal-september.org...

If exist "name\"

will return true if name is a directory, false if name is a file or doesn't
exist


Todd

unread,
Oct 15, 2010, 7:01:47 PM10/15/10
to


@echo off
if exist %temp%\ (echo It's a directory) ELSE (echo It's a file)
if exist J:\temp\ItsAFile\ (echo It's a directory) ELSE (echo It's a file)

J:\temp>eraseme
It's a directory
It's a directory

Everything is coming out a directory. What am I missing?

Many thanks,
-T

billious

unread,
Oct 15, 2010, 8:42:13 PM10/15/10
to

"Todd" <To...@invalid.com> wrote in message
news:i9amgr$6uh$1...@news.eternal-september.org...

Beats me. This is my test:


This solution developed using XP

----- batch begins -------
[1]@echo off
[2]:: setlocal - so that environment variables are cleared on termination
[3]setlocal
[4]:: create afile as a file, adirectory as a directory
[5]:: and delete any existing doesnotexist (suppressing errors)
[6]dir>afile
[7]md adirectory 2>nul
[8]del doesntexist 2>nul
[9]:: Now test
[10]set name=afile
[11]call :test
[12]set name=adirectory
[13]call :test
[14]set name=doesntexist
[15]call :test
[16]goto :eof
[17]
[18]:test
[19]if exist "%name%" (echo %name% exists) else (echo %name% does not exist)
[20]if exist "%name%\" echo %name% is a directory
[21]echo\==============
[22]goto :eof
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof


This is the result:

afile exists
==============
adirectory exists
adirectory is a directory
==============
doesntexist does not exist
==============


Hmm - you're posting from Linux, aren't you? That may have something to do
with it..???


Todd

unread,
Oct 15, 2010, 8:54:29 PM10/15/10
to
I can not win on this one. Everything is a directory.

My XP is in a virtual machine.

-T

J:\temp\BatchTest>fd
afile exists
afile is a directory


==============
adirectory exists
adirectory is a directory
==============
doesntexist does not exist
==============

J:\temp\BatchTest>dir
Volume in drive J is CentOS, 3.0.33-3.29.el5_5.1
Volume Serial Number is 0AD1-2095

Directory of J:\temp\BatchTest

10/15/2010 05:51 PM <DIR> .
10/15/2010 05:50 PM <DIR> ..
10/15/2010 05:51 PM 401 afile
10/15/2010 05:50 PM 545 fd.bat
10/15/2010 05:51 PM <DIR> adirectory

Todd

unread,
Oct 15, 2010, 8:57:45 PM10/15/10
to
On 10/15/2010 05:54 PM, Todd wrote:
> I can not win on this one. Everything is a directory.
>
> My XP is in a virtual machine.
>
> -T

Figured it out. I was running my test on a network drive.
When run on a local drive, it is a different story.
Geez Windows has a lot of bugs it it!

-T

C:\NtUtil\tmp>j:\temp\BatchTest\fd.bat
afile exists

billious

unread,
Oct 15, 2010, 9:22:06 PM10/15/10
to

"Todd" <To...@invalid.com> wrote in message
news:i9ata9$i6r$2...@news.eternal-september.org...

Certainly has - and it seems youve found another.

Here's another approach:

This solution developed using XP

----- batch begins -------
[1]@echo off
[2]:: setlocal - so that environment variables are cleared on termination
[3]setlocal
[4]:: create afile as a file, adirectory as a directory
[5]:: and delete any existing doesnotexist (suppressing errors)
[6]dir>afile
[7]md adirectory 2>nul
[8]del doesntexist 2>nul
[9]:: Now test
[10]set name=afile
[11]call :test
[12]set name=adirectory
[13]call :test
[14]set name=doesntexist
[15]call :test
[16]goto :eof
[17]
[18]:test

[19]set result=Directory
[20]if exist %name% (for /f %%i in ( ' dir /b %name% 2^>nul ' ) do set
result=File) else (set result=doesn't exist)
[21]echo\%name% %result%
[22]echo\==============
[23]goto :eof
------ batch ends --------

Lines start [number] - any lines not starting [number] have been wrapped and
should be rejoined. The [number] that starts the line should be removed

The label :eof is defined in NT+ to be end-of-file but MUST be expressed as
:eof

The spaces surrounding the single-quotes are for emphasis only. The SPACES
are not required but the single-quotes ARE required.

Results:

afile File
==============
adirectory Directory
==============
doesntexist doesn't exist
==============

Todd

unread,
Oct 15, 2010, 9:44:36 PM10/15/10
to
On 10/15/2010 06:22 PM, billious wrote:
> Here's another approach:
> This solution developed using XP

Thank you! Love examples.

-T

Matthias Tacke

unread,
Oct 16, 2010, 9:51:00 AM10/16/10
to

Hi Todd,

you can check for the similar file attributes with the ~a extension of
either the command line arguments or the for variables.

But there is no file attribute - if there is an attribute and it is not
a dir or link (reparse point) it is a more or less (depending on other
attributes) normal file.

Using billious batch as a base:

@echo off & setlocal
dir>afile
md adirectory 2>nul
del doesntexist 2>nul
:: Now test
For %%A in (afile adirectory doesntexist
) do IF "" EQU "%%~aA" (Echo nonexistent:%%A) Else Echo %%~aA %%A
goto :eof
:: 123456789
:: drahscotl
:: ||||||||l--- [l=link] Reparse Point
:: |||||||t---- Temporary file
:: ||||||o----- Offline
:: |||||c------ compressed
:: ||||s------- system
:: |||h-------- hidden
:: ||a--------- archive
:: |r---------- read only
:: d----------- directory

--
Regards
Matthias

foxidrive

unread,
Oct 16, 2010, 10:38:47 AM10/16/10
to


Here's another option that seems to work ok.


@echo off
dir "%~1" /b /a:d /-p >nul 2>&1
if errorlevel 1 (
echo file
) else (
echo folder
)
pause


--
Regards,
Mic

Matthias Tacke

unread,
Oct 16, 2010, 11:27:21 AM10/16/10
to
Am 2010-10-16 16:38, schrieb foxidrive:

> Here's another option that seems to work ok.
>
>
> @echo off
> dir "%~1" /b /a:d /-p >nul 2>&1
> if errorlevel 1 (
> echo file
> ) else (
> echo folder
> )
> pause
>
>

Hi Mic,

I'd insert a line in front:

if not Exist "%~1" Echo %1 doesn't exist & Goto :Eof

To avoid erroneously outputting "file"

--
Regards
Matthias

foxidrive

unread,
Oct 16, 2010, 12:06:24 PM10/16/10
to

Good idea. However the if exist would have to detect a file and a
folder - this might work.

@echo off
dir "%~1" /b /-p >nul 2>&1 || echo/ %1 doesn't exist & goto :EOF


dir "%~1" /b /a:d /-p >nul 2>&1
if errorlevel 1 (
echo file
) else (
echo folder
)
pause

--
Regards,
Mic

Tim Meddick

unread,
Oct 16, 2010, 1:48:21 PM10/16/10
to
Wow! You people really are getting very convoluted!

Try this (too simple!?)....


@echo off
if EXIST "%USERPROFILE%\LOCALS~1\TEMP\nul" (echo TEMP Dir Exists.) else
(echo TEMP Dir Absent.)

==

Cheers, Tim Meddick, Peckham, London. :-)


"Todd" <To...@invalid.com> wrote in message

news:i9agup$22c$1...@news.eternal-september.org...

Tim Meddick

unread,
Oct 16, 2010, 2:40:36 PM10/16/10
to
I do apologise, the script I quoted in my last post does not seem to work
with long file names and quotation marks!.

However, it is still very much workable if you use the directory's short
name without quotation marks......

@echo off
set TEMP=C:\DOCUME~1\%USERNAME%\LOCALS~1\TEMP
set TMP=C:\DOCUME~1\%USERNAME%\LOCALS~1\TEMP
if NOT EXIST %TEMP%\nul echo TEMP DIR NOT THERE
if EXIST %TEMP%\nul echo TEMP DIR PRESENT

Todd Vargo

unread,
Oct 16, 2010, 6:59:34 PM10/16/10
to

@echo off
set spec=%~1
::set spec=c:\path_to\spec
if not exist "%spec%" (
echo "%spec%" does not exist
goto :eof
)
if exist "%spec%\" (
echo "%spec%" is a directory
) else (
echo "%spec%" is a file
)

Tim Meddick

unread,
Oct 16, 2010, 10:35:29 PM10/16/10
to
Well it's not bash is it?!...

It's quite plainly - "alt.msdos.batch" .....

==

Cheers, Tim Meddick, Peckham, London. :-)


"Todd Vargo" <tlv...@sbcglobal.netz> wrote in message
news:i9daoq$3ba$1...@news.albasani.net...

foxidrive

unread,
Oct 16, 2010, 10:52:25 PM10/16/10
to
On 17/10/2010 05:40, Tim Meddick wrote:
> However, it is still very much workable if you use the directory's short
> name without quotation marks......

The \nul technique was reliable in MSDOS and Win9x but is not reliable
in NT based operating systems.


> @echo off
> set TEMP=C:\DOCUME~1\%USERNAME%\LOCALS~1\TEMP
> set TMP=C:\DOCUME~1\%USERNAME%\LOCALS~1\TEMP
> if NOT EXIST %TEMP%\nul echo TEMP DIR NOT THERE
> if EXIST %TEMP%\nul echo TEMP DIR PRESENT
>
> ==
>
> Cheers, Tim Meddick, Peckham, London. :-)
>


--
Regards,
Mic

Todd

unread,
Oct 17, 2010, 2:42:34 AM10/17/10
to
On 10/16/2010 07:52 PM, foxidrive wrote:

> The \nul technique was reliable in MSDOS and Win9x but is not reliable
> in NT based operating systems.

I have noticed this too. There is no predicting when it will and when
it will not work.

-T

Todd

unread,
Oct 17, 2010, 2:47:00 AM10/17/10
to
Love it.

Question: how do I test for the "d" (directory) flag?

Many thanks,
-T


Matthias Tacke

unread,
Oct 17, 2010, 11:36:41 AM10/17/10
to
Am 2010-10-17 08:47, schrieb Todd:
> Love it.
>
> Question: how do I test for the "d" (directory) flag?
>
> Many thanks,
> -T
>
You can only check positional contents of variables,
this version builds a pseudo array of the attributes
and then checks if the corresponding flag is set,
if true the verbose meaning will be echoed without a cr using
set /P to allow all attribs on one line.


@echo off & setlocal EnableDelayedExpansion


dir>afile
md adirectory 2>nul
del doesntexist 2>nul

:: Build attributes pseudo array
Set ACnt=0
For %%A in ( directory read-only archive hidden
system compressed offline tempfile link
) Do Set "Attrib%Acnt%=%%A" & set /A "ACnt+=1"

:: Now test
For %%A in (afile adirectory doesntexist

) do Set "Fattr=%%~aA+" & Call :Check "%%A"
goto :eof

:Check
If "%Fattr%" EQU "+" Echo nonexistent:%~1 & Goto :Eof
Set /P "_=%~1 "<NUL
For /L %%C in (0,1,9
) do If "!Fattr:~%%C,1!" NEQ "-" Set /P _= %1 <NUL
Echo.


--
Regards
Matthias

Tim Meddick

unread,
Oct 17, 2010, 11:37:37 AM10/17/10
to
I have never seen it fail under XP as long as you use the short path name
and don't use quotation marks!

==

Cheers, Tim Meddick, Peckham, London. :-)


"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:Rxtuo.3830$no5....@newsfe12.iad...

foxidrive

unread,
Oct 17, 2010, 12:13:25 PM10/17/10
to
On 18/10/2010 02:37, Tim Meddick wrote:
> I have never seen it fail under XP as long as you use the short path
> name and don't use quotation marks!

You could be right.

But it is unrealistic to expect people to convert long paths and names
to short ones and doubly so when the batch commands to do the conversion
fail to work at times.


--
Regards,
Mic

Tim Meddick

unread,
Oct 18, 2010, 4:06:17 AM10/18/10
to
Are you saying that such as :

(Command)
CVT2SHRT.BAT "c:\Program Files\Winimage\winimage.exe"

CVT2SHRT.BAT
***************
@echo off
echo %~s1

(Output)
C:\PROGRA~1\WinImage\winimage.exe


....fails to work at times?.......

==

Cheers, Tim Meddick, Peckham, London. :-)


"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message

news:QgFuo.3448$O64....@newsfe20.iad...

foxidrive

unread,
Oct 18, 2010, 5:36:36 AM10/18/10
to
On 18/10/2010 19:06, Tim Meddick wrote:
> Are you saying that such as :
>
> (Command)
> CVT2SHRT.BAT "c:\Program Files\Winimage\winimage.exe"
>
> CVT2SHRT.BAT
> ***************
> @echo off
> echo %~s1
>
> (Output)
> C:\PROGRA~1\WinImage\winimage.exe
>
>
> .....fails to work at times?.......

Yes.


> ==
>
> Cheers, Tim Meddick, Peckham, London. :-)
>
>
>
>
> "foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
> news:QgFuo.3448$O64....@newsfe20.iad...
>> On 18/10/2010 02:37, Tim Meddick wrote:
>>> I have never seen it fail under XP as long as you use the short path
>>> name and don't use quotation marks!
>>
>> You could be right.
>>
>> But it is unrealistic to expect people to convert long paths and names
>> to short ones and doubly so when the batch commands to do the
>> conversion fail to work at times.
>>
>>
>> --
>> Regards,
>> Mic
>


--
Regards,
Mic

Tim Meddick

unread,
Oct 18, 2010, 1:54:44 PM10/18/10
to
This has not been my experience...

==

Cheers, Tim Meddick, Peckham, London. :-)


"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message

news:SyUuo.3029$yw....@newsfe18.iad...

foxidrive

unread,
Oct 18, 2010, 6:37:22 PM10/18/10
to
On 19/10/2010 04:54, Tim Meddick wrote:
> This has not been my experience...

You can lead a horse to water...

Google in alt.msdos.batch.nt and find the threads for yourself.

--
Regards,
Mic

Todd Vargo

unread,
Oct 18, 2010, 8:31:14 PM10/18/10
to

"Tim Meddick" <timme...@o2.co.uk> wrote in message
news:i9dndr$16r$1...@speranza.aioe.org...

> Well it's not bash is it?!...
>
> It's quite plainly - "alt.msdos.batch" .....
>
> ==
>
> Cheers, Tim Meddick, Peckham, London. :-)

It does not appear you have a point, top poster.

Tim Meddick

unread,
Oct 19, 2010, 9:40:27 AM10/19/10
to
Sorry, you have lost me now....

I was simply stating that, however I (personally) choose to use it in batch
scripts, utilizing short-path names, while avoiding the use of quotation
marks, that the "nul file" directory detection method works (for me).

e.g.;


@echo off
if EXIST C:\PROGRA~1\INTERN~1\nul goto PRESENT
if NOT EXIST C:\PROGRA~1\INTERN~1\nul goto ABSENT
:PRESENT
echo.
echo Internet Explorer folder present.
echo.
goto :EOF
:ABSENT
echo.
echo Internet Explorer folder is absent.
echo.


==

Cheers, Tim Meddick, Peckham, London. :-)


"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message

news:T_3vo.2652$jw4....@newsfe21.iad...

0 new messages