Version 14 of file exists

Updated 2007-06-04 09:04:28 by mb

file exists name

Returns 1 if file name exists and the current user has search privileges for the directories leading to it, 0 otherwise.


Q. What's the difference between file exists and file isfile?

A. file exists returns true (1) for all existing directories and regular files. file isfile returns true only for existing regular files.


Note: if [file exists $a] && ([file type $a] == "file") then [file exists ${a}/], which seems inconsistent.

In normal usage, it wouldn't bother anyone, but in VFS work, there can be a world of difference between a file "$a", a directory "$a", and a file "$a/". I suspect this stems from [file split a/b] == {a b} != {a b {}} - 20040613 CMcC


MB : I have a problem on using the file exist command under windows XP while processing file names. The following script exhibits the problem. When one file "myfile.txt" is created, the file exist command says that the file "MYFILE.txt" exists, which is wrong since the two files have different name cases. I wonder if this is a portability problem because of the Windows XP platform or an internal bug of the Tcl command file exist.

  #
  # Test to know whether windows can take the case into account
  # in the file names
  #
  # 1. Create a new directory
  # 2. Create a new file in that directory
  # 3. Check that under windows the Tcl command "file exist" returns a wrong value.
  #
  package require fileutil
  set tmpdir [fileutil::tempdir]
  set newdir [file join $tmpdir "dirtotest"]
  file mkdir $newdir
  set filename [file join $newdir "myfile.txt"]
  puts "Touching $filename"
  fileutil::touch $filename
  set fexist [file exist $filename]
  puts "File exist : $fexist"
  set otherfilename [file join $newdir "MYFILE.txt"]
  puts "Other file : $otherfilename"
  set fexist [file exist $otherfilename]
  puts "Other file exist : $fexist"
  #
  # Check that the other file is not is the list returned by "glob"
  #
  set listOfFiles [glob -directory $newdir *]
  puts "Files : $listOfFiles"
  set found [lsearch $listOfFiles $filename]
  puts "Found $filename in the directory $newdir : $found"
  set found [lsearch $listOfFiles $otherfilename]
  puts "Found $otherfilename in the directory $newdir : $found"
  #
  # Clean-up
  #
  file delete -force $newdir

This script generates the output :

  Touching C:/WINDOWS/Temp/dirtotest/myfile.txt
  File exist : 1
  Other file : C:/WINDOWS/Temp/dirtotest/MYFILE.txt
  Other file exist : 1
  Files : C:/WINDOWS/Temp/dirtotest/myfile.txt
  Found C:/WINDOWS/Temp/dirtotest/myfile.txt in the directory C:/WINDOWS/Temp/dirtotest : 0
  Found C:/WINDOWS/Temp/dirtotest/MYFILE.txt in the directory C:/WINDOWS/Temp/dirtotest : -1

We can see that the returned value of the file exist command is wrong, and that the list returned by glob only shows the file "myfile.txt", which is the least ! I have not tested the same script under Linux, but it would be interesting. What is the explanation ?


See also:


Tcl syntax help - Category Command - Category File