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
----- 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
I'm not sure your code will work, Billious.
===[screen capture]===
M:\>if exist m:\a.bat\. echo abc
abc
===[screen capture]===
>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)
)
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.
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
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.
> 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>
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.
>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...
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/
>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?
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.
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).