Jump to content

can anyone help me tidy this code up


Recommended Posts

Can anyone help me with this code I have to do a stringreplace on two lines in a text file. No matter what i try i cannot get it to work unless i duplicate the code and do the two diff stringreplace in each. I hope this make sense

#include <file.au3>

dim $temp

; ----------------------------------------------------------------------------
; Script Start
; ----------------------------------------------------------------------------
_FileReadToArray( "C:\system.dfn", $temp )  

For $n = 1 to $temp[0]

    $temp[$n]=  StringReplace($temp[$n],"CRD*34*34*10*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "CRD*34*34*00*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00")
            FileWrite("tempfilename.txt",$temp[$n])
                
    IF $n < $temp[0] THEN FileWrite("tempfilename.txt",@lf)

Next

FileCopy("tempfilename.txt","c:\system1.dfn",1)
FileDelete("tempfilename.txt")



_FileReadToArray( "C:\system1.dfn", $temp ) 

For $n = 1 to $temp[0]

    $temp[$n]=  StringReplace($temp[$n],"CRD*37*37*10*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "CRD*37*37*00*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00")
            FileWrite("tempfilename.txt",$temp[$n])
                
    IF $n < $temp[0] THEN FileWrite("tempfilename.txt",@lf)

Next

FileCopy("tempfilename.txt","c:\system1.dfn",1)
FileDelete("tempfilename.txt")
Link to comment
Share on other sites

Sorry didn't explain properly

The code i poseted does works. However it kinda messy (duplication) I was just wondering if anyone can help me clear it up.

The file isn't important. it can be any old file it just becomes a bit more complicated if you have to do more than 2 or more stringReplace on a file.

Link to comment
Share on other sites

dostuff(0)
dostuff(1)

func dostuff($num)
if $num = 0 then $num = ""
_FileReadToArray( "C:\system" & $num & ".dfn", $temp )    

For $n = 1 to $temp[0]

    $temp[$n]=     StringReplace($temp[$n],"CRD*37*37*10*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "CRD*37*37*00*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00")
            FileWrite("tempfilename.txt",$temp[$n])
                
    IF $n < $temp[0] THEN FileWrite("tempfilename.txt",@lf)

Next

FileCopy("tempfilename.txt","c:\system" & $num & ".dfn",1)
FileDelete("tempfilename.txt")
endfunc

Who else would I be?
Link to comment
Share on other sites

I don't quite understand the general purpose of this function but I can suggest the following:

#include <file.au3>

    ReplaceTextInFile($temp[$n],"CRD*34*34*10*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "CRD*34*34*00*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "c:\system0.cfn" )

    ReplaceTextInFile($temp[$n],"CRD*37*37*10*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "CRD*37*37*00*0*15*15*0*0*0*0*00*18*00*00*00*00*22*01*00*18*00*00*00*00", "c:\system1.cfn" )

Func ReplaceTextInFile( $InputFile, $OrigString, $ChangeString, $OutputFile )

   Local $temp, $n

   _FileReadToArray( $InputFile, $temp )    

   For $n = 1 to $temp[0]

      $temp[$n]= StringReplace( $temp[$n], $OrigString, $ChangeString )
        
      FileWrite( "tempfilename.txt", $temp[$n] )
                
      If $n < $temp[0] THEN FileWrite( "tempfilename.txt", @LF )

   Next

   FileCopy( "tempfilename.txt", $OutputFile, 1 )
   FileDelete( "tempfilename.txt" )

EndFunc

I haven't tested it but the logic is there so if there are any bugs you shouldn't need to dig very far. Hope this helps.

*** Matt @ MPCS

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...