0

I am trying to check if file exist at the path in swift 3 but it always shows "NO FILE EXIST"

I have also tried it with atPath: self.strTitle+"/back/index.html" and even this doesn't work.

here strTitle is a file path located inside asset folder and is read through an array. There lies two pages one on the back side and one on the front. So there is front for all but back pages are limited to few. And this is where it breaks.

print("STR TITLE::  ",self.strTitle) .   // STR TITLE = assets/a6th_nerve_palsy
let fileManager : FileManager   = FileManager.default

if fileManager.fileExists(atPath: self.strTitle+"/back/index"){
    print("FILE EXIST")

    }else{
        print("NO FILE EXIST")
    }

Thanks in advance :)

Prateekro
  • 566
  • 1
  • 6
  • 28
  • 1
    are you getting the file from your bundle ? – Nikunj Damani Mar 02 '17 at 06:01
  • Yes. I have it all stored in my assets – Prateekro Mar 02 '17 at 06:02
  • 2
    Why would you check if the file you put yourself in your assets if it is obviously there? Just get your resource url using `Bundle.main.url(forResource: "fileName", withExtension: "ext")` If your file it is located inside a subdirectory just use `Bundle.main.url(forResource: "fileName", withExtension: "ext", subdirectory: "subdirectory")` http://stackoverflow.com/questions/34548771/swift-how-do-i-get-the-file-path-inside-a-folder/34548888#34548888 – Leo Dabus Mar 02 '17 at 06:48
  • so I have two directories. assets/abc/index.html and assets/abc/back/index.html . where all assets don't have back/index.html – Prateekro Mar 02 '17 at 06:51
  • 1
    Thanks @LeoDabus I found a work around with you link's help. Will post my answer down here :) – Prateekro Mar 02 '17 at 06:53
  • 1
    try `bundle.path(forResource: "index", withExtension: "html", subdirectory: strTitle + "/back" )` – Leo Dabus Mar 02 '17 at 06:59

3 Answers3

0

to find the path you should try this code.

class Controller : UIViewController {

    override func viewDidLoad() {
        let bundle = Bundle(for: Controller.self)
        let filePath = bundle.path(forResource: "index", ofType: "html")
        let fileManager : FileManager   = FileManager.default

        if fileManager.fileExists(atPath: filePath!){
            print("file found")
        } else {
            print("file not found")
        }
    }

}
Nikunj Damani
  • 753
  • 3
  • 11
0

After debugging and with help of @LeoDabus I could figure out. It could directly be done to check file path existence.

let bundle = Bundle(for: PageContentViewController.self)
let filePath = bundle.path(forResource: self.strTitle+"/back/index", ofType: "html")

        if filePath == nil{
            print("FILE NOT FOUND")
            // Show No page as page do not exist
        } else {
            print("FILE FOUND")
            // Show Back page
        }

Thanks all. :)

Prateekro
  • 566
  • 1
  • 6
  • 28
0
let fileName = getDocumentsDirectory().appendingPathComponent("\(lastPath)")
          if FileManager.default.fileExists(atPath: fileName){
                        try? FileManager.default.removeItem(atPath: fileName)
               }

func getDocumentsDirectory() -> NSString {
     let paths =  NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = paths[0]
    return documentsDirectory as NSString
}
Punit
  • 1,330
  • 6
  • 13