Jump to content

Replacing a string in a file...


Yumeii
 Share

Recommended Posts

Ok so I'm new to this whole thing and I thought that maybe getting a script to do this for me would be much faster than manually going through 1000+ files. After much searching through the forums and experimentation, I've broken down and now I'm asking for help.

Sob story aside, here's what I'm trying to do. I have a ton of .sm files which I need to edit. Basically, in each one, I'm searching for a string and replacing it with another and then saving the file. I was also hoping to be able to drag and drop files into the exe when finished so I could just mass edit them.

FileRegister("sm","C:\WINDOWS\notepad.exe","Open in notepad.exe")
$file=FileOpen($cmdline,0)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$data = FileRead($file)
FileClose($file)
$data = StringReplace($data,"aaa","bbb")
$file = FileOpen($file,2)
FileWrite($file,$data)
FileClose($file)

So before I registered the .sm extension, I kept getting the open error. After I registered it, nothing appeared when I dragged the file onto the compiled script so I can only assume I got that part working. But when I open the .sm file afterward to check if it worked, "aaa" is still "aaa." I'm guessing I screwed up somewhere along the line. Any help would be much appreciated.

Link to comment
Share on other sites

I added a quick message box to pop up displaying $data after the FileRead and also after the StringReplace to check it. It shows up the correct info after FileRead and it also has "aaa" changed to "bbb" after the StringReplace.

Link to comment
Share on other sites

Umm... what I posted above was more or less it. As I've never really done this before, I'm guessing I need to use arrays? But I have no clue on how to even begin setting that up... and forum search results just finds me things where people talk about using arrays as if you knew how to do it already. I read through the online documentation and what little I found on arrays didn't help me much aside from letting me know that's what I should probably use.

Link to comment
Share on other sites

Ah I see now :)

Try this:

$split = StringSplit($CMDLINERAW, '" "', 1)
For $i = 1 To $split[0]
;Remove the "
    $filename = StringReplace($split[$i], '"', '')
;Just to let us know whats happening and where we're at:
    MsgBox(0, "", "Parsing File Name: " & $filename)
;Read the file
    $data = FileRead($filename)
    If @error Then
        MsgBox(0, "", "Could not read original data for " & $filename)
        ContinueLoop
    EndIf
;Replace Data
    $data = StringReplace($data, "aaa", "bbb")
;Reopen file and replace contents
    $file = FileOpen($filename, 2)
    If $file = -1 Then
        MsgBox(0, "", "Could not replace data in " & $filename)
        ContinueLoop
    EndIf
    FileWrite($file, $data)
    FileClose($file)
Next
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...