Making Batch file code with VBScript to work with Unicode symbols

故事扮演 提交于 2019-12-08 11:09:06

问题


So recently I asked for help with creating .srt subtitles. Here is the link. I got the help and everything works fine until the videofile in the folder has unicode symbols in its name. If it does, then VBScript error appears. Question is how to make this code work correctly with Unicode symbols. Here is the code:

@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

回答1:


After reviewing the previous question I merged all the code and logic into one unicode safe VBScript file.

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


来源:https://stackoverflow.com/questions/34472943/making-batch-file-code-with-vbscript-to-work-with-unicode-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!