VBScript fopen function

A VBScript equivalent of PHP’s fopen


Public function fopen(filename, mode)

    Dim filePath
    filePath = fileMapPath(filename)

    If left(filePath,len("http://")) = "http://" Then
        fopen = file_get_contents(filePath)
        Exit Function
    End If

    Select Case mode
    Case "r"
        '読み込みのみでオープンします。
        Set ts = fso.OpenTextFile(filePath,1,false)
    Case "w"
        '書き込みでオープンします。
        Set ts = fso.OpenTextFile(filePath,2,true)
    Case "a"
        '追記でオープンします。
        Set ts = fso.OpenTextFile(filePath,8,true)
    Case "x"
        '書き込みでオープンします。ファイルが存在した場合はfalseを返します。
        If is_file(filePath) Then
            fopen = false
        Else
            Set ts = fso.OpenTextFile(filePath,2,true)
        End If
    Case Else
        'empty
        ts = false
    End Select

end function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the filesystem extension