Option Explicit
On Error Resume Next
dim oArgs, sPfad, sType, cFiles, sText, ni, bFound, nMaxFileSize
dim oFSO, oFolder, oFile, olFSO, olFile
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const logging=0
Const LogFile=".\isoconvert.log"
nMaxFileSize = 10*1024*1024

dim aReplace(5,1)
aReplace(0,0)="ISO:camt.052.001.02:APC:STUZZA:payments:003"
aReplace(0,1)="urn:iso:std:iso:20022:tech:xsd:camt.052.001.02"
aReplace(1,0)="ISO:camt.053.001.02:APC:STUZZA:payments:003"
aReplace(1,1)="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02"
aReplace(2,0)="ISO:camt.054.001.02:APC:STUZZA:payments:003"
aReplace(2,1)="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"
aReplace(3,0)="ISO:camt.052.001.02:APC:STUZZA:payments:004"
aReplace(3,1)="urn:iso:std:iso:20022:tech:xsd:camt.052.001.02"
aReplace(4,0)="ISO:camt.053.001.02:APC:STUZZA:payments:004"
aReplace(4,1)="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02"
aReplace(5,0)="ISO:camt.054.001.02:APC:STUZZA:payments:004"
aReplace(5,1)="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"

dim aExt(3)
aExt(0)="XML"
aExt(1)="052"
aExt(2)="053"
aExt(3)="054"

set oArgs=wscript.Arguments

if oArgs.Count>0 then
  if instr(oArgs(0),"?")<>0 then
    wscript.echo VBCRLF & "Dieses BusinessLine Skript kann nicht direkt aufgerufen werden" & VBCRLF
    wscript.quit
  end if
end if

if oArgs.Count <> 2 then
  wscript.echo VBCRLF & "Syntaxfehler beim Aufruf des BusinessLine Skripts" & VBCRLF
  wscript.quit
end if

sPfad=oArgs(0)
sType=oArgs(1)

if logging = 1 then
  set olFSO = CreateObject("Scripting.FileSystemObject")
  LogToFile "Script started: " & sPfad & " " & sType
end if

if sType = "-D" then
  ReplaceNameSpace sPfad, aReplace
elseif sType = "-V" then
  set oFSO = CreateObject("Scripting.FileSystemObject")
  set oFolder = oFSO.GetFolder(sPfad)
  if Err.Number <> 0 then ErrorHandling "Error Folder access " & sPfad, Err.Number, Err.Description end if
  set cFiles = oFolder.Files
  for each oFile in cFiles
    if oFile.size > 0 and oFile.size < nMaxFileSize and oFile.Name <> wscript.scriptname and UBOUND(Filter(aExt, oFSO.getextensionname(oFile.path),1,1)) > -1  then
	  sText = oFSO.OpenTextFile(oFile.Path).ReadAll
	  if Err.Number <> 0 then ErrorHandling "Error reading from " & oFile.Path, Err.Number, Err.Description end if
	  bFound = 0
	  for ni = 0 to UBOUND(aReplace)
        if instr(sText, aReplace(ni,0)) > 0 Then
          bFound = 1
		  exit for
        end if
	  next
	  if bFound = 1 then
	    if logging = 1 then
		  LogToFile "replace invoked on " & oFile.Path
		end if
	    ReplaceNameSpace oFile.Path, aReplace
	  end if
    end if
  next
else
  wscript.echo VBCRLF & "Syntaxfehler beim Aufruf des BusinessLine Skripts, unbekannter Type-Parameter " & sType & VBCRLF
  if logging = 1 then
    LogToFile "unknown type-parameter " & sType
  end if
end if

if logging = 1 then
  LogToFile "Script stopped: " & sPfad & " " & sType
end if

wscript.quit 

sub ReplaceNameSpace (ssRNFileName, asRNReplace)
  On Error Resume Next
  dim objsRNFSO, objsRNFile, ssRNText, ssRNNewText, nsRNi
  Set objsRNFSO = CreateObject("Scripting.FileSystemObject")
  Set objsRNFile = objsRNFSO.OpenTextFile(ssRNFileName, ForReading)
  if Err.Number <> 0 then ErrorHandling "Error open for reading " & ssRNFileName, Err.Number, Err.Description end if
  ssRNText = objsRNFile.ReadAll
  if Err.Number <> 0 then ErrorHandling "Error reading from " & ssRNFileName, Err.Number, Err.Description end if
  objsRNFile.Close
  for nsRNi = 0 to UBOUND(asRNReplace)
    ssRNText = Replace(ssRNText, asRNReplace(nsRNi,0), asRNReplace(nsRNi,1),1,-1,1)
  next
  Set objsRNFile = objsRNFSO.OpenTextFile(ssRNFileName, ForWriting)
  if Err.Number <> 0 then ErrorHandling "Error open for appending " & ssRNFileName, Err.Number, Err.Description end if
  objsRNFile.Write ssRNText
  if Err.Number <> 0 then ErrorHandling "Error writing to " & ssRNFileName, Err.Number, Err.Description end if
  objsRNFile.Close
end sub

sub LogToFile (sLogText)
  On Error Resume Next
  set olFile = olFSO.OpenTextFile(LogFile, ForAppending, 1)
  olFile.WriteLine now & " : " & sLogText
  olFile.Close
  Err.Clear
end sub

sub ErrorHandling (sErrText, nErrNumber, sErrDesc)
  On Error Resume Next
  if logging = 1 then
	LogToFile sErrText & " - E: " & nErrNumber & " : " & sErrDesc
  end if
  wscript.echo sErrText & " - E: " & nErrNumber & " : " & sErrDesc
  wscript.quit 1
end sub