PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average.

Distinguishing between a directory and a file

 
 
Angel Tsankov
Guest
Posts: n/a
 
      13th Mar 2006
How do I check (in a batch file) if a file is a directory?
 
Reply With Quote
 
 
 
 
foxidrive
Guest
Posts: n/a
 
      13th Mar 2006
On Mon, 13 Mar 2006 11:34:33 +0200, Angel Tsankov wrote:

> How do I check (in a batch file) if a file is a directory?



Here's one way:

@echo off
setlocal
if not exist %1 goto :EOF
for /f "delims=" %%a in ('dir %1 ^|find "<DIR>"') do set var=folder
if "%var%"=="folder" (echo %1 is a folder) else (echo %1 is a file)
 
Reply With Quote
 
Jerold Schulman
Guest
Posts: n/a
 
      13th Mar 2006
On Mon, 13 Mar 2006 11:34:33 +0200, Angel Tsankov <NA@NA> wrote:

>How do I check (in a batch file) if a file is a directory?


See tip 10229 » How can a script determine if a file system object is a folder or a file? (03-Mar-06)
in the 'Tips & Tricks' at http://www.jsifaq.com

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
Reply With Quote
 
Dean Wells [MVP]
Guest
Posts: n/a
 
      13th Mar 2006
if exist \windows\nul (echo yes) else (echo no)

.... replace \windows per your requirements.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Angel Tsankov wrote:
> How do I check (in a batch file) if a file is a directory?



 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      13th Mar 2006
Angel Tsankov wrote:
> How do I check (in a batch file) if a file is a directory?


75} How do I detect if an object is a file or a folder?
59} How do I find if a folder exists? How about visible files in it?
157253 Mar 9 2006 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
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      13th Mar 2006
Dean Wells [MVP] wrote:
> if exist \windows\nul (echo yes) else (echo no)


Not in XP. A frequent misunderstanding. The null trick is about
MS-DOS+Win../95/98/Me.

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
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
 
Reply With Quote
 
foxidrive
Guest
Posts: n/a
 
      13th Mar 2006
On Mon, 13 Mar 2006 15:38:15 +0200, Timo Salmi wrote:

> Dean Wells [MVP] wrote:
>> if exist \windows\nul (echo yes) else (echo no)

>
> Not in XP. A frequent misunderstanding. The null trick is about
> MS-DOS+Win../95/98/Me.



It does work in XP, Timo. Rather elegant in its simplicity.
 
Reply With Quote
 
Dean Wells [MVP]
Guest
Posts: n/a
 
      13th Mar 2006
Having tested this prior to posting, I find no problem. Can you
demonstrate where or under what conditions it fails?

I wouldn't label it a trick either, it's simply an artifact of the way
the shell handles well-known names (devices in this case) ... they exist
in every location.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l

Timo Salmi wrote:
> Dean Wells [MVP] wrote:
>> if exist \windows\nul (echo yes) else (echo no)

>
> Not in XP. A frequent misunderstanding. The null trick is about
> MS-DOS+Win../95/98/Me.
>
> All the best, Timo



 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      13th Mar 2006
Dean Wells [MVP] wrote:
> Having tested this prior to posting, I find no problem. Can you
> demonstrate where or under what conditions it fails?


C:\>if exist "C:\Documents and Settings\nul" (echo yes) else (echo no)
no

Next test. Leave out the quotes, and nothing happens.

Next test this, and it goes right
C:\>if exist "C:\Documents and Settings\" (echo yes) else (echo no)
yes

These are tricky tasks.

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
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
 
Reply With Quote
 
Timo Salmi
Guest
Posts: n/a
 
      13th Mar 2006
Timo Salmi wrote:
> Dean Wells [MVP] wrote:
>> Having tested this prior to posting, I find no problem. Can you
>> demonstrate where or under what conditions it fails?


> These are tricky tasks.


Here is one of the ways to do it so that it works also for long paths

@echo off & setlocal enableextensions
if "%~1"=="" (
echo Usage: %~0 [FileOrFolderName]
goto :EOF
)
::
if not exist "%~1" (
echo File or folder "%~1" not found
goto :EOF
)
if exist "%~1\" (
echo "%~1" is a folder
) else (
echo "%~1" is a file
)
endlocal & goto :EOF

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
private.php?do=newpm&u= <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Would like to write batch file to copy all TXT files in a given directory and all subdirectories to a single target directory. Rob Windows XP General 5 20th Aug 2007 02:42 PM
Setting up a directory and moving a file into that directory from =?Utf-8?B?Z3BwbnJkcmo=?= Windows Vista General Discussion 4 11th Jul 2007 01:06 PM
Help: copying directory share names whilst doing bulk directory/file duplication. Troy Microsoft Windows 2000 1 4th May 2005 11:15 AM
vcdeploy : error VCD0035: Failed to create the file system directory for the virtual directory. Access is denied. Dominic Microsoft VC .NET 0 17th Jun 2004 01:27 AM
Distinguishing Name =?Utf-8?B?Q293ZG9nIEdhbA==?= Microsoft Access VBA Modules 1 20th Apr 2004 04:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:29 AM.