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

    • robcull
      By robcull
      Hello all! I have had some issues reading text from different types of windows, occasionally, specifically with controlgettext. 
      **Before I begin, I know there are better ways to do what I attempt in the example below. That's not the point of this post. The point is my issues with controlgettext. 
      I am about to cite an example with an application you may be familiar with called SpeedFan (v4.52). My problem is not specific to speedfan, it is simply the most recent and easily reproducible example I can think of. 
      So, the goal of the script below is to get a string of text containing the current fan RPMs from the highlighted control in the screenshot below (see "speedfan_control_details.png").

      Now, here's a simple script for grabbing the window handle and reading the text from that control: 
      $wintitle = "SpeedFan 4.52" $controlID = "197934" ;will be reformatted as "[ID:######]" $hwnd = wingethandle($wintitle) if @error<>0 then msgbox(0, "WinGetHandle", "FAILURE. @error="&@error) Exit EndIf $text = ControlGetText($hwnd, "", "[ID:"&$controlID&"]") if @error=1 then msgbox(0, "ControlGetText", "FAILURE. @error="&@error) ;failure returns "" and @error=1 Exit EndIf msgbox (0, "ControlGetText", "SUCCESS. @error="&@error &@CRLF& "$text="&$text) ;success returns string and @error=0 You'll see that the ControlGetText operation runs without error, however it does not capture any text from the control. If you explore the other controls in this one window, you'll find mixed results across the board. Neither the temps nor voltages can be read, while the log field and some other elements can be read. Even when you read the text from the whole window, those elements are not included in the visible nor hidden texts. 
       
      I have run into this issue many times in the past- inconsistencies in the ability of autoit to interact with certain controls. What is it which makes this text different than any other readable texts? Is there an alternate method of reading the text in the window/control which could work? Any and all info to help me solve this mystery and satisfy my curiosity would be greatly appreciated. 
      Thanks  -Rob C
      PS: Running Autoit v3.3.14.2 on Win7 Ultimate x64
    • thoms
      By thoms
      Hello Forum,
      I'm trying to vertically center text in label controls, but no way. The search on the forum returns no result, or I don't search the right way
      When I insert a button and a label of same size close to each other, the text is centered on the button, but top aligned on the label, or edit. Which doesn't look really aesthetic
      Any idea is welcome
      Thanks in advance,
      Thoms
    • nacerbaaziz
      By nacerbaaziz
      hello
      Greetings to all
      dears i have a big problem in the bass.dll
      e.g
      If you play an audio file using the bass.dll
      and  You changed the audio output
      Such as setting or removing headphones
      The sound output of bass.dll does not change
      Is there any solution
      If it exists, please give it to me
      Greetings and thanks in advance
      I apologize for my bad English
    • nacerbaaziz
      By nacerbaaziz
      Hi friends, I have a question, please.
      I have created a window containing a edit box
      I need to know how to change the keyboard language,
      e.g :
      if a user open the  script  it will be change the keyBord language   in window automatically into English
      I found more examples but didn't work with me .
      Note:  
       I use the windows 10 os
      Please help me .
      thanks in advance to all
      with my Greetings and my appreciation for all users and admins
    • Ian_Mac
      By Ian_Mac
      HotKeySet("^{SPACE}", "get_color") $colorCodeHere = ;<---------------- func get_color() Global $point = MouseGetPos() Global $color = PixelGetColor($point[0], $point[1]) MsgBox(0, "debug", "result: " & $color) EndFunc While 1 Sleep(100) WEnd Hello guys my problem is, how can i store the value that i get in that PixelGetColor($point[0], $point[0])
      i know that when i msgbox the color i will show the = of $color
      but i wanted it to be the value like  $colorCodeHere = 13456254. but  not hardcoding the value.
       
      or how could i say 
      if $color is = to $color then do this and if  $color is not  equal to $color then do that.