ur

How to change values in space separated files

7 posts in this topic

I have a text file whose data will be as below.

win10x64 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (32-bit)_2500.exe" SilentInstall.exe
win10x64clone1 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (64-bit)_2500.exe" DM64.exe
win10x64clone2 ~\erwin Notallowed1! "erwin Mart Server r9.7 (32-bit).exe" SilentInstall.exe
win10x64clone3 ~\erwin Notallowed1! "erwin License Server r9.7 (32-bit).exe" SilentInstall.exe

Each line will have multiple values separated by space.

If a value contains space in it, the value is surrounded by quotes.

My task is to check how many values are there in each line.

If the line contains 5 values, I need to replace the 4th value with the string contained in a variable.

If it contains 4 values then also  I need to replace the 4th value followed by appending 5 th value to it as SilentInstall.exe

If the value I am replacing contains spaces then I need to surround the new value with quotes.

 

Any one can suggest how to do this,??

Share this post


Link to post
Share on other sites



One way :

#include <Array.au3>

Local $aLines = ['win10x64 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (32-bit)_2500.exe" SilentInstall.exe', _
'win10x64clone1 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (64-bit)_2500.exe" DM64.exe', _
'win10x64clone2 ~\erwin Notallowed1! "erwin Mart Server r9.7 (32-bit).exe" SilentInstall.exe', _
'win10x64clone3 ~\erwin Notallowed1! "erwin License Server r9.7 (32-bit).exe" SilentInstall.exe']
Local $aItems

For $i = 0 To UBound($aLines) - 1
    $aItems = StringRegExp($aLines[$i], '(?|"([^"]+)"|(\H+))', 3)
    _ArrayDisplay($aItems)
Next

 

Share this post


Link to post
Share on other sites
2 minutes ago, jguinch said:

One way :

#include <Array.au3>

Local $aLines = ['win10x64 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (32-bit)_2500.exe" SilentInstall.exe', _
'win10x64clone1 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (64-bit)_2500.exe" DM64.exe', _
'win10x64clone2 ~\erwin Notallowed1! "erwin Mart Server r9.7 (32-bit).exe" SilentInstall.exe', _
'win10x64clone3 ~\erwin Notallowed1! "erwin License Server r9.7 (32-bit).exe" SilentInstall.exe']
Local $aItems

For $i = 0 To UBound($aLines) - 1
    $aItems = StringRegExp($aLines[$i], '(?|"([^"]+)"|(\H+))', 3)
    _ArrayDisplay($aItems)
Next

 

But I need to replace the 4th word and rewrite back to the file from where I read the content.

If 5th argument is not available then I need to apped SilentInstall.exe at the end.

Can you suggest.

Share this post


Link to post
Share on other sites
20 hours ago, jguinch said:

You could start your answer by something like "thanks for your help", don't you think ?
My answer does not do what you want, but should help you to. Why don't you try by yourself ?

 

My aplologies, I didn't mean that.

 

Share this post


Link to post
Share on other sites

This is the code for the requirement I mentioned.

Thanks @jguinch for your help to split the string values.

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

;$sFilePath="c:\Build\Machines.txt"
;CreateMachinesFiles($sFilePath)

;Replace 4th argument exe with the name given by us..and give our helper exe,if 5th arg not there then it keep this
Func CreateMachinesFiles($exeName="erwin D .exe",$helpExeName="SilentInstall.exe",$sFilePath=@ScriptDir&"\Machines_Format.txt")

    Local $aLines
    if FileExists($sFilePath) Then
        _FileReadToArray($sFilePath, $aLines)
        _ArrayDelete($aLines, 0)
        ;_ArrayDisplay($aLines)
    Else
        msgbox(0,"Remote Execution","Machines info file not found")
        Exit
    EndIf

    #cs
    Local $aLines = ['win10x64 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (32-bit)_2500.exe" SilentInstall.exe', _
    'win10x64clone1 ~\erwin Notallowed1! "erwin Data Modeler r9.7 (64-bit)_2500.exe" DM64.exe', _
    'win10x64clone2 ~\erwin Notallowed1! "erwin Mart Server r9.7 (32-bit).exe" SilentInstall.exe', _
    'win10x64clone3 ~\erwin Notallowed1! "erwin License Server r9.7 (32-bit).exe"', _
    'win10x64clone3 ~\erwin Notallowed1! "erwin License Server r9.7 (32-bit).exe" SilentInstall.exe']
    #ce
    Local $aItems

    For $i = 0 To UBound($aLines) - 1
        $aItems = StringRegExp($aLines[$i], '(?|"([^"]+)"|(\H+))', 3)
        ;_ArrayDisplay($aItems)
        if UBound($aItems)>=4 Then
            $aItems[3] = $exeName;"erwin D .exe"
            if UBound($aItems)=4 Then
                ReDim $aItems[UBound($aItems)+1]
                $aItems[4] = $helpExeName;"erwin DE .exe"
            EndIf
        EndIf
        ;_ArrayDisplay($aItems)
        $aLines[$i] = MergeToLines($aItems)
    Next

    MergeToFile($aLines)

EndFunc

Func MergeToFile($aLines,$sFileToSave = @ScriptDir&"\Machines.txt");Merge array of Lines to file
    if FileExists($sFileToSave) Then FileDelete($sFileToSave)
    $sLines = $aLines[0]
    For $i = 1 To UBound($aLines) - 1
        $sLines = $sLines & @CRLF & $aLines[$i]
    Next
    ;MsgBox(0,"",$sLines)
    FileWrite($sFileToSave,$sLines)
EndFunc

Func MergeToLines($aItems);Merge array of words to line
    $line = $aItems[0]
    For $i = 1 To UBound($aItems) - 1
        if CheckForSpaces($aItems[$i]) Then
            $aItems[$i] = """" & $aItems[$i] & """"
        EndIf
        $line =$line & " " & $aItems[$i]
    Next
    return $line
EndFunc

Func CheckForSpaces($string);Check if spaces exist in a string
    if StringLen($string) - StringLen(StringStripWS ( $string, $STR_STRIPALL )) > 0 Then
        return True
    Else
        return False
    EndIf
EndFunc

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • natedog102
      By natedog102
      This small UDF helps you save any text to speech to a .wav file. 
      UDF:
      #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #include-once ; #INDEX# ======================================================================================================================= ; Title .........: _TTStoWav() ; AutoIt Version : 3.3.14.2 ; Language ......: English ; Author(s) .....: natedog102 ; Modifiers .....: ; Forum link ....: https://www.autoitscript.com/forum/topic/191573-tts-text-to-speech-to-wav-file-using-sapi/ ; Description ...: TTS to .wav file using SAPI ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _TTStoWav ; Description ...: TTS to .wav file using SAPI ; Syntax.........: _TTStoWav($sText, $sLocation[, $iRate = 1[, $iVolume = 100]]) ; Parameters ....: $sText - String you want converted to a .wav ; $sLocation - Save location + file name, example: "C:\folder\sample.wav" ; $iRate - TTS speak rate between -10 and 10 ; $iVolume - Volume of the TTS between 0 and 100 ; Return values .: Success - Returns one ; Failure - Returns zero ; Author ........: natedog102 ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _TTStoWav($sText, $sLocation, $iRate = 1, $iVolume = 100) $oFileStream = ObjCreate("SAPI.SpFileStream") If @error Then Return 0 EndIf $oFileStream.Format.Type = 39 $oFileStream.Open($sLocation, 3) $oSpeech = ObjCreate('SAPI.SpVoice') If @error Then Return 0 EndIf $oSpeech.AudioOutputStream = $oFileStream $oSpeech.Rate = $iRate $oSpeech.Volume = $iVolume $oSpeech.Speak($sText, 3) $oSpeech.WaitUntilDone(10000) $oFileStream.Close() Return 1 EndFunc ;==>_TTStoWav And here's example usage:
      _TTStoWav("This is a test", "C:\folder-must-exist\sample2.wav") I have never made a UDF before so I don't know if I coded it correctly or if I followed all the recommended guidelines. Apologies if I haven't.
      DOWNLOAD:
       _TTStoWav.au3
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Guys this is my gui and i need the numeric value typed in the highlighted  inputbox. if this value is greater than 1 i need to perform some operations
      \
       
      Here is the summary about that field which i got from wininfo tool

      i tried writing the code as shown below.. but the msgbox is popping up empty value..when the same code is pasted under some button action i am able to access the value present in the field..but when placed alone as written in the attached script i am unable to fetch it... 

      here i am using the valu present in the field

      can you guys tell me what's wrong??? or how can i get that value?
       
      My_GUI.au3

    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      in need the path/text present int above shown input box to be copied or retrieved into a variable .. how can i do that?
       
      Thanks
       
    • Shirdish_chakravarthi
      By Shirdish_chakravarthi
      Hello Friends i am new to autoit and i am stuck in middle of automation of flashing tool..
      my requirement is i have a window and i have to get the value present in that window (below is the picture)

      in the above window i need to get the value of the highlighted field.the value of this filed keeps on changing...ii need to get the current value present in the field..below are the details of the window and field..

       
      CAN it be done?
       
      thanks

    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
              </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
          </div>
      </div>
      Many thanks in advance
      cheers
      Roger