ひらがなをカタカナに変換する

VBScriptで、ひらがなをカタカナに変換する方法です。※コピー貼り付けでそのまま使用できます

'定義
hiragana = Array("ぁ","あ","ぃ","い","ぅ","う","ぇ","え","ぉ","お","か","が","き","ぎ","く","ぐ","け","げ","こ","ご","さ","ざ","し","じ","す","ず","せ","ぜ","そ","ぞ","た","だ","ち","ぢ","っ","つ","づ","て","で","と","ど","な","に","ぬ","ね","の","は","ば","ぱ","ひ","び","ぴ","ふ","ぶ","ぷ","へ","べ","ぺ","ほ","ぼ","ぽ","ま","み","む","め","も","ゃ","や","ゅ","ゆ","ょ","よ","ら","り","る","れ","ろ","わ","ゎ","ゐ","ゑ","を","ん")
katakana = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ヮ","ヰ","ヱ","ヲ","ン")
hankata = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ワ","イ","エ","ヲ","ン")


'変換する関数
Function ConvertStr(SrcStr, BefAry, AftAry)
  Dim CnvStr
  CnvStr = SrcStr
  For i=0 To UBound(BefAry)
    CnvStr = Replace(CnvStr, BefAry(i), AftAry(i))
  Next
  ConvertStr = CnvStr
End Function


'実行方法
'ConvertStr("変換したい文字列", hiragana, katakana)


'実行例
msgbox ConvertStr("あいうえお", hiragana, katakana) 
'結果:アイウエオ

 

 

カタカナをひらがなに変換する

VBScriptで、カタカナをひらがなに変換する方法です。※コピー貼り付けでそのまま使用できます

尚、半角カタカナは全角のひらがなに変換します。

'定義
hiragana = Array("ぁ","あ","ぃ","い","ぅ","う","ぇ","え","ぉ","お","か","が","き","ぎ","く","ぐ","け","げ","こ","ご","さ","ざ","し","じ","す","ず","せ","ぜ","そ","ぞ","た","だ","ち","ぢ","っ","つ","づ","て","で","と","ど","な","に","ぬ","ね","の","は","ば","ぱ","ひ","び","ぴ","ふ","ぶ","ぷ","へ","べ","ぺ","ほ","ぼ","ぽ","ま","み","む","め","も","ゃ","や","ゅ","ゆ","ょ","よ","ら","り","る","れ","ろ","わ","ゎ","ゐ","ゑ","を","ん")
katakana = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ヮ","ヰ","ヱ","ヲ","ン")
hankata = Array("ァ","ア","ィ","イ","ゥ","ウ","ェ","エ","ォ","オ","カ","ガ","キ","ギ","ク","グ","ケ","ゲ","コ","ゴ","サ","ザ","シ","ジ","ス","ズ","セ","ゼ","ソ","ゾ","タ","ダ","チ","ヂ","ッ","ツ","ヅ","テ","デ","ト","ド","ナ","ニ","ヌ","ネ","ノ","ハ","バ","パ","ヒ","ビ","ピ","フ","ブ","プ","ヘ","ベ","ペ","ホ","ボ","ポ","マ","ミ","ム","メ","モ","ャ","ヤ","ュ","ユ","ョ","ヨ","ラ","リ","ル","レ","ロ","ワ","ワ","イ","エ","ヲ","ン")


'変換する関数
Function ConvertStr(SrcStr, BefAry, AftAry)
  Dim CnvStr
  CnvStr = SrcStr
  For i=0 To UBound(BefAry)
    CnvStr = Replace(CnvStr, BefAry(i), AftAry(i))
  Next
  ConvertStr = CnvStr
End Function


'実行方法
'ConvertStr(ConvertStr("変換したい文字列", hankata, katakana), katakana, hiragana)


'実行例
msgbox ConvertStr(ConvertStr("アイウエオカキクケコ", hankata, katakana), katakana, hiragana)
'結果:あいうえおかきくけこ

 

全角を半角に変換する

VBScriptで、文字列の全角を半角に変換する方法です。

'================================
' 関数部分
'================================
Const SHIFT_CODE = &H7DE1

'指定した文字列の全角英数を半角に変換して返す
Function StrConvNarrow(strWide)

    Dim length, index, retStr, retChar, tempChar, tempCode
    retStr = ""
    StrConvNarrow = retStr
    
    length = Len(strWide)
    For index = 1 To length
        tempChar = Mid(strWide, index, 1)
        tempCode = Asc(tempChar)
        If (tempCode >= &H824F And tempCode <= &H8258) Or _
            (tempCode >= &H8260 And tempCode <= &H8279) Then
            retChar = Chr(tempCode + SHIFT_CODE)
        ElseIf tempCode >= &H8281 And tempCode <= &H829A Then
            retChar = Chr(tempCode + SHIFT_CODE - 1)
        Else
            retChar = tempChar
        End If
        retStr = retStr & retChar
    Next
    
    StrConvNarrow = retStr

End Function



'================================
' 上の関数の実行例
'================================
msgbox StrConvNarrow("abcde")   'abcde

 

 

半角を全角に変換する

VBScriptで、文字列の半角を全角に変換する方法です。

'================================
' 関数部分
'================================
Const SHIFT_CODE = &H7DE1

'指定した文字列の半角英数を全角に変換して返す
Function StrConvWide(strNarrow)

    Dim length, index, retStr, retChar, tempChar, tempCode
    retStr = ""
    StrConvWide = retStr
    
    length = Len(strNarrow)
    For index = 1 To length
        tempChar = Mid(strNarrow, index, 1)
        tempCode = Asc(tempChar)
        If (tempCode >= &H30 And tempCode <= &H39) Or _
            (tempCode >= &H41 And tempCode <= &H5A) Then
            retChar = Chr(tempCode - SHIFT_CODE)
        ElseIf tempCode >= &H61 And tempCode <= &H7A Then
            retChar = Chr(tempCode - SHIFT_CODE + 1)
        Else
            retChar = tempChar
        End If
        retStr = retStr & retChar
    Next
    
    StrConvWide = retStr

End Function



'================================
' 上の関数の実行例
'================================
msgbox StrConvWide("abcde")   'abcde

 

このサイトで使っている関数は、

このサイトで紹介されていたのを使用しています。

小文字を大文字に変換する

VBScriptで、文字列の小文字を大文字に変換する方法です。

'構文
UCase("文字列")

'実行例
msgbox UCase("abcde")       ' 結果:ABCDE
msgbox UCase("abcde")   ' 結果:ABCDE

 

 

 

大文字を小文字に変換する

VBScriptで、文字列の大文字を小文字に変換する方法です。

'構文
LCase("文字列")

'実行例
msgbox LCase("ABCDE")       ' 結果:abcde
msgbox LCase("ABCDE")   ' 結果:abcde

文字の切り取り

VBScriptで文字列の切り取りをする方法です。

'左からX文字切り取る
left("文字列" , 切り取る文字数X)

'右からX文字切り取る
right("文字列" , 切り取る文字数X)

'文字の左X文字目からY文字切り取る
mid("文字列" , 切り取り開始位置X , 切り取る文字数Y)


'実行例
'"abcdefg"の左から2文字切り取る
msgbox left("abcdefg",2)      '結果:ab

'"abcdefg"の右から2文字切り取る
msgbox right("abcdefg",2)      '結果:fg

'"abcdefg"の左3文字目から4文字切り取る
msgbox mid("abcdefg",3,4)      '結果:cdef

特定の文字が含まれるかを調べる

VBScriptで文字列のバイト数をカウントする方法です。

LenBがうまくカウントしてくれないので次の関数を使用しています。

'バイト数をカウントする関数
Function LenByte(strVal)
    Dim i, strChr
    LenByte = 0
    If Trim(strVal) <> "" Then
        For i = 1 To Len(strVal)
            strChr = Mid(strVal, i, 1)
            If (Asc(strChr) And &HFF00) <> 0 Then
                LenByte = LenByte + 2
            Else
                LenByte = LenByte + 1
            End If
        Next
    End If
End Function

' 上の関数の呼び出し例
msgbox LenByte("abcde")         ' 結果:5
msgbox LenByte("ABCDE")         ' 結果:5
msgbox LenByte("あいうえお")    ' 結果:10
msgbox LenByte("ABCD")      ' 結果:8

特定の文字が含まれるかを調べる

VBScriptである文字列に特定の文字が含まれるかを調べるには、「Instr」を使用します。

'文字が含むかを調べる構文(IF文)
if Instr("この文字列から","この文字を探す") > 0 then
   '探す文字があった時の処理
   msgbox "含みます"
else
   '探す文字がなかった時の処理
   msgbox "含みません"
end if


'======================================
' Instrの補足
'======================================
'Instrは文字が何番目の位置にあるかを返します
Instr("この文字列から","この文字を探す")

'あった場合はその位置が返ります
Instr("abcde","c")   '結果 : 3

'なかった場合は0が返ります
Instr("abcde","g")   '結果 : 0

文字の長さを取得する

VBScriptで文字の長さを取得するには、「len」を使用します。

'構文
len("文字列")

'例 ※結果確認用に「WScript.Echo」でメッセージボックスとして表示します
WScript.Echo len("abc")         ' 結果:3
WScript.Echo len("12345")       ' 結果:5
WScript.Echo len("")            ' 結果:0
WScript.Echo len("ABCDE")  ' 結果:5  ※全角でも1文字は1でカウント
WScript.Echo len(null)          ' 結果:null

フォルダ内のファイル一覧を取得する

フォルダ内のファイル一覧を取得する方法です。

実行例では、ファイル数分メッセージボックスが表示されます。適宜変更をお願いします。

'フォルダ内のファイル一覧を取得する
Sub getFolderFileList(folderPath)
    Dim fso
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")
    FindFolder fso.GetFolder(folderPath)
End Sub

'ファイル一覧を表示する(サブフォルダも再帰する)
Sub FindFolder(ByVal objMainFolder)
    Dim objSubFolder
    Dim objFile
    'フォルダがあれば再帰
    For Each objSubFolder In objMainFolder.SubFolders
        FindFolder objSubFolder
    Next
    'フォルダの中のファイル情報を表示
    For Each objFile In objMainFolder.files
        'フォルダの中のファイル名を表示する(ファイルの数だけ表示されるため注意)
        WScript.Echo objFile.Path
    Next
End Sub

'実行例
getFolderFileList("C:\test1")

フォルダの存在チェック

フォルダの存在チェックをする方法です。

'構文
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists("フォルダのパス") Then
End If

'実行例
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FolderExists("C:\test") Then
   msgbox("存在する")
else
   msgbox("存在しない")
End If

フォルダを作成する

フォルダを作成する方法です。

'構文
Dim fs
Set fs = WScript.CreateObject("Scripting.FileSystemObject")
fs.CreateFolder("フォルダのパス")

'実行例
Dim fs
Set fs = WScript.CreateObject("Scripting.FileSystemObject")
fs.CreateFolder("C:\test\test2")