hello,
have a vbscript to read file names. strip off the file extension. and parse the file name into two parts. if the name is abc1 and the next is abc2 the basename is abc the strNumber(0) = 1 strNumber(1) = 2
where I'm having trouble is determining the regex to work with unusual names like abc, abc-1, acb-2 and the odd case: 1,2,3
there are no formal file naming conventions. individual users can make up any name or series of names. for a series there is often an identifiable pattern. which can be determined by looking at the names. so I attempt to have the computer "look" at the names. ignore non series names.
here is a sample list of some problem names.
1
2
2bases2
2bases
2battle
2hotel
3
4
5
6
escape2_a
escape2_b
escape2_c
escape2_d
escape2_e
op4two1
op4two2
Prisoned2a
Prisoned2b
bh1_l1.bsp
Bh1_l1_bonus.bsp
bh1_l2.bsp
sc_deadthings.bsp
sc_deadthings2.bsp
sc_deadthings3.bsp
sc_tombofdeath.bsp
sc_tombofdeath-2.bsp
sc_tombofdeath-3.bsp
sc_tombofdeath-4.bsp
sc_tombofdeath-5.bsp
read the directory of file names into an array. strip ".bsp"
Code:
Sub FillArray(objCurrentFolder, objLogFile, arrTestNames)
Dim strTemp
Dim strSearch
Dim strOutput
Dim objNewFolder
Dim objFile
Dim objStream
dim a
dim intIndex
strSearch = ".bsp"
intIndex = 0
For Each objFile In objCurrentFolder.Files
strTemp = Right(objFile.Name, 4)
If UCase(strTemp) = UCase(strSearch) Then
'Got one
a=Split(CStr(objFile.Name),".")
'populate database
arrTestNames (intIndex) = a(0)
'strOutput = arrTestNames (intIndex)
'objLogFile.writeline strOutput
intIndex = intIndex + 1
End If
Next
End Sub
create the dictionary
Code:
Const TextCompare = 1
'Start by making a dictionary of all the base names. The value will be an array of the numerals
Set dicBaseNames = CreateObject("Scripting.Dictionary")
dicBaseNames.CompareMode = TextCompare
For Each strName In arrTestNames
strBase = GetBaseName(strName)
strNumber = GetSequenceNumber(strName)
If dicBaseNames.Exists(strBase) Then
nUBound = UBound(dicBaseNames(strBase)) + 1
arrTemp = dicBaseNames(strBase)
ReDim Preserve arrTemp(nUBound)
arrTemp(nUBound) = strNumber
dicBaseNames(strBase) = arrTemp
Else
dicBaseNames.Add strBase, Array(strNumber)
End If
Next
here are the two main functions to split the file names
Code:
Function GetSequenceNumber(strName)
Dim oRE
Dim colMatches
Dim oMatch
Set oRE = New Regexp
oRE.Pattern = "\D*(\d*)(.*$)"
oRE.IgnoreCase = True
Set colMatches = oRE.Execute(strName)
For Each oMatch In colMatches
GetSequenceNumber = oMatch.Submatches(0) & oMatch.Submatches(1)
Exit Function
Next
GetSequenceNumber = ""
End Function
Function GetBaseName(strName)
Dim oRE
Dim colMatches
Dim oMatch
Set oRE = New Regexp
oRE.Pattern = "(\D*)\d*(\D*)"
oRE.IgnoreCase = True
Set colMatches = oRE.Execute(strName)
For Each oMatch In colMatches
GetBaseName = oMatch.SubMatches(0)
Exit Function
Next
GetBaseName = "ERROR"
End Function
and the output subroutine
Code:
Sub StepThruDic(objCurrentFolder, objLogFile)
Dim strTemp
Dim strSearch
Dim strOutput
Dim objNewFolder
Dim objFile
Dim objStream
Dim i
strOutput = "changelevel bug squished. Summary of changes:"
objLogFile.writeline strOutput
For Each strBase in dicBaseNames.Keys
For i = 1 To UBound(dicBaseNames(strBase))
If strBase <> "c" then 'skip all c1 c2 c3 series
strOutput = strBase & dicBaseNames(strBase)(i - 1) & ".cfg" & " " & strBase
objLogFile.writeline strOutput
strOutput = " nextmap" & " " & strbase & dicBaseNames(strBase)(i)
objLogFile.writeline strOutput
objFSO.OpenTextFile(strBase & dicBaseNames(strBase)(i - 1) & ".cfg", 8).WriteLine _
"nextmap" & " " & strbase & dicBaseNames(strBase)(i)
End If
Next
Next
End Sub