#navi(../)
* ディレクトリ・ファイルの有無確認方法 [#p91bf36f]
ファイルまたはディレクトリが存在するかしないかを確認するサンプルコードを以下に記します。~
有無確認は以下の構文で簡単に調べることができます。
 File.exist?(ファイル名orディレクトリ名)
#contents
#htmlinsertpcsp(ll-top.html,ll-sp.html)


** サンプルコード [#bba027a0]
以下にサンプルコードを記します。
#ref(file_exist.rb)
 # check directory
 p File.exist?("/tmp")
 p File.exist?("/hoge")
 # check file
 p File.exist?("/etc/hosts")
 p File.exist?("/etc/hoge")

** 実行結果 [#f5f8234d]
 $ ruby file_exist.rb 
 true
 false
 true
 false
存在する場合はtrue、しない場合はfalseが返却されます。

#htmlinsertpcsp(ll-btm.html,ll-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS