SFV File parser in batch

Home made / NONE
April 18, 2009 at 13:07:07
Specs: Windows XP, 2.8 ghz / 2 gig
I made a script to compared the crc in a file name to the actual crc of a file. There is a txt file with a file name then the files crc on each line. The script finds the crc in the file name and compares it to the actual crc.

Example of a line from the file.
[a-s]_blood;_the_last_vampire__killerhis__[f6d0e757].mkv F6D0E757

----------------------------------------------------------------------
@echo off

setlocal enabledelayedexpansion

echo drag and drop sfv onto window
set /p settings=
cls

for /f "usebackq tokens=*" %%x in (%settings%) do (
set line=%%x
set crc=!line:~-8!
set filename=!line:~0,-9!

call :check !filename!
if /I not "!z:~0,8!" == "!crc!" (
echo -
echo !filename!
echo !crc!
echo -
)
set z=
)

goto end

:check
set x= %1%2%3
:again

if "%x:~-1%" == "[" set s=%z% & goto :EOF

set z=%x:~-1%%z%

set x=%x:~0,-1%

goto again

:end
pause

----------------------------------------------------------------------

The script works so far but it returns many false positives, which I believe is due to characters in the file names. I believe ; , & are some characters throwing it off.

I found changing set x= %1 in the first line of the check subroutine to set x= %1%2%3 eliminates the false positive that include ; and , in the file name. Do those characters cause the script to think the file name is multiple strings instead of one?


See More: SFV File parser in batch

Report •


#1
April 18, 2009 at 19:51:55
Yes, it does. The ampersand also has special meaning to CMD.

Report •

#2
April 18, 2009 at 20:39:43
thanks very helpful

well I found out after some searching that ^ is an escape character so i replaced any instance of & in the filename var like this set filename=!filename:^&=.!

Anyone know of anyway to speed this up? Such as a way to extract the crc from the file name instead of checking each character from the end of the file backwards?


Report •

#3
April 18, 2009 at 22:32:01
Since your using delayed expansion anyway try changing all of your variables to use !exclaimationmarks!, doing so should escape all the special characters.

As for "set x= %1%2%3" try using:

for %%a in (" %~1%~2%~3") do set x=%%~a

It shoul escape the special characters in the parameters.


Report •

Related Solutions

#4
April 20, 2009 at 03:21:45
Anyone know of anyway to speed this up?
Use any other language?

VBScript:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder(".").Files
  If LCase(fso.GetExtensionName(f)) = "sfv" Then _
    ParseSFV fso.OpenTextFile(f)
Next 'f

Sub ParseSFV(sfv)
  Do Until sfv.AtEndOfStream
    line = sfv.ReadLine
    s = InStrRev(line, " ")
    If s Then
      crc = Mid(line, s + 1)
      line = Left(line, s)
      If InStr(1, line, crc, 1) = 0 Then _
        WScript.Echo "CRC mismatch: " & line
    End If
  Loop
End Sub


Report •

#5
April 20, 2009 at 13:16:28
thanks for the vbs script. How could you have the mismatch ones echoed to a window instead of separate popups? Having separate popups is a bit of a pain.

also how would you make it ignore lines that start with ;


Report •

#6
April 22, 2009 at 11:30:58
How could you have the mismatch ones echoed to a window instead of separate popups? Having separate popups is a bit of a pain.
Normally, I'd be willing to tell you to just use cscript, but I've been thinking about DHTML, so WTH, I might as well wrap it in a HTApplication.
SFV.HTA:
<html>
<head>
  <hta:application applicationName="SVFCheck" />
  <title>SFV Checker</title>
  <style type="text/css">
    body { background: #CCFFCC }
  </style>
  <script language="vbscript">
    Sub Main()
      window.resizeTo 700,500
      Set outList = document.createElement("ul")
      document.body.appendChild outList
      Set fso = CreateObject("Scripting.FileSystemObject")
      For Each f In fso.GetFolder(".").Files
        If LCase(fso.GetExtensionName(f)) = "sfv" Then
          Set out = document.CreateElement("li")
          out.innerHTML = f.Name
          out.appendChild ParseSFV(fso.OpenTextFile(f))
          outList.appendChild out
        End If
      Next 'f
      If document.hasChildNodes(outList) Then _
        document.body.style.background = "#FFCCCC"
    End Sub
    
    Function ParseSFV(sfv) 'As ul
      Set ret = document.createElement("ul")
      Do Until sfv.AtEndOfStream
        line = Trim(sfv.ReadLine)
        s = InStrRev(line, " ")
        If s And Left(line, 1) <> ";" Then
          crc = Mid(line, s + 1)
          line = Left(line, s)
          If InStr(1, line, crc, 1) = 0 Then
            Set li = document.CreateElement("li")
            li.innerHTML = "CRC mismatch: " & line
            li.title = "Expected: " & crc
            ret.appendChild li
          End If
        End If
      Loop
      Set ParseSFV = ret
    End Function
  </script>
</head>
<body onload="Main" />
</html>


Report •

#7
April 22, 2009 at 11:52:51
Thank you very much.

This type of scripting is very interesting.


Report •


Ask Question