使用 VBScript 制作批处理文件代码以使用 Unicode 符号

Making Batch file code with VBScript to work with Unicode symbols

所以最近我寻求有关创建 .srt 字幕的帮助。 我得到了帮助,一切正常,直到文件夹中的视频文件名称中包含 unicode 符号。如果是,则会出现 VBScript 错误。问题是如何使此代码与 Unicode 符号一起正确工作。 这是代码:

@echo off&cls

::The Path of your Videos files

set "$VideoPath=C:\FolderwithVideos"

::If you want your Code in this BAT remove the REMs Below :

rem dir "%$VideoPath%" /O:S /b /-p /o:gn > "C:\result.txt"
rem call replacer.bat result.txt ".mp4" ""

setlocal enabledelayedexpansion
set /a $Count=1
set "$Timer=00:00:00"


(for /f "delims=" %%a in (result.txt) do (
  call:getVideolength "%%a.mp4"
  for /f "delims=" %%x in ('cscript //nologo getvideolength.vbs') do (
       call:SumTime !$Timer! %%x
       for /f "delims=" %%y in ('cscript //nologo SumTime.vbs') do set "$NewTimer=%%y"
       echo !$Count!
       echo !$Timer!,000 --^> !$NewTimer!,000
       echo %%a
       Set $Timer=!$NewTimer!
  )
  set /a $Count+=1
  echo.
))>Output.srt

echo Done !!!
type Output.srt
pause
exit/b

:GetVideoLength
(echo dim objShell
echo dim objFolder
echo dim objFolderItem
echo set objShell = CreateObject("shell.application"^)
echo set objFolder = objShell.NameSpace("%$videoPath%"^)
echo set objFolderItem = objFolder.ParseName(%1^)
echo dim objInfo
echo objInfo = objFolder.GetDetailsOf(objFolderItem, 27^)
echo wscript.echo objinfo)>GetVideoLength.vbs
exit/b


:SumTime
echo wscript.echo FormatDateTime(CDate("%1"^) + CDate("%2"^),3^) >SumTime.vbs
exit/b

在回顾了上一个问题后,我将所有代码和逻辑合并到一个 unicode 安全的 VBScript 文件中。

Option Explicit

Const adUnsignedBigInt = 21
Const adVarWChar = 202
Const adVarChar = 200

Dim VideoDir
    VideoDir = "C:\FolderwithVideos"
    'or from a script argument when called like : cscript script.vbs "C:\FolderwithVideos"
    'VideoDir = WScript.Arguments(0)

Const adSaveCreateOverWrite = 2
Const adCRLF = -1
Const adWriteLine = 1

Dim ShellApp
Set ShellApp = CreateObject("Shell.Application")

Dim Fso
Set Fso = CreateObject("Scripting.Filesystemobject")

Dim VideoExts
Set VideoExts = CreateObject("scripting.dictionary")
    VideoExts.CompareMode = vbTextCompare
    VideoExts.Add "mp4", Null
    'add more extensions if you need
    'VideoExts.Add "srt", Null
    'VideoExts.Add "mkv", Null

Dim SrtStream
Set SrtStream = CreateObject("Adodb.Stream")
    SrtStream.Charset = "utf-8"
    SrtStream.Open

Dim Folder, IFolderItem, VideoCount, OldDuration, NewDuration, FileExtension, SrtPath, RsSorted
Set RsSorted = CreateObject("Adodb.Recordset")
    RsSorted.Fields.Append "Name", adVarWChar, 255
    RsSorted.Fields.Append "Size", adUnsignedBigInt
    RsSorted.Fields.Append "Duration", adVarChar, 8
    RsSorted.Open

NewDuration = TimeSerial(0,0,0)
Set Folder = ShellApp.NameSpace(VideoDir)
For Each IFolderItem In Folder.Items
    FileExtension = Fso.GetExtensionName(IFolderItem.Name)
    If VideoExts.Exists(FileExtension) Then
        RsSorted.AddNew Array("Name", "Size", "Duration"), Array(IFolderItem.Name, IFolderItem.Size, Folder.GetDetailsOf(IFolderItem, 27))
    End If
Next
    RsSorted.UpdateBatch
    RsSorted.Sort = "Size, Name"
If Not RsSorted.BOF Then RsSorted.MoveFirst
While Not RsSorted.EOF And Not RsSorted.BOF
    FileExtension = Fso.GetExtensionName(RsSorted("Name").Value)
    VideoCount = VideoCount + 1
    OldDuration = NewDuration
    NewDuration = OldDuration + CDate(RsSorted("Duration").Value)
    SrtStream.WriteText VideoCount, adWriteLine
    SrtStream.WriteText OldDuration & ",001 --> " & NewDuration & ",000", adWriteLine       
    SrtStream.WriteText Left(RsSorted("Name").Value, Len(RsSorted("Name").Value) - (Len(FileExtension) + 1)), adWriteLine
    SrtStream.WriteText "", adWriteLine
    RsSorted.MoveNext
Wend

SrtPath = Fso.BuildPath(VideoDir, "Output.srt")
SrtStream.SaveToFile SrtPath, adSaveCreateOverWrite
SrtStream.Close

WScript.Echo "Done!"
WScript.Echo SrtPath