Yumeii Posted January 31, 2009 Share Posted January 31, 2009 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 More sharing options...
Authenticity Posted January 31, 2009 Share Posted January 31, 2009 Does FileRead successfully read the file content? Link to comment Share on other sites More sharing options...
Yumeii Posted January 31, 2009 Author Share Posted January 31, 2009 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 More sharing options...
James Posted January 31, 2009 Share Posted January 31, 2009 ReplaceInFile() and there is also a built in function, go see Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Yumeii Posted January 31, 2009 Author Share Posted January 31, 2009 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. Link to comment Share on other sites More sharing options...
Yumeii Posted February 1, 2009 Author Share Posted February 1, 2009 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? Link to comment Share on other sites More sharing options...
BrettF Posted February 1, 2009 Share Posted February 1, 2009 Post your full code... Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Yumeii Posted February 1, 2009 Author Share Posted February 1, 2009 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 More sharing options...
BrettF Posted February 1, 2009 Share Posted February 1, 2009 You say drag and drop, where do you drag and drop them? Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Yumeii Posted February 1, 2009 Author Share Posted February 1, 2009 Dragging the .sm files and dropping them onto the .exe when I compile the script. Link to comment Share on other sites More sharing options...
Authenticity Posted February 1, 2009 Share Posted February 1, 2009 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 Link to comment Share on other sites More sharing options...
BrettF Posted February 1, 2009 Share Posted February 1, 2009 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 Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Share Posted February 1, 2009 I showed you how to "ReplaceInFile()". Now all you need to do is look in the help file for drag and drop on to GUI. Then loop through all items with the function. Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Yumeii Posted February 1, 2009 Author Share Posted February 1, 2009 Yay, it works! Thanks again for all your help, everyone! Link to comment Share on other sites More sharing options...
James Posted February 1, 2009 Share Posted February 1, 2009 What worked? Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
BrettF Posted February 1, 2009 Share Posted February 1, 2009 My post... Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now