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
If exist "name\"
will return true if name is a directory, false if name is a file or doesn't
exist
@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
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..???
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
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
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
==============
Thank you! Love examples.
-T
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
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
> 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
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
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...
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
@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
)
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...
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
> 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
Question: how do I test for the "d" (directory) flag?
Many thanks,
-T
@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
==
Cheers, Tim Meddick, Peckham, London. :-)
"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:Rxtuo.3830$no5....@newsfe12.iad...
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
(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...
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
==
Cheers, Tim Meddick, Peckham, London. :-)
"foxidrive" <foxi...@gotcha.woohoo.invalid> wrote in message
news:SyUuo.3029$yw....@newsfe18.iad...
You can lead a horse to water...
Google in alt.msdos.batch.nt and find the threads for yourself.
--
Regards,
Mic
It does not appear you have a point, top poster.
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...