Wednesday, 11 May 2016

Replace Content of a Batch File

#include <Array.au3>
#include <File.au3>

$ParentPath=<FilePath>

Local $aFileList = _FileListToArray($ParentPath, "*")
  If @error = 1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
    Exit
EndIf
If @error = 4 Then
    MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
    Exit
  EndIf
$count1=0
  For $i= 1 to $aFileList[0]
  if StringInStr($aFileList[$i],"_") and not StringInStr($aFileList[$i],"APAC")  and not StringInStr($aFileList[$i],"IGL") and not StringInStr($aFileList[$i],"ILP")   then

  $SmokeFolder= $ParentPath & "\" & $aFileList[$i]
  Local $bFileList = _FileListToArray($SmokeFolder, "*.bat")

  ConsoleWrite($aFileList[$i] & " >> " & $bFileList[1] & @CRLF)

  $ChangedLine =  '\\file4\develop\wmc_apps\QMC-Automation\Global_Test\Kill.vbs UFT.exe' & @CRLF
  $ChangedLine =  $ChangedLine & '<neededChange>' & @CRLF
  $ChangedLine =  $ChangedLine & '<neededChange>' & @CRLF
  $ChangedLine =  $ChangedLine &'<neededChange>' & @CRLF

  $file = FileOpen ($SmokeFolder & "\" & $bFileList[1], 0 )
  Dim $Contents
  While 1
     $Line = FileReadLine ( $file )
     If @error Then
     Exitloop
     MsgBox "Error occured"
     EndIf
    
     $Contents = $ChangedLine  & $Line & @CRLF
  WEnd
  FileClose ( $file )
  $file = FileOpen ($SmokeFolder & "\" & $bFileList[1], 2 )
  FileWrite ( $file, $Contents )
  FileClose ( $file )

  $count1=$count1+1
  EndIf
  Next
ConsoleWrite($count1)