FileManager alway returns true in Linux


(Proyb P) #1

let filename = ""
let checkfile = FileManager.default
var isDir : ObjCBool = false
if checkfile.fileExists(atPath: "files/"+filename, isDirectory: &isDir) {
print("true")
} else {
print("false")
}

On Linux, if the "filename" variable is empty string, checkfile will
returns as true which should be return as false since the file is not
found. macOS is working fine.

A temporary workarounds is to use
let filename = "NOTFOUND"


(John Smith) #2

That is correct as long as the directory “files/“ exists. filename is set to an empty string this your concat statement appends an empty string to your constant string of “files/“ resulting in the path “files/“.

···

On Mar 10, 2017, at 1:53 PM, Proyb P via swift-users <swift-users@swift.org> wrote:

let filename = ""
let checkfile = FileManager.default
var isDir : ObjCBool = false
if checkfile.fileExists(atPath: "files/"+filename, isDirectory: &isDir) {
print("true")
} else {
print("false")
}

On Linux, if the "filename" variable is empty string, checkfile will returns as true which should be return as false since the file is not found. macOS is working fine.

A temporary workarounds is to use
let filename = "NOTFOUND"
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Proyb P) #3

MacOS shown different behavior.

···

On Saturday, 11 March 2017, John Smith <jsmith5pam@yahoo.com> wrote:

That is correct as long as the directory “files/“ exists. filename is set
to an empty string this your concat statement appends an empty string to
your constant string of “files/“ resulting in the path “files/“.

> On Mar 10, 2017, at 1:53 PM, Proyb P via swift-users < > swift-users@swift.org <javascript:;>> wrote:
>
> let filename = ""
> let checkfile = FileManager.default
> var isDir : ObjCBool = false
> if checkfile.fileExists(atPath: "files/"+filename, isDirectory: &isDir) {
> print("true")
> } else {
> print("false")
> }
>
> On Linux, if the "filename" variable is empty string, checkfile will
returns as true which should be return as false since the file is not
found. macOS is working fine.
>
> A temporary workarounds is to use
> let filename = "NOTFOUND"
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org <javascript:;>
> https://lists.swift.org/mailman/listinfo/swift-users


(Marco S Hyman) #4

Running a playground using Xcode 8.2.1 on macOS 10.12.3 your code returns true and the isDir value is also true.

Are you sure the directory “files” exists on your macOS test environment?

let filename = ""
let checkfile = FileManager.default
var isDir : ObjCBool = false

if checkfile.fileExists(atPath: "."+filename, isDirectory: &isDir) {
    print("true, directory = \(isDir)")
} else {
    print("false")
}

results in this output: "true, directory = true”

Marc

···

On Mar 10, 2017, at 12:32 PM, Proyb P via swift-users <swift-users@swift.org> wrote:

MacOS shown different behavior.