制作批处理文件code使用VBScript和UNI code符号工作 [英] Making Batch file code with VBScript to work with Unicode symbols
问题描述
所以最近我要求帮助与创建.SRT字幕。 <一href=\"http://stackoverflow.com/questions/34457893/batch-to-create-video-srt-subtitles-with-right-timing\">Here是链接。
我得到的帮助和一切工作正常,直到文件夹中的录像档案在其名称UNI code符号。如果确实如此,则出现的VBScript错误。问题是如何正确地做这个code工作,统一code符号。
这里是code:
关闭@echo和放大器; CLS::您的视频文件的路径设置$将videoPath = C:\\ FolderwithVideos::如果你想在这个BAT您code删除REMS下图:REM目录%$将videoPath%/ O:S / B / -p / O:GN&GT; C:\\的Result.txt
REM电话replacer.bat的Result.txt.MP4SETLOCAL enabledelayedexpansion
集/ A $计数= 1
设置$定时器= 00:00:00
(FOR / Fdelims =%%一中(的Result.txt)做(
拨打:getVideolength%% a.mp4
FOR / Fdelims =%%倍In('CSCRIPT // NOLOGO getvideolength.vbs')做(
来电:SumTime $计时器! %%X
FOR / Fdelims =%%的Y('CSCRIPT // NOLOGO SumTime.vbs)并设置$ NewTimer = %% Y
回音!$算!
回声$定时器,000 - ^&GT;! !$ NewTimer!000
回声%%一
设置$沙漏!$ NewTimer!
)
集/ A $计数+ = 1
回声。
))GT; Output.srt回声完成!
键入Output.srt
暂停
退出/ B:GetVideoLength
(回声暗淡objShell
回声暗objFolder
回声暗objFolderItem
呼应设置objShell =的CreateObject(shell.application^)
呼应设置objFolder = objShell.NameSpace(%$将videoPath%^)
呼应设置objFolderItem = objFolder.ParseName(%1 ^)
回声暗objInfo
呼应objInfo = objFolder.GetDetailsOf(objFolderItem,27 ^)
回声wscript.echo objinfo)GT; GetVideoLength.vbs
退出/ B
:SumTime
回声wscript.echo的FormatDateTime(CDATE(%1^)+ CDATE(%2^),3 ^)&GT; SumTime.vbs
退出/ B
回顾previous的问题,我合并所有的code和逻辑到一个单向code安全VBScript文件后。
显式的选项常量adUnsignedBigInt = 21
常量adVarWChar = 202
常量adVarChar = 200昏暗VideoDir
VideoDir =C:\\ FolderwithVideos
或者叫的时候像一个脚本参数:CSCRIPT script.vbsC:\\ FolderwithVideos
'VideoDir = WScript.Arguments(0)常量adSaveCreateOverWrite = 2
常量adCRLF = -1
常量adWriteLine = 1昏暗ShellApp
设置ShellApp =的CreateObject(Shell.Application)Fso的暗淡
设置Fso的= CREATEOBJECT(Scripting.FileSystemObject的)昏暗VideoExts
设置VideoExts =的CreateObject(的Scripting.Dictionary)
VideoExts.CompareMode = vbTextCompare
VideoExts.AddMP4,NULL
添加更多的扩展,如果你需要
VideoExts.AddSRT,NULL
VideoExts.AddMKV,NULL昏暗SrtStream
设置SrtStream =的CreateObject(的ADODB.Stream)
SrtStream.Charset =UTF-8
SrtStream.Open暗淡的文件夹,IFolderItem,VIDEOCOUNT,OldDuration,NewDuration,FileExtension,SrtPath,RsSorted
设置RsSorted =的CreateObject(Adodb.Recordset)
RsSorted.Fields.Append姓名,adVarWChar,255
RsSorted.Fields.Append大小,adUnsignedBigInt
RsSorted.Fields.Append持续时间,adVarChar,8
RsSorted.OpenNewDuration = TimeSerial的(0,0,0)
设置文件夹= ShellApp.NameSpace(VideoDir)
对于每个IFolderItem在Folder.Items
FileExtension = Fso.GetExtensionName(IFolderItem.Name)
如果VideoExts.Exists(FileExtension)然后
RsSorted.AddNew阵列(名称,大小,持续时间),阵列(IFolderItem.Name,IFolderItem.Size,Folder.GetDetailsOf(IFolderItem,27))
万一
下一个
RsSorted.UpdateBatch
RsSorted.Sort =大小,名称
如果没有RsSorted.BOF然后RsSorted.MoveFirst
虽然没有RsSorted.EOF与不RsSorted.BOF
FileExtension = Fso.GetExtensionName(RsSorted(姓名)。值)
VIDEOCOUNT = VIDEOCOUNT + 1
OldDuration = NewDuration
NewDuration = OldDuration + CDATE(RsSorted(持续时间)。值)
SrtStream.WriteText VIDEOCOUNT,adWriteLine
SrtStream.WriteText OldDuration&安培; 001 - &gt;中和放大器; NewDuration&安培; 000,adWriteLine
SrtStream.WriteText左(RsSorted(姓名)值,莱恩(RsSorted(姓名)值) - (LEN(FileExtension)+ 1)),adWriteLine
SrtStream.WriteText,adWriteLine
RsSorted.MoveNext
WENDSrtPath = Fso.BuildPath(VideoDirOutput.srt)
SrtStream.SaveToFile SrtPath,adSaveCreateOverWrite
SrtStream.CloseWScript.Echo完成!
WScript.Echo SrtPath
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
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
这篇关于制作批处理文件code使用VBScript和UNI code符号工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!