Pyprohly
New member
Hey guys, I'm back,
And have your script, Videoclocknet.
I gave up scripting the solution in Batch after deciding the help of regex was sure needed to accomplish the task, and thus I’ve used VBScript instead.
Here are your instructions, Vid', create the VBScript that is attached to this post, whip out a Command Prompt and run a command line similar to the following,
Please create a backup or set up a test environment to trial the script before attempting anything.
Preview
And have your script, Videoclocknet.
I gave up scripting the solution in Batch after deciding the help of regex was sure needed to accomplish the task, and thus I’ve used VBScript instead.
Here are your instructions, Vid', create the VBScript that is attached to this post, whip out a Command Prompt and run a command line similar to the following,
Code:
[COLOR="Silver"]C:\>[/COLOR]cscript /nologo script.vbs "C:\Path\To\*.itb" "C:\Path\To\My\OutputFolder" /fileencoding:utf-16le /recurse
Please create a backup or set up a test environment to trial the script before attempting anything.
Preview
Code:
' th-373293.vbs InputFiles OutputFolder [/FileEncoding:UTF-8] [/RecurseInputFolder:False]
' Desc.: For each file specified by 'InputFiles', read value of the '<FileName>'
' tag in the file, move the file specified by the tag's value to 'OutputFolder'
' then edit the '<FileName>' tag's value to the new location of the moved file.
Set objArgs = WScript.Arguments
Set objNamedArgs = WScript.Arguments.Named
Set fso = CreateObject("Scripting.FileSystemObject")
Set reFilemask = New RegExp
Set reXml = New RegExp
strOpenTag = "<FileName>"
strCloseTag = "</FileName>"
Sub EnforceCScriptUsage
If Not LCase(Right(WScript.FullName, 11)) = "cscript.exe" Then
MsgBox "Please use CScript to execute this script."
WScript.Quit(1)
End If
End Sub
Sub CheckSwitches
If objNamedArgs.Count Then
intCount = 0
If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then intCount = intCount + 1
If (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then intCount = intCount + 1
If Not objNamedArgs.Count = intCount Then
WScript.Echo "Exception: unexpected switches used"
WScript.Quit(1)
End If
End If
End Sub
Function ReadTextFile(strFileName, strCharSet)
With CreateObject("ADODB.Stream")
.Open
.Charset = strCharSet
.LoadFromFile strFileName
ReadTextFile = .ReadText
.Close
End With
End Function
Function WriteTextFile(strFileName, strCharSet, strText)
With CreateObject("ADODB.Stream")
.Open
.Charset = "utf-16le"
.WriteText strText
.SaveToFile strFileName, 2
.Close
End With
End Function
Function ProcessXmlFile(strXmlFileName)
If Not strXmlFileName = WScript.ScriptFullName Then
If reFilemask.Test(fso.GetFileName(strXmlFileName)) Then
WScript.Echo vbCrLf & "Processing '" & strXmlFileName & "'..."
strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
Set objMatchs = reXml.Execute(strXmlContent)
For Each Match in objMatchs
strXmlContent = ReadTextFile(strXmlFileName, strFileEncoding)
XmlFileNameTag = Match.Value
XmlFileNameTagValue = Mid(XmlFileNameTag, Len(strOpenTag) + 1, Len(XmlFileNameTag) - Len(strOpenTag) - Len(strCloseTag))
On Error Resume Next
'WScript.Echo " " & "Moving '" & XmlFileNameTagValue & "'.."
strNewFileName = fso.GetAbsolutePathName(strOutputFolder & "\" & fso.GetFileName(XmlFileNameTagValue))
fso.MoveFile XmlFileNameTagValue, strNewFileName
If Err <> 0 Then
If Err = 53 Then WScript.Echo " " & "Error: non-existing source file was referenced: '" & XmlFileNameTagValue & "'"
If Err = 58 Then
intCounter = 1
strNewFileName = Left(fso.GetFileName(XmlFileNameTagValue), (Len(fso.GetFileName(XmlFileNameTagValue)) - (Len(fso.GetExtensionName(XmlFileNameTagValue)) + 1))) & " (" & intCounter & ")" & "." & fso.GetExtensionName(XmlFileNameTagValue)
Do
intCounter = intCounter + 1
Err.Clear
fso.MoveFile XmlFileNameTagValue, fso.GetAbsolutePathName(strOutputFolder & "\" & strNewFileName)
Loop Until (Err = 0) Or (intCounter > 3000)
If Err = 0 Then
WScript.Echo " " & "Note: source file '" & XmlFileNameTagValue & "' was renamed to '" & strNewFileName & "'"
Else
WScript.Echo " " & "Unhandled Error: " & CStr(Err) & vbCrLf & " " & " " & "Failed to rename '" & XmlFileNameTagValue & "' to '" & strNewFileName & "'"
End If
End If
End If
If Err = 0 Then
strNewXmlContent = Replace(strXmlContent, XmlFileNameTag, strOpenTag & strNewFileName & strCloseTag)
On Error Goto 0
WriteTextFile strXmlFileName, strFileEncoding, strNewXmlContent
End If
On Error Goto 0
Next
End If
End If
End Function
EnforceCScriptUsage()
Select Case objArgs.Unnamed.Count
Case 0
WScript.Echo "Exception: missing 2 required positional arguments: 'InputFiles' and 'OutputFolder'"
WScript.Quit(1)
Case 1
WScript.Echo "Exception: missing 1 required positional argument: 'OutputFolder'"
WScript.Quit(1)
Case 2
strInputFiles = WScript.Arguments.Item(0)
strOutputFolder = WScript.Arguments.Item(1)
strInputFolder = fso.GetParentFolderName(fso.GetAbsolutePathName(strInputFiles))
strInputFilemask = fso.GetFileName(strInputFiles)
strInputFilemaskRegex = strInputFilemask
strInputFilemaskRegex = Replace(strInputFilemaskRegex, ".", "\.")
strInputFilemaskRegex = Replace(strInputFilemaskRegex, "?", ".")
strInputFilemaskRegex = Replace(strInputFilemaskRegex, "*", ".*")
strInputFilemaskRegex = "^" & strInputFilemaskRegex & "$"
Case Else
MsgBox "Exception: this script takes exactly 2 arguments but " & objArgs.Count & " were given"
WScript.Quit(1)
End Select
CheckSwitches()
If (objNamedArgs.Exists("FileEncoding")) And (objNamedArgs.Exists("E")) Then
WScript.Echo "Exception: 'FileEncoding' optional parameter cannot be used twice"
WScript.Quit(1)
ElseIf (objNamedArgs.Exists("FileEncoding")) Or (objNamedArgs.Exists("E")) Then
If objNamedArgs.Exists("FileEncoding") Then strFileEncoding = objNamedArgs.Item("FileEncoding")
If objNamedArgs.Exists("E") Then strFileEncoding = objNamedArgs.Item("E")
Else
strFileEncoding = "UTF-8"
End If
reFilemask.Global = True
reFilemask.IgnoreCase = True
reFilemask.Pattern = strInputFilemaskRegex
reXml.Global = True
reXml.IgnoreCase = False
reXml.Pattern = strOpenTag & ".+?" & strCloseTag
If Not fso.FolderExists(strOutputFolder) Then
WScript.Echo "Exception: the output folder '" & fso.GetAbsolutePathName(strOutputFolder) & "' does not exist"
WScript.Quit(1)
End If
If (objNamedArgs.Exists("Recurse")) Or (objNamedArgs.Exists("RecurseInputFolder")) Or (objNamedArgs.Exists("R")) Then
Function RecurseFolders(objFolder)
For Each objFile In objFolder.Files
ProcessXmlFile(objFile.Path)
Next
For Each strSubFolder In objFolder.SubFolders
RecurseFolders(strSubFolder)
Next
End Function
RecurseFolders fso.GetFolder(strInputFolder)
Else
For Each objFile In fso.GetFolder(strInputFolder).Files
ProcessXmlFile(objFile.Path)
Next
End If
Attachments
My Computer
- Computer type
- PC/Desktop
- OS
- Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan