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

detect whether an argument is a file or directory

19 views
Skip to first unread message

al...@vp.pl

unread,
Dec 19, 2007, 7:42:39 AM12/19/07
to
Hi all,

I have a file containing many entries (path to a file or path to a
directory). I want to loop through this list and perform a different
action if the present line leads to a file and different when it leads
to a directory (I assume that all files will have some kind of extension
and directories will not). How the current entry is pointing to a file
or a directory?

Thanks,
AL

billious

unread,
Dec 19, 2007, 9:05:58 AM12/19/07
to

<al...@vp.pl> wrote in message news:fkb3k5$e92$1...@news.onet.pl...


----- batch begins -------
[1]@echo off
[2]setlocal
[3]for /f "tokens=*" %%i in (fdlist.txt) do set yhx=%%~xi&if exist "%%i\." (
[4]if defined yhx echo "%%i" is an existing directory with an extension
[5]if not defined yhx echo "%%i" is an existing directory without an
extension
[6]) else (
[7]if exist "%%i" (
[8]if defined yhx echo "%%i" is an existing file with an extension
[9]if not defined yhx echo "%%i" is an existing file without an extension
[10]) else (
[11]echo "%%i" does not appear to exist
[12])
[13])
------ 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 SETLOCAL on [2] means that de-allocating YHX is not required


foxidrive

unread,
Dec 19, 2007, 9:21:20 AM12/19/07
to
On Wed, 19 Dec 2007 22:05:58 +0800, "billious" <billio...@hotmail.com>
wrote:

I'm not sure your code will work, Billious.

===[screen capture]===
M:\>if exist m:\a.bat\. echo abc
abc

===[screen capture]===

foxidrive

unread,
Dec 19, 2007, 9:35:17 AM12/19/07
to
On Wed, 19 Dec 2007 07:42:39 -0500, "al...@vp.pl" <al...@vp.pl> wrote:

>I have a file containing many entries (path to a file or path to a
>directory). I want to loop through this list and perform a different

>action if the line leads to a file and different when it leads
>to a directory?


@echo off
for /f "delims=" %%a in ('type "file.txt"') do (
echo "%%a"
dir "%%a"|find "<DIR>">nul&&(echo ---FOLDER---)||(echo file)
)


billious

unread,
Dec 19, 2007, 10:06:02 AM12/19/07
to

"foxidrive" <got...@woohoo.invalid> wrote in message
news:53aim3l66ag7q5olp...@4ax.com...

Quite correct.

There should NOT be a dot between the "\" and closing-quote in [3]


----- batch begins -------
[1]@echo off

[2]setlocal enabledelayedexpansion
[3]for /f "tokens=*" %%i in (fdlist.txt) do set yhx=%%~xi&if exist "%%i\" (


[4]if defined yhx echo "%%i" is an existing directory with an extension
[5]if not defined yhx echo "%%i" is an existing directory without an
extension
[6]) else (
[7]if exist "%%i" (
[8]if defined yhx echo "%%i" is an existing file with an extension
[9]if not defined yhx echo "%%i" is an existing file without an extension
[10]) else (
[11]echo "%%i" does not appear to exist
[12])
[13])
------ batch ends --------

screen capture :---------
"c:\106x\filename" is an existing file without an extension
"c:\106x\filename.ext" is an existing file with an extension
"c:\106x\fulldirectoryname" is an existing directory without an extension
"c:\106x\fulldirectoryname.ext" is an existing directory with an extension
"c:\106x\emptydirectoryname" is an existing directory without an extension
"c:\106x\emptydirectoryname.ext" is an existing directory with an extension
"c:\106x\idonotexist" does not appear to exist
"c:\106x\idonotexist.ext" does not appear to exist

end screen capture :---------

where C:\106x is my test directory,
filename[.ext] are indeed existing files
fulldirectoryname[ext] are directories containing files
emptydirectoryname[ext] are directories containing nothing
idonotexist[.ext] do not exist.

Can't say whether it'll work for poison characters - especially "!" but
seems ok for normal ASCII. Have my doubts about parentheses as well - but
not the time to test every which-way.


Timo Salmi

unread,
Dec 19, 2007, 3:26:55 PM12/19/07
to
al...@vp.pl <al...@vp.pl> wrote:
> How the current entry is pointing to a file or a directory?

75} How do I detect if an object is a file or a folder?
195399 May 17 2007 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

ten.n...@virgin.net

unread,
Dec 19, 2007, 4:51:22 PM12/19/07
to

Here's an alternate method:
::-----START-----
@ECHO OFF&SETLOCAL ENABLEEXTENSIONS
IF [%1]==[] (ECHO/Usage: %~N0 [File^|Folder]) ELSE (
FOR /F "USEBACKQ DELIMS=" %%? IN ('%*') DO CALL :T_ "%%~f?" %%~a?)
PING -n 6 127.0.0.1>NUL&GOTO :EOF
:T_
IF [%2]==[] (ECHO/%~1 doesn't exist) ELSE (
ECHO/%2|FINDSTR/b d>NUL 2>&1&&ECHO/%~1 is a directory||ECHO/%~1 is a
file)
::------END------
Either drag and drop a file or folder onto it or run it with a file or
folder as a parameter.

al...@vp.pl

unread,
Dec 19, 2007, 8:04:44 PM12/19/07
to
al...@vp.pl wrote:
Thank you all for your suggestions.

foxidrive

unread,
Dec 19, 2007, 9:19:53 PM12/19/07
to
On Wed, 19 Dec 2007 21:51:22 +0000, ten.n...@virgin.net wrote:

> FOR /F "USEBACKQ DELIMS=" %%? IN ('%*') DO CALL :T_ "%%~f?" %%~a?)

Great, another person who uses wildcards inappropropriately where people
learning come to play. <rolls eyes>

ten.n...@virgin.net

unread,
Dec 20, 2007, 6:06:58 AM12/20/07
to

If this was a tutorial group, which it is not BTW, it would be rude of you
not to explain to the learners in this playground what you mean by that
statement. Who are the other people, what is inappropriate about using a
wildcard in this instance etc.

Try in your tutorial not to allow the fact that someone who doesn't sit on
this newsgroup all day pouncing on every question has come up with a fully
working example which noone else, including yourself, had mentioned; and
might I add explained to the less initiated what to do to use it.

Had it not worked, like the billious example earlier you also commented on,
been slow or caused errors/potential errors, then I could understand your
eye rolling escapades; however it isn't.

foxidrive

unread,
Dec 20, 2007, 9:00:56 AM12/20/07
to
On Thu, 20 Dec 2007 11:06:58 +0000, ten.n...@virgin.net wrote:

>On Thu, 20 Dec 2007 13:19:53 +1100, foxidrive wrote:
>
>> On Wed, 19 Dec 2007 21:51:22 +0000, ten.n...@virgin.net wrote:
>>
>>> FOR /F "USEBACKQ DELIMS=" %%? IN ('%*') DO CALL :T_ "%%~f?" %%~a?)
>>
>> Great, another person who uses wildcards inappropropriately
>

>If this was a tutorial group, which it is not BTW

Isn't it? People post here to learn or to get a solution to a problem, and it
is my hope that those will be inspired enough to seek to understand parts of,
or all of the solution. It is how I began to develop an interest in scripting.

Obfuscating wildcards is not helping them...

Tom Lavedas

unread,
Dec 20, 2007, 10:42:39 AM12/20/07
to
On Dec 19, 9:35 am, foxidrive <got...@woohoo.invalid> wrote:

> On Wed, 19 Dec 2007 07:42:39 -0500, "a...@vp.pl" <a...@vp.pl> wrote:
> >I have a file containing many entries (path to a file or path to a
> >directory). I want to loop through this list and perform a different
> >action if the line leads to a file and different when it leads
> >to a directory?
>
> @echo off
> for /f "delims=" %%a in ('type "file.txt"') do (
> echo "%%a"
> dir "%%a"|find "<DIR>">nul&&(echo ---FOLDER---)||(echo file)
> )

Actually there is a third state that this test does not address, that
is, when the pathspec points to a non-existent item (neither dir or
file). Also, since we are in NT+, the find filter isn't necessary, is
it? ...

@echo off
for /f "delims=" %%a in ('echo %1') do (
echo "%%a"
dir /ad /b "%%a" 2>nul&&(goto :Folder)||(
dir /a-d /b "%%a" 1>nul 2>nul&&(goto :File)||goto :Not_Found
)
)
:Folder
echo Folder&goto :eof
:File
echo File&goto :eof
:Not_Found
echo Not found&goto :eof

I did the branching since the OP said he wanted an approach that
performed different actions based on the results.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

foxidrive

unread,
Dec 20, 2007, 11:09:09 AM12/20/07
to
On Thu, 20 Dec 2007 07:42:39 -0800 (PST), Tom Lavedas <tglb...@cox.net> wrote:

>On Dec 19, 9:35 am, foxidrive <got...@woohoo.invalid> wrote:
>> On Wed, 19 Dec 2007 07:42:39 -0500, "a...@vp.pl" <a...@vp.pl> wrote:
>> >I have a file containing many entries (path to a file or path to a
>> >directory). I want to loop through this list and perform a different
>> >action if the line leads to a file and different when it leads
>> >to a directory?
>

>Actually there is a third state that this test does not address, that
>is, when the pathspec points to a non-existent item (neither dir or
>file). Also, since we are in NT+, the find filter isn't necessary, is
>it? ...

The specs for the issue were that it was a list that lead to a file or folder
so it doesn't need the non-existent test, but for completeness you are quite
right and it is useful.

My observation is that this could use a 1>nul as well perhaps?

Tom Lavedas

unread,
Dec 20, 2007, 1:36:46 PM12/20/07
to
On Dec 20, 11:09 am, foxidrive <got...@woohoo.invalid> wrote:

Yes, quite. I tested at a level that did not have subfolders below
it, but with subfolders below the 1>nul is required to keep the screen
clear.

Tom Lavedas

unread,
Dec 20, 2007, 1:40:24 PM12/20/07
to
On Dec 20, 11:09 am, foxidrive <got...@woohoo.invalid> wrote:

Oh, one other thing. Yes, yhe OP stated file or folder, but the
filespecs are subject to error (if a person is involved, which in this
life can't be avoided, I think).

0 new messages