1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Scalaで"\\server\directory" 形式のpathを "smb://server/directory" に変換してみる

Last updated at Posted at 2014-02-07

Windowsのネットワーク上にあるフォルダにアクセスするときの、\\server で始まるパスはMacだと使えないので、 smb://で始まるpathに変換する必要がある。
これをscalaで実装してみる

match を使う

matchとはswith文みたいなもので、値がcase文にマッチしたらその処理を行う。

x match {
  case 1 => println("one")
  case 2 => println("two")
  case _ => println("other")
}

マッチさせる値はintegerじゃなくてもStringとかでも使える。

match使ったスクリプト

ConvartPath.scala
object Main {
    def main(args: Array[String]) = {
        val arg = args(0)
        print("smb:")
        for (x <- arg) {
            x match {
               case '\\' => print("/")
               case _ => print(value)
            }
         }
	 println("")
    }
}

これを下記のように使う。(引数はバックスラッシュがあるせいで''で囲まないと行けない。""で囲むのはだめ)

$ scala ConvertPath.scala '\\test\test'
smb://test/test

上記は
printlnとか多用してちょっとイケテナイ。
もっとよいスクリプトを書けるはず…。

JavaのAPIを使う

ConvartPath2.scala
object Main {
    def main(args: Array[String]) = {
        val arg = args(0)
        val path = "smb:" + arg.replaceAll("$\\$", "/")
        println(path)
    }
}

JavaのStrigのAPIを使ったほうがシンプルに書ける。
こっちの方がいいかも。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?