file vs directory

Custom / CUSTOM
February 13, 2010 at 12:54:50
Specs: Microsoft Windows XP Professional, 2.401 GHz / 2047 MB
Is there perfect code in DOS to figure out whether a given name, exists either as a file, or a directory ?

I do not need to know that a give name exists either as a file, I need to know :

- does it exist
- if yes, is it a file or a directory

Oops, should have been in the programming forum ...


See More: file vs directory

Report •

#1
February 13, 2010 at 13:31:11
Oops, should have been in the programming forum ...

It is now.

Stuart


Report •

#2
February 13, 2010 at 15:59:24
"Is there perfect code in DOS"
Pardon? I thought the terms "perfect code" and "DOS" were
mutually exclusive.

Hold on, it seems this is Windows XP, not DOS. The following
works under Windows XP and later versions. Note we can't
use the simple IF EXIST command as that doesn't
differentiate between files and directories.

setlocal
set /p name=What name? 
set /p location=Where to look? 
pushd %location%
set found=neither
for %%a in (%name%*) do if "%%a" == "%name%" set found=file
for /d %%a in (%name%*) do if "%%a" == "%name%" set found=directory
echo Found %found%
popd


Report •

#3
February 13, 2010 at 16:29:02
well, there might be any number of files and/or directories with a given name on the system, so you would need a list/report rather than a boolean test for file/dirname.
this script will generate a report but unfor. it still requires 2 passes:
@echo off
pushd \
attrib /s /d %1| find /i /v "File not found - %1">all
for %%a in (all) do set xx=%%~za
if %xx% equ 0 goto :none
attrib /s %1| find /i /v "File not found - %1">files
for %%a in (files) do set xx=%%~za
if %xx% neq 0 (
findstr /l /g:files /v /i all >dirs 2>nul
echo.files named %1
type files
) else (
move /y all dirs
)
for %%a in (dirs) do set xx=%%~za
if %xx% neq 0 (
echo.dirs named %1
type dirs
)
del files
del dirs
del all
goto :eof
:none
echo nothing named %1

Report •

Related Solutions

#4
February 13, 2010 at 16:31:51
my turn for oops: i interpreted as meaning anywhere on the entire system

Report •

#5
February 13, 2010 at 17:43:54
Appending a backslash to "if exist" works well.

if exist "%whatamI%\" (
    echo dir
) else if exist "%whatamI%" (
    echo file
) else (
    echo non-existent
)


Report •

#6
February 14, 2010 at 01:57:09
Hi Judago,

pretty smooth


=====================================
Helping others achieve escape felicity

M2


Report •

#7
February 14, 2010 at 02:27:07
Hi M2,

Thanks!

I found it a little while ago trying find an alternative to the old "if exist d:\ir\nul", which will only take unquoted paths.....


Report •

#8
February 14, 2010 at 04:44:34
edit 123

Report •

#9
February 14, 2010 at 04:48:36
Judago, This is perfect:

> if exist "%whatamI%\" (
> echo dir
> ) else if exist "%whatamI%" (
> echo file
> ) else (
> echo non-existent
> )
>

What still amazes me, is that the IF /? does not mention directories at all ... it says that it can check "if files exist" yes or no ...

... undocumented feature ... still ...


Report •

#10
February 14, 2010 at 04:50:31
> I found it a little while ago trying find an alternative
> to the old "if exist d:\ir\nul", which will only take
> unquoted paths.....

It's the same reason why I raised this one ... got bored with the ..\nul thing ...


Report •

#11
February 14, 2010 at 05:43:11
Yeah, inline help does miss a few of the finer points, but that could be due to some things either working or not because of some incidental nature.

Report •

#12
February 14, 2010 at 12:43:50
But the existance of directories in a filesystem, can hardly be classified as an incident, can it ?

Other commands are clear about differences between files and directories (as it should), but the IF command does not. There's no mentioning of directories, at all:

C:\>if /? | findstr /I director

C:\>if /? | findstr /I file
IF [NOT] EXIST filename command
EXIST filename Specifies a true condition if the specified filename
IF EXIST filename. (
del filename.
echo filename. missing.
IF EXIST filename. del filename. ELSE echo filename. missing
IF EXIST filename. del filename.
ELSE echo filename. missing
IF EXIST filename. (del filename.) ELSE echo filename. missing

C:\>


Report •

#13
February 14, 2010 at 22:06:54
No the existence of directories isn't an "incident". What I was trying to say is that you can't expect something to be documented if it wasn't designed or foreseen for that purpose.

I'm not saying "if exist" was or wasn't designed with directories in mind(because it probably was) , just that it's possible directories could be a result of the method used rather than a premeditated effort.....

http://xkcd.com/552/


Yes the documentation is often inadequate and yes it would be nice for it to be more complete and accurate, but there is no point in getting upset. The only thing that can be done about it is contact Microsoft with the details.....


Report •

#14
February 15, 2010 at 02:59:56
I like the cartoon Judago!

By the way, tvc highlighted something with IF/? - why does it use the syntax IF EXIST filename. ? Is there any use in the dot after the filename? To me it seems to work identically with or without it.


Report •

#15
February 15, 2010 at 05:55:57
xkcd is almost always classic!

As for the dot I don't know.

Trying to rename a file with a dot on the end works, but the filename doesn't seem to end in a dot and assoc and "folder options" seem to treat a single dot as files that don't have extension. If this is relevant or not I'm unsure....


Report •

#16
February 15, 2010 at 06:50:17
That dot is indeed weird ...

Thanks for reply anyway, I don't understand the cartoon actually, possibly since my primary language is not english ... too much difficult words in there !


Report •

#17
February 15, 2010 at 08:46:31
Speaking of folder options - files types...

Has anyone tried to associate a file with no extension? With or without dot.


=====================================
Helping others achieve escape felicity

M2


Report •

#18
February 15, 2010 at 08:59:51
@M2:

Re: assoc file without extension

Yes, tried and failed. It would be great if it was possible.

I think Microsoft's philosophy is that the "normal" user would not need such a feature. I.e. they would never come across such a file, because all documents, images, videos and songs, they all have an extension. It's only geeks that ever handle files without extension.


Report •

#19
February 15, 2010 at 17:34:14
"Has anyone tried to associate a file with no extension? With or without dot."

I tried it before posting, it worked for me on xp home sp3 and it worked for me.

"Yes, tried and failed. It would be great if it was possible."

This worked for me:

assoc .=txtfile

So did adding a new type in folder options with just a single dot.

The folder options method doesn't behave properly for me, but it does hold a basic association. The advanced option was unusable...

I wouldn't be surprised if these methods caused issues, especially since they seem to hit and miss.....



Report •

#20
February 15, 2010 at 18:11:01
Mechanix2Go: Has anyone tried to associate a file with no extension?
Yep. On my box, Notepad opens no extension files. I just used regedit, so if your assoc doesn't do it for you, just manually add the entry into HKCR

Report •

#21
February 16, 2010 at 02:40:32
Thanks, it worked! I don't know why it didn't work when I first
tried, maybe I was using Windows 2000 because it was a couple
of years ago. Now all I need to do is to convince it that files
imported from Unix, such as .profile, actually have a
name of ".profile" and no extension - and not an
extension of ".profile" and no name!

Report •

#22
February 16, 2010 at 11:54:39
Cool ... was looking for this one as well ... very annoying to have lots of extension-less files on Windows. OK, another bug from the list!

Report •

#23
February 17, 2010 at 03:23:43
I managed to get the no-ext assoc but I forget how.

:(

Ithink I used 2 adjacent apostrophes in file types. w2kSP4


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question