Sign in to follow this  
Followers 0
vickerps

can anyone help me tidy this code up

8 posts in this topic

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")

Share this post


Link to post
Share on other sites



I don't know what is C:\system1.dfn. I test another file and the result is ok.

Is C:\system1.dfn exist in your driver? I don't have this file. You can have a debug to know if read it successful

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

this-is-me, the long-ass number that is being replaced is actually two numbers. The first one is CRD*34... and the second is CRD*37...

Share this post


Link to post
Share on other sites

Sorry, didn't look that close.


Who else would I be?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thanks Mac @MCPS

The code looks good to me. Wish i thought of it but i guess thats why im a beginer.

Edited by vickerps

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
Sign in to follow this  
Followers 0