#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Outfile=..\Compiled Scripts\MRR_Cleanup.exe #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include #include #include #include #Include #include #include #include Dim $Version = "v2017.03.24.02" $UserPreferenceFile = @UserProfileDir & "\" & @ScriptName & ".UserPreferences.txt" Dim $ReturnArray[0] $ReturnArray = getUserPreferenceValue("DIRECTORY") $DirectoryToLookIn = $ReturnArray[0] $LineinUserPreferenceFile = $ReturnArray[1] $ReturnArray = 0 If $DirectoryToLookIn = "" Then $DirectoryToLookIn = @UserProfileDir $FolderToWalkThrough = FileSelectFolder("Select the path you want this script to walk through",$DirectoryToLookIn) If $FolderToWalkThrough = "" Then Exit setUserPreferenceValue("DIRECTORY",$FolderToWalkThrough,$LineinUserPreferenceFile) $StartTime = TimerInit() $FileArray = _FileListToArray($FolderToWalkThrough,'*',1,True) Dim $ProblemsArray[1] Dim $EndOfScriptMessage = "This script normalized the following fields for the designated segments below:" & @CRLF & @CRLF Dim $DefectMessage = "The following is a list of defects that were also accounted for through data normalization. Unless otherwise specified, they were supposed to have been fixed on the 2/10 deployment:" & @CRLF & @CRLF Dim $CI_NormalizeArray = StringSplit("2,5,7,21,22,28,32,33,34,35,36,37,38,39,40,53",",",2) Dim $CI_RemoveXtraSpacesArray = StringSplit("8,9,15,17,25",",",2) Dim $CI_TrimArray = StringSplit("11->1,15->1,17->15,19->2,25->18,27->1",",",2) $EndOfScriptMessage = $EndOfScriptMessage & "CI" & @CRLF & @TAB & _ArrayToString($CI_NormalizeArray,",") $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & @TAB & "10 & 26" & @TAB & @TAB & "Replace periods and commas with a single space" & @CRLF & _ @CRLF & @TAB & "9 & 25" & @TAB & @TAB & "Periods, commas and apostrophes exist in GCS and not in VTS" & _ @CRLF & @TAB & "Removed double spaces from:" & @TAB & _ArrayToString($CI_RemoveXtraSpacesArray,",") & _ @CRLF & @TAB & "Trimmed Segments:" & @TAB & @TAB & _ArrayToString($CI_TrimArray,",") Dim $FI_NormalizeArray = StringSplit("2,3,4,7,8,10,11,14,16,17,18,19,20,23,24,25,26",",",2) Dim $FI_DefectArray = StringSplit("13,17",",",2) $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "FI" & @CRLF & @TAB & _ArrayToString($FI_NormalizeArray,",") $DefectMessage = $DefectMessage & @CRLF & @CRLF & "FI" & @CRLF & @TAB & _ArrayToString($FI_DefectArray,",") $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "MH" & @CRLF & @TAB & "Entire line of data has been normalized" $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "MT" & @CRLF & @TAB & "2" Dim $PS_NormalizeArray = StringSplit("2,3,4,9,10,11,12,15,23,24,25",",",2) Dim $PS_DefectArray = StringSplit("7,13,14,20,22",",",2) $DefectMessage = $DefectMessage & "PS" & @CRLF & @TAB & _ArrayToString($PS_DefectArray,",") $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "SI" & @CRLF & @TAB & _ArrayToString($PS_NormalizeArray,",") Dim $SI_NormalizeArray = StringSplit("4,8",",",2) $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "SI" & @CRLF & @TAB & _ArrayToString($SI_NormalizeArray,",") Dim $WS_NormalizeArray = StringSplit("2,3,4,5,6",",",2) $DefectMessage = $DefectMessage & @CRLF & @CRLF & "WS" & @CRLF & @TAB & "7" $DefectMessage = $DefectMessage & @CRLF & @TAB & "7" & @TAB & "Does not match. Obtain list of files for investigation" $EndOfScriptMessage = $EndOfScriptMessage & @CRLF & @CRLF & "WS" & @CRLF & @TAB & _ArrayToString($WS_NormalizeArray,",") Dim $NormalizeDefects = MsgBox(35,"There Be Defects Laddy","Do you want to normalize the defects? " & @CRLF & @CRLF & _ $DefectMessage) If $NormalizeDefects = 6 Then $NormalizeDefects = True ElseIf $NormalizeDefects = 2 Then Exit Else $NormalizeDefects = False EndIf SplashTextOn("Walking Through Files","",250,50, 500, -1, 16, $DLG_TEXTLEFT,"") For $Count = 1 to $FileArray[0] ControlSetText("Walking Through Files","","Static1","File " & $Count & " of " & $FileArray[0]) Local $FileInList = $FileArray[$Count] Local $FileContentArray _FileReadToArray($FileInList, $FileContentArray,1) Local $TotalLinesInFile = $FileContentArray[0] Local $FileLineNumber = 0 ; Open the file by erasing the contents and preparing to write each line out again For $i = 1 to $TotalLinesInFile $FileLineNumber = $FileLineNumber + 1 Local $NewLine = $FileContentArray[$i] Local $NewLineString = $NewLine Local $BeginningOfLine = getSegmentValueFromString($NewLine,1) Switch $BeginningOfLine Case "CE" If $NormalizeDefects Then $NewLineString = "CE|ENTIRE LINE OF NORMALIZED DATA" EndIf Case "CI" For $ElementValue In $CI_NormalizeArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"CI " & $ElementValue) Next For $ElementValue In $CI_RemoveXtraSpacesArray Local $Segment = getSegmentValueFromString($NewLineString,$ElementValue) $Segment = StringStripWS($Segment,4) $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,$Segment) Next Local $Segment9 = getSegmentValueFromString($NewLineString,9) $Segment9 = removeSpecialCharactersFromString($Segment9) $NewLineString = replaceSegmentValueInString($NewLineString,9,$Segment9) Local $Segment10 = getSegmentValueFromString($NewLineString,10) $Segment10 = StringRegExpReplace($Segment10,"[\.|\,|\)|\(]"," ") $Segment10 = StringRegExpReplace($Segment10,'"'," ") $Segment10 = StringStripWS($Segment10,6) $NewLineString = replaceSegmentValueInString($NewLineString,10,$Segment10) Local $Segment11 = getSegmentValueFromString($NewLineString,11) $Segment11 = StringRegExpReplace($Segment11,'"',"") $NewLineString = replaceSegmentValueInString($NewLineString,11,$Segment11) Local $Segment25 = getSegmentValueFromString($NewLineString,25) $Segment25 = removeSpecialCharactersFromString($Segment25) $NewLineString = trimSegmentInString($NewLineString,25,$Segment25) Local $Segment26 = getSegmentValueFromString($NewLineString,26) $Segment26 = StringRegExpReplace($Segment26,"[\.|\,|\(|\)]"," ") $Segment26 = StringRegExpReplace($Segment26,'"'," ") $Segment26 = StringStripWS($Segment26,6) ; Note that the replacement of the double space is done intentionally prior to the removal of the double quote $NewLineString = replaceSegmentValueInString($NewLineString,26,$Segment26) For $ElementValue In $CI_TrimArray Local $FromToArray = StringSplit($ElementValue,"->",3) $NewLineString = trimSegmentInString($NewLineString,$FromToArray[0],$FromToArray[1]) Next Case "FI" For $ElementValue In $FI_NormalizeArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"FI " & $ElementValue) Next If $NormalizeDefects Then For $ElementValue In $FI_DefectArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"DEFECT - FI " & $ElementValue) Next EndIf Case "MH" $NewLineString = "MH|ENTIRE LINE OF NORMALIZED DATA" Case "MT" $NewLineString = replaceSegmentValueInString($NewLineString,2,"MT 2") Case "PS" ; Get value of PS15. If it contains "Unspecific Error" in the field, do NOT normalize it Local $Segment15 = getSegmentValueFromString($NewLineString,15) If not StringRegExp($NewLineString,"Unspecific Error") Then _ArrayAdd($PS_NormalizeArray,"15") EndIf For $ElementValue In $PS_NormalizeArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"PS " & $ElementValue) Next Local $Segment16 = getSegmentValueFromString($NewLineString,16) $Segment16 = StringRegExpReplace($Segment16,"[\.|\,|\\]"," ") $Segment16 = StringRegExpReplace($Segment16,"[\-|\/]","") $Segment16 = StringStripWS($Segment16,4) $NewLineString = replaceSegmentValueInString($NewLineString,16,$Segment16) If $NormalizeDefects Then For $ElementValue In $PS_DefectArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"DEFECT - PS " & $ElementValue) Next EndIf Case "SI" For $ElementValue In $SI_NormalizeArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"SI " & $ElementValue) Next Case "WS" For $ElementValue In $WS_NormalizeArray $NewLineString = replaceSegmentValueInString($NewLineString,$ElementValue,"WS " & $ElementValue) Next If $NormalizeDefects Then $NewLineString = replaceSegmentValueInString($NewLineString,7,"DEFECT - WS 7") EndIf EndSwitch $FileContentArray[$i] = $NewLineString Next Local $FileHandle = FileOpen($FileInList,2) _FileWriteFromArray($FileHandle,$FileContentArray,1) FileClose($FileHandle) Next MsgBox(0,'Normalized Fields',"Walked through all " & $Count - 1 & " files!" & @CRLF & @CRLF & $EndOfScriptMessage) Exit Func replaceSegmentValueInString($StringToAdjust,$SegmentToReplace,$ValueToReplaceWith) Local $ReturnString, $NewString $StringToAdjust = StringRegExpReplace($StringToAdjust,"\~","") Local $Array = StringSplit($StringToAdjust,"|") For $i = 1 to $Array[0] If $i = $SegmentToReplace Then $ReturnString = $ReturnString & "|" & $ValueToReplaceWith Else $ReturnString = $ReturnString & "|" & $Array[$i] EndIf Next ;Trim the asterisk from beginning of the string and place a tilde on the end $ReturnString = StringRegExpReplace($ReturnString,"^\|","") & "~" $Array = 0 Return $ReturnString EndFunc Func getSegmentValueFromString ($StringToSearch,$SegmentToGet) $StringToSearch = StringRegExpReplace($StringToSearch,"\~",'') Local $LineArray = StringSplit($StringToSearch,"|") Local $ReturnValue If $LineArray[0] < $SegmentToGet Then $ReturnValue = "" Else $ReturnValue = $LineArray[$SegmentToGet] EndIf $LineArray = 0 Return $ReturnValue EndFunc Func trimSegmentInString($StringToAddress,$SegmentNumber,$DesiredSegmentLength) Local $StringArray = StringSplit($StringToAddress,"|") Local $ReturnString If $StringArray[0] >= $SegmentNumber Then $StringToAddress = StringRegExpReplace($StringToAddress,"\~",'') Local $StringToTrim = $StringArray[$SegmentNumber] Local $CurrentSegmentLength = StringLen($StringToTrim) If $CurrentSegmentLength > $DesiredSegmentLength Then $StringToTrim = StringTrimRight($StringToTrim,$CurrentSegmentLength - $DesiredSegmentLength) $StringToTrim = StringStripWS($StringToTrim,2) Local $NewString Local $NewArray = StringSplit($StringToAddress,"|") For $i = 1 to $NewArray[0] If $i = $SegmentNumber Then $NewString = $NewString & "|" & $StringToTrim Else $NewString = $NewString & "|" & $NewArray[$i] EndIf Next ;Trim the asterisk from beginning of the string $NewString = StringRegExpReplace($NewString,"^\|","") $NewArray = 0 $ReturnString = $NewString Else $ReturnString = $StringToAddress EndIf $ReturnString = $ReturnString & "~" Else $ReturnString = $StringToAddress EndIf $StringArray = 0 Return $ReturnString EndFunc Func removeSpecialCharactersFromString($StringToStrip) Local $StrippedString = StringRegExpReplace($StringToStrip,"[\.|\,\']",'') Return $StrippedString EndFunc Func getUserPreferenceValue ($KeyName) Local $KeyValue Local $UserPreferenceArray $KeyValue = "" $KeyLineNumber = 0 Local $KeyArray[2] If FileExists ($UserPreferenceFile) Then ; Open pref file and find value _FileReadToArray($UserPreferenceFile,$UserPreferenceArray,1) Local $LineNumber = 0 For $Element in $UserPreferenceArray If StringInStr($Element, $KeyName & "|") Then $KeyValue = $Element $KeyLineNumber = $LineNumber EndIf $LineNumber = $LineNumber + 1 Next EndIf Local $KeyArray [2] = [$KeyValue,$KeyLineNumber] Return $KeyArray EndFunc Func setUserPreferenceValue ($KeyName, $KeyValue, $FileLineNumber) Local $KeyValuePair = $KeyName & "|" & $KeyValue & @CRLF If Not FileExists($UserPreferenceFile) Then _FileCreate($UserPreferenceFile) EndIf If $FileLineNumber = 0 Then FileOpen($UserPreferenceFile,1) FileWrite($UserPreferenceFile,$KeyValuePair) FileClose($UserPreferenceFile) Else _FileWriteToLine($UserPreferenceFile,$FileLineNumber,$KeyValuePair,True) EndIf EndFunc