#常用VBS小工具: iconv , unix2dos/dos2unix , base64Encode/Decode
**目录**
[TOC]
##前言
作为一个技术宅,有时候手边没有些随时备用的小工具是不行的,就像下载个游戏说明文档是乱码肿么办?有些配置文件用记事本打开没换行,给它换行之后游戏就无法加载运行了肿么办?有些游戏资源是用`base64`写在配置文档中的肿么办?(额……越往后场景越牵强了……)好吧,收集整理了一些`VBS`小工具,不用安装任何编译器或手动安装解释器,直接存成文本文档改扩展名就能运行,方便快捷,分享出来,或许对更多人有用。另外,代码也简洁易懂,必要时,可以记住方法现写现用,哈哈……
##文本编码转换工具iconv.vbs
文件:`iconv.vbs`
作用:转换文本编码
使用方式:自己双击运行看帮助吧……
```VB
Set oArgs = WScript.Arguments
If oArgs.Count <> 3 Then
outStr = "用法: {wscript | cscript} "& Wscript.ScriptName &" fileName fromCode toCode" & vbcrlf
outStr = outStr & vbcrlf &"可用的编码方式在注册表“HKEY_CLASSES_ROOT\MIME\Database\Charset”查看" & vbcrlf
' set WSHshell=wscript.createobject("WScript.Shell")
' WScript.Echo WSHshell.RegRead("HKEY_CLASSES_ROOT\MIME\Database\Charset\")
Const HKCR = &H80000000
strPath = "MIME\Database\Charset"
Set oReg = GetObject("Winmgmts:\root\default:StdRegProv")
oReg.EnumKey HKCR,strPath,arr
outStr = outStr & vbcrlf & "可用的编码格式有:" & vbcrlf
For Each s In arr
outStr = outStr & s & vbtab
Next
WScript.Echo outStr
Else
ChangeCode oArgs(0),oArgs(1),oArgs(2)
End If
Set oArgs = Nothing
Function ChangeCode(strFile,code1,code2)
Set ADOStrm = CreateObject("ADODB.Stream")
ADOStrm.Type = 2
ADOStrm.Mode = 3
ADOStrm.CharSet = code1
ADOStrm.Open
ADOStrm.LoadFromFile strFile
data= ADOStrm.ReadText
ADOStrm.Position = 0
ADOStrm.CharSet = code2
ADOStrm.WriteText data
ADOStrm.SetEOS
ADOStrm.SaveToFile strFile&"_"&code2&".txt", 2
ADOStrm.Close
End Function
```
##Unix换行转DOS/Windows换行工具unix2dos.vbs
文件:`unix2dos.vbs`
作用:将转换文本文档`unix`换行改为`dos`/`windows`换行
使用方式:将要转换的文档拖到该`vbs`脚本图标上即可
```VB
Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "用法: {wscript | cscript} "& Wscript.ScriptName &" fileName " & vbcrlf
Else
Unix2DosFile(oArgs(0))
End If
Function Unix2DosFile(filePath)
Dim Fso
Set Fso = wscript.CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const isCreateNew = True
Set f=fso.OpenTextFile(filePath, ForReading, isCreateNew)
s=replace(f.ReadAll,vbLf,vbCrLf) ' replace dos to unix format: vbcrlf=chr(13)chr(10)
f.Close
Set f=fso.OpenTextFile(filePath,ForWriting,isCreateNew)
f.Write s
f.Close
Set f=Nothing
Set Fso=Nothing
End Function
```
##DOS/Windows换行转Unix换行工具dos2unix.vbs
文件:`dos2unix.vbs`
作用:将转换文本文档`DOS`/`windows`换行改为`unix`换行
使用方式:将要转换的文档拖到该vbs脚本图标上即可
```VB
Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "用法: {wscript | cscript} "& Wscript.ScriptName &" fileName " & vbcrlf
Else
Dos2UnixFile(oArgs(0))
End If
Function Dos2UnixFile(filePath)
Dim Fso
Set Fso = wscript.CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForWriting = 2
Const isCreateNew = True
Set f=fso.OpenTextFile(filePath, ForReading, isCreateNew)
s=replace(f.ReadAll,vbCrLf,vbLf) ' replace dos to unix format: vbcrlf=chr(13)chr(10)
f.Close
Set f=fso.OpenTextFile(filePath,ForWriting,isCreateNew)
f.Write s
f.Close
Set f=Nothing
Set Fso=Nothing
End Function
```
##Base64编码转换工具base64Encode.vbs
文件:`base64Encode.vbs`
作用:将文件转换为`base64`编码
使用方式:将要转换的文档拖到该`vbs`脚本图标上即可
```VB
Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "用法: {wscript | cscript} "& Wscript.ScriptName &" fileName " & vbcrlf
Else
hexVal = ReadBinary(oArgs(0))
Call WriteFile_Append(oArgs(0) & "_base64.txt", hexVal)
End If
Set oArgs=Nothing
Function ReadBinary(FileName)
Const adTypeBinary = 1
Dim stream, xmldom, node
Set xmldom = CreateObject("Microsoft.XMLDOM")
Set node = xmldom.CreateElement("binary")
node.DataType = "bin.base64"
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeBinary
stream.Open
stream.LoadFromFile FileName
node.NodeTypedValue = stream.Read
stream.Close
Set stream = Nothing
ReadBinary = node.Text
Set node = Nothing
Set xmldom = Nothing
End Function
Public Function WriteFile_Append(pathway,words)
Dim fileSystemObj,fileSpec,logFile,way
Set fileSystemObj = CreateObject("Scripting.FileSystemObject")
fileSpec = pathway
Set logFile = fileSystemObj.OpenTextFile(fileSpec, 8, true)
logFile.WriteLine (CStr(words))
logFile.Close
Set logFile = Nothing
End Function
```
##Base64解码转换工具base64Decode.vbs
文件:`base64Decode.vbs`
作用:将`base64`编码的文本文件解码
使用方式:将要转换的文档拖到该`vbs`脚本图标上即可
```VB
Set oArgs = WScript.Arguments
If oArgs.Count <> 1 Then
WScript.Echo "用法: {wscript | cscript} "& Wscript.ScriptName &" fileName " & vbcrlf
Else
Conv2Binary(oArgs(0))
End If
Set oArgs=Nothing
Function Conv2Binary(FileName)
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Mode = 3
stream.CharSet = "ASCII"
stream.Open
stream.LoadFromFile FileName
Dim stream, xmldom, node
Set xmldom = CreateObject("Microsoft.XMLDOM")
Set node = xmldom.CreateElement("binary")
node.DataType = "bin.base64"
node.Text=stream.ReadText
stream.Close
stream.Type = 1
stream.Mode = 3
stream.Open
stream.Position = 0
stream.write node.NodeTypedValue
stream.SetEOS
stream.SaveToFile FileName & "_decode.txt", 2
stream.Close
Set stream = Nothing
Set node = Nothing
Set xmldom = Nothing
End Function
```