StarCoder17
StarCoder17

Reputation: 175

How to check if multiple file exists in a directory

I wanted to check if multiple file with same extension exists in a directory, if found perform the operation. Since file exists only accepts one argument as a filename, its not seems to be working for me.

I tried the following code.

set filenames [glob *.cpf]

      if {[file exists $filenames == 1 }  {
      file delete {*} [glob *.cpf]
} 

This code didn't deletes the files eg: a.cp, b.cpf in a directory and seems to skipping this step. Please suggest if i am missing something.

Dan

Upvotes: 1

Views: 4226

Answers (1)

Dinesh
Dinesh

Reputation: 16428

You may have to check for the length of the received filenames and loop through (if u want) and delete as per you need.

set filenames [glob -nocomplain *.cpf]
# If length of 'filenames' is more than 1 means, multiple files avaialble
if {[llength $filenames]>1} {
    # Your file operations
    foreach fname $filenames    {
        # Loop through the file
    }
}

The flag -nocompain will help us in preventing any error messages if there is no matching pattern.

Reference : glob

Upvotes: 1

Related Questions