Jump to content
Sign in to follow this  
Yumeii

Replacing a string in a file...

Recommended Posts

Yumeii

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.

Share this post


Link to post
Share on other sites
Authenticity

Does FileRead successfully read the file content?

Share this post


Link to post
Share on other sites
Yumeii

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.

Share this post


Link to post
Share on other sites
Yumeii

Oh... ha ha ha... stupid me. I didn't notice I put:

$file = FileOpen($file,2)

It was trying to open the wrong thing the 2nd time for writing. Problem solved. I'm an idiot. Thanks for everyone's help.

Share this post


Link to post
Share on other sites
Yumeii

Arg... ok so this thing works for single files if I drag and drop them... but it doesn't do anything if I drag/drop multiple files on it. How do I get it to work with multiples?

Share this post


Link to post
Share on other sites
BrettF
Yumeii

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.

Share this post


Link to post
Share on other sites
BrettF
Yumeii

Dragging the .sm files and dropping them onto the .exe when I compile the script.

Share this post


Link to post
Share on other sites
Authenticity

Actually, you'll need to preform the same thing you did but $CmdLine[0] times so it's For $i = 1 To $CmdLine[0]...Next

Share this post


Link to post
Share on other sites
BrettF

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

Share this post


Link to post
Share on other sites
Yumeii

Yay, it works! Thanks again for all your help, everyone!

Share this post


Link to post
Share on other sites
BrettF

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  

×