0

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
Community
  • 1
  • 1
  • what's the catch using batch-file file here? why don't you only use vbscript, is there a reason for that? – Kul-Tigin Dec 26 '15 at 21:06
  • No, no reason at all. I thought it would be easier, because that was most familiar to me. I personally don't code at all, just have some surface knowledge. If there is another way to accomplish my goal, I would be grateful if you help. –  Dec 26 '15 at 22:42

1 Answers1

3

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
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Everything is great, but could please make that videofiles in folder would be sorted by size, and only after that script would run? Other than that, everything works fine! –  Dec 27 '15 at 00:02
  • If you knew how you helped me out! Such a great community in here! Thank you very much! –  Dec 27 '15 at 02:30
  • @user5715027 this is not how SO works. accept this as answer because it's the answer. then ask a new question with the new issue. – Kul-Tigin Jan 29 '16 at 13:42
  • Oh, i'm sorry, i thought you wouldn't get my question. –  Jan 29 '16 at 16:51