ファイル ファイルの拡張子名を取得

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Function FileExtensionName(strFileName As StringAs String
'*****************************
'ファイルの拡張子名を取得
'*****************************
'[.]付きで返す

Dim i As Long

i = InStrRev(strFileName, ".")
'i = i + 1 '([.]無しで返す場合)
FileExtensionName = Mid(strFileName, i)

'-------------------------------------------------------------------------
'【構文】
'InstrRev(stringcheck, stringmatch[, start[, compare]])
'文字列から指定文字列を最後から検索し文字位置を返す

'stringcheck    必ず指定    検索先の文字列式を指定。
'stringmatch    必ず指定    検索する文字列式を指定。
'start          省略可能    各検索の開始位置を設定。
'compare        省略可能    文字列比較のモード指定。規定値バイナリモード
'
'引数compareの設定値
'
'定数 値 説明
'vbUseCompareOption    -1 Option Compare ステートメントの設定比較
'vbBinaryCompare        0 バイナリ モード比較
'vbTextCompare          1 テキスト モード比較
'VbDatabaseCompare      2 Microsoft Access の場合
'-------------------------------------------------------------------------
End Function


Function FileExtensionNameFso(strPath As StringAs String
'*****************************
'ファイルの拡張子名を取得FSO
'*****************************
'[.]無しで返す

Dim objSFSO As Object
Set objSFSO = CreateObject("Scripting.FileSystemObject")

FileExtensionNameFso = objSFSO.GetExtensionName(strPath)

Set objSFSO = Nothing

End Function


Function FileNonExtensionNameFso(strPath As String)
'***********************************
'ファイル名だけ拡張子なしを取得FSO
'***********************************
'[.]無しで返す

Dim objSFSO
Set objSFSO = CreateObject("Scripting.FileSystemObject")

FileNonExtensionNameFso = objSFSO.GetBaseName(strPath)

Set objSFSO = Nothing

End Function


Private Sub test()
Dim strNm As String
Dim strPth As String

strNm = ThisWorkbook.Name
strPth = ThisWorkbook.Path & "\" & strNm

MsgBox FileExtensionName(strNm)

MsgBox FileExtensionNameFso(strPth)

MsgBox FileNonExtensionNameFso(strPth)

End Sub

 

2000年01月01日|[VBサンプルコード]:[ファイル]