randeep Posted June 24, 2009 Share Posted June 24, 2009 (edited) Hello all can anyone pls suggest how to find a particular string in a text file without opening it????? thanx in advance Edited June 24, 2009 by randeep [font="Palatino Linotype"]Randeep Singh[/font][sub][/sub] Link to comment Share on other sites More sharing options...
BrettF Posted June 24, 2009 Share Posted June 24, 2009 (edited) I would say impossible. How can you know whats in the file without opening it? Maybe explain your intent and why you outcome is. Edited June 24, 2009 by BrettF 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...
randeep Posted June 24, 2009 Author Share Posted June 24, 2009 (edited) @BrettFjust like "_ReplaceStringInFile". it replaces the particular string in a text file and replaces what you provide without opening the text file.just like that is there any command to find a string in a text file.thanxI would say impossible. How can you know whats in the file without opening it? Maybe explain your intent and why you outcome is. Edited June 24, 2009 by randeep [font="Palatino Linotype"]Randeep Singh[/font][sub][/sub] Link to comment Share on other sites More sharing options...
oMBRa Posted June 24, 2009 Share Posted June 24, 2009 I would say impossible. How can you know whats in the file without opening it? Maybe explain your intent and why you outcome is.I think with "Open" he means opened with a text editor@OP look at FileOpen, FileRead, StringInstr Link to comment Share on other sites More sharing options...
randeep Posted June 24, 2009 Author Share Posted June 24, 2009 @oMBRathanx for replying but all these commands not working. I think with "Open" he means opened with a text editor@OP look at FileOpen, FileRead, StringInstr [font="Palatino Linotype"]Randeep Singh[/font][sub][/sub] Link to comment Share on other sites More sharing options...
Tvern Posted June 24, 2009 Share Posted June 24, 2009 (edited) @oMBRa thanx for replying but all these commands not working. Works for me: $File = FileOpen("test.txt",0) ;open a file for reading $String = FileRead($File) ;read the file FileClose($File) ;close the file $String = StringReplace($String,"replace this string","replacement string") ;replace what you want to replace $File = FileOpen("test.txt",2) ;open the file for writing ;FileWrite("test.txt",$String) Fixed this to use the filehandle FileWrite($File,$String) ;write the new content FileClose($File) ;close the file ;profit! Will replace each occurence of the string "replace this string", with "replacement string". Edited June 24, 2009 by Tvern Link to comment Share on other sites More sharing options...
randeep Posted June 24, 2009 Author Share Posted June 24, 2009 (edited) oooops it deleted my original file $File = FileOpen("C:\out.txt",0) ;open a file for reading $String = FileRead($File) ;read the file FileClose($File) ;close the file $String = StringReplace($String,"reply","done") ;replace what you want to replace $File = FileOpen("C:\out.txt",2) ;open the file for writing FileWrite("out.txt",$String) ;write the new content FileClose($File) ;close the file ;profit!oÝ÷ Ú«¨µéÚ Edited June 24, 2009 by randeep [font="Palatino Linotype"]Randeep Singh[/font][sub][/sub] Link to comment Share on other sites More sharing options...
BrettF Posted June 24, 2009 Share Posted June 24, 2009 That is the general idea with using flag 2 write mode (erase previous contents) with FileOpen()...You must learn to work out whats happening with the code before you decide to run it and see what it does... 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...
randeep Posted June 24, 2009 Author Share Posted June 24, 2009 @BrettFThat is the general idea with using flag 2 write mode (erase previous contents) with FileOpen()...pls see my previous postThat is the general idea with using flag 2 write mode (erase previous contents) with FileOpen()...You must learn to work out whats happening with the code before you decide to run it and see what it does... [font="Palatino Linotype"]Randeep Singh[/font][sub][/sub] Link to comment Share on other sites More sharing options...
BrettF Posted June 24, 2009 Share Posted June 24, 2009 And look at? You do realise that you are wiping it here: $File = FileOpen("C:\out.txt",2);open the file for writing FileWrite("out.txt",$String);write the new content And writing to out.txt, which is probably (?) in your script dir. Do you mean this? ;This is just for ease of use $infile = "C:\old_file.txt" $outfile = @ScriptDir & "\new_file.txt" ;Read old data $old_data = FileRead ($infile) ;Replace occurences of "one" with "two" $new_data = StringReplace ($old_data, "one", "two") ;Open the output file, erasing previous contents $hFile = FileOpen ($outfile, 2) ;Write new contents to file FileWrite ($hFile, $new_data) ;Close the handle FileClose ($hFile) 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...
Tvern Posted June 24, 2009 Share Posted June 24, 2009 $File = FileOpen("C:\out.txt",2);open the file for writing FileWrite("out.txt",$String);write the new contentThat was actually an error caused by my original example, which I fixed. But yeah the example I showed you worked and still works, but it's up to you to add errorchecking. if you change "FileWrite("out.txt",$String)" to "FileWrite($File,$String)" your example should work, given that the file "C:\out.txt" exists. 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