Jump to content

Recommended Posts

Posted

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,??

Posted

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

 

Posted
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.

Posted
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.

 

Posted

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

 

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
×
×
  • Create New...