Jump to content
nanobox1471

Re-Name Duplicate String in File

Recommended Posts

nanobox1471

What I'm  trying to do is rename a duplicate string in a file to make it unique. using _ReplaceStringInFile I am able to rename the first occurrence or all occurrences. Any suggestions how to alter my code to rename following occurrences until all instances have been changed.

$file = @ScriptDir & "\test.ini"
Global $sharename =  IniReadSection ($file,"PrintList")

For $x = 1 to UBound ($sharename) -1
    If String ($sharename[$x][0]) = "ShareName" Then
        $str_replace = _ReplaceStringInFile ($file,"ShareName","ShareName0" &+ 1 ,1,0)
        If $str_replace = 1 Then
            ExitLoop
        EndIf
    EndIf
Next

ShellExecute ($file) ; to Verify Change to Document

 

test.ini

Share this post


Link to post
Share on other sites
nanobox1471

NVM Figured it out... couldn't Use ShareName as both search string and replace string.

Modified:

$str_replace = _ReplaceStringInFile ($file,"ShareName","ShareName0" &+ 1

To:

$str_replace = _ReplaceStringInFile ($file,"ShareName","PrintName0" & $x ,1,0)

and all values were modified appropriately.

Share this post


Link to post
Share on other sites
Malkey

This example appears to work.

#include <File.au3>

Local $iDup = 0
$file = "test.ini"
Global $sharename = IniReadSection($file, "PrintList")

For $x = 1 To UBound($sharename) - 1
    If String($sharename[$x][0]) = "ShareName" Then
        $iDup += 1
        $sharename[$x][0] &= StringRight("00" & $iDup, 2)
    EndIf
Next
IniWriteSection($file, "PrintList", $sharename)

ShellExecute($file) ; to Verify Change to Document"

 

  • Like 1

Share this post


Link to post
Share on other sites
nanobox1471

Works like a charm... Thank You

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

×