使用 VBScript 制作批处理文件代码以使用 Unicode 符号
Posted
技术标签:
【中文标题】使用 VBScript 制作批处理文件代码以使用 Unicode 符号【英文标题】:Making Batch file code with VBScript to work with Unicode symbols 【发布时间】:2015-12-26 16:03:06 【问题描述】:所以最近我在创建 .srt 字幕方面寻求帮助。 Here is the link. 我得到了帮助,一切正常,直到文件夹中的视频文件的名称中有 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
【问题讨论】:
这里使用批处理文件有什么问题?为什么不只使用vbscript,有什么原因吗? 不,完全没有理由。我认为这会更容易,因为这对我来说最熟悉。我个人根本不会编码,只是有一些表面知识。如果有其他方法可以实现我的目标,如果您能提供帮助,我将不胜感激。 【参考方案1】:在查看了上一个问题后,我将所有代码和逻辑合并到一个 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
【讨论】:
一切都很好,但是可以请让文件夹中的视频文件按大小排序,并且只有在该脚本运行之后?除此之外,一切正常! 如果你知道你是如何帮助我的!这里有这么棒的社区!非常感谢! @user5715027 这不是 SO 的工作方式。接受这个作为答案,因为它就是答案。然后用新问题提出一个新问题。 哦,对不起,我以为你不会明白我的问题。以上是关于使用 VBScript 制作批处理文件代码以使用 Unicode 符号的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在批处理文件中嵌入和执行VBScript而不使用临时文件?