Jump to content

find string


Recommended Posts

Hello all

can anyone pls suggest how to find a particular string in a text file without opening it?????

thanx in advance

Edited by randeep

[font="Palatino Linotype"]Randeep Singh[/font][sub][/sub]

Link to comment
Share on other sites

I would say impossible. How can you know whats in the file without opening it? :D

Maybe explain your intent and why you outcome is.

Edited by BrettF
Link to comment
Share on other sites

@BrettF

just 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.

thanx

I would say impossible. How can you know whats in the file without opening it? :D

Maybe explain your intent and why you outcome is.

Edited by randeep

[font="Palatino Linotype"]Randeep Singh[/font][sub][/sub]

Link to comment
Share on other sites

I would say impossible. How can you know whats in the file without opening it? :D

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

@oMBRa

thanx for replying but all these commands not working. :D

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

@oMBRa

thanx for replying but all these commands not working. :D

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 by Tvern
Link to comment
Share on other sites

oooops :D

it deleted my original file :D

$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 by randeep

[font="Palatino Linotype"]Randeep Singh[/font][sub][/sub]

Link to comment
Share on other sites

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... :D

Link to comment
Share on other sites

@BrettF

That is the general idea with using flag 2 write mode (erase previous contents) with FileOpen()...

pls see my previous post

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... :D

[font="Palatino Linotype"]Randeep Singh[/font][sub][/sub]

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

$File = FileOpen("C:\out.txt",2);open the file for writing
FileWrite("out.txt",$String);write the new content
That 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

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...