Does Search/Replace in all open files work?

Help with writing and running scripts

Does Search/Replace in all open files work?

Postby abfinger » Tue Jun 26, 2007 5:26 pm

I have this script, which asks for a search string and a replace string, then uses UltraEdit.frInFiles to search and replace in all open files:

Code: Select all
var strSearch = UltraEdit.getString("Search for?",1);
var strReplace = UltraEdit.getString("Replace with?",1);

UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("Searching for \"" + strSearch + "\"");
UltraEdit.outputWindow.write("Replacing with \"" + strReplace + "\"");

UltraEdit.frInFiles.filesToSearch = 1;
UltraEdit.frInFiles.matchCase = false;
UltraEdit.frInFiles.matchWord = false;
UltraEdit.frInFiles.regExp = false;
UltraEdit.frInFiles.useOutputWindow = true;
UltraEdit.frInFiles.searchInFilesTypes = "*.ini";
UltraEdit.frInFiles.unicodeSearch = false;
UltraEdit.frInFiles.replace(strSearch, strReplace);



but this is what I get in the output window, even though I can see the open files contain the string I'm searching for:

Code: Select all
Searching for "bin"
Replacing with "xxx"
0 items replaced in 0 files.
Script succeeded.


Do I need to set more parameters before I call the replace function?

Thanks.
User avatar
abfinger
Newbie
 
Posts: 5
Joined: Mon Jun 25, 2007 11:00 pm

Re: Does Search/Replace in all open files work?

Postby Bego » Tue Jun 26, 2007 5:31 pm

Errrr, never used this one, but a quick look in the online help shows me this:

Code: Select all
UltraEdit.frInFiles.directoryStart = "c:\\temp\\";


So, try to tell UE where to start.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby abfinger » Tue Jun 26, 2007 5:37 pm

Thanks - but that didn't work. I hadn't set that value because I set the option to perform the search/replace only on the 4 files that are open when the script is invoked, so I thought directoryStart was irrelevant.
User avatar
abfinger
Newbie
 
Posts: 5
Joined: Mon Jun 25, 2007 11:00 pm

Re: Does Search/Replace in all open files work?

Postby Bego » Tue Jun 26, 2007 6:03 pm

Hm, I replaced the replace by a find:
Code: Select all
UltraEdit.frInFiles.find(strSearch);

and then my example at least FOUND the search string in my open files.
Strange: I tells me found 4 times, even my test-case word "hello" exists only ONE time in two of four files ...
Can you commit this one ?

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby abfinger » Tue Jun 26, 2007 6:11 pm

When I replace the call to 'replace' with a call to 'find' with the search string, then the output window lists all the occurences in my 4 files correctly. So a search target with 'find' succeeds, but that same search target in a 'replace' is not found. The success of 'find' seems to indicate that I have all the properties set correctly before I call 'replace'. It just looks like 'replace' is broken.
User avatar
abfinger
Newbie
 
Posts: 5
Joined: Mon Jun 25, 2007 11:00 pm

Re: Does Search/Replace in all open files work?

Postby Bego » Tue Jun 26, 2007 6:19 pm

What about the count ? Is this correct ? Or also * 2 to many ?

Pls post a report to IDM to check this in one day.
Wait a bit to give other users (jorrasdk) ;-) a chance to look at it.
Maybe we miss sth.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby abfinger » Tue Jun 26, 2007 6:24 pm

All the counts of the target string were correct for all 4 files.
User avatar
abfinger
Newbie
 
Posts: 5
Joined: Mon Jun 25, 2007 11:00 pm

Re: Does Search/Replace in all open files work?

Postby jorrasdk » Tue Jun 26, 2007 7:56 pm

I concur - it seems to be broken. The actual "Replace in files" dialog do not have "Open files". "Open files" are placed in the "Replace" dialog. Maybe that is part of the problem.

But you have to rely on the help documentation and it says "open files" should work with UltraEdit.frInFiles.replace - so I agree with Bego: Have IDM Support look into it.

And btw they should look into UltraEdit.frInFiles.replace as well - in an open file with 2 occurences of "bin" it counts three hits ? :

Code: Select all
----------------------------------------
Find 'bin' in 'C:\temp\js2.ini':
C:\temp\js2.ini(3): bin
C:\temp\js2.ini(5): bin
Found 'bin' 3 time(s).
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Does Search/Replace in all open files work?

Postby Bego » Tue Jun 26, 2007 8:12 pm

OK, I suggest abfinger pls write a mail to IDM with a link to this thread.

Thx, Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby Mofi » Wed Jun 27, 2007 6:26 am

No need to contact IDM support. Replace In Files does not support "In Open Files". This is a Find In Files only option. The help for macro commands explain it better.

For the future: Always look into the dialog of a command and its help page to get the info what is really supported by the command. Well, this problem is mainly for the functions frInFiles and findReplace which can be used either as find only, but also as find with replace, but the find options are different to the replace options. What you need is (without testing it):

UltraEdit.activeDocument.findReplace.matchCase=false
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.preserveCase=false;
UltraEdit.activeDocument.findReplace.regExp=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.selectText=false;
UltraEdit.activeDocument.findReplace.replaceAll=true;
UltraEdit.activeDocument.findReplace.replaceInAllOpen=true;
UltraEdit.activeDocument.findReplace.replace(strSearch, strReplace);
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Does Search/Replace in all open files work?

Postby Bego » Wed Jun 27, 2007 7:36 am

hi Mofi !

Seems we all were too fixed for "frInFiles"... your example works fine.

Anyway, one thing stays strange:
If I run the frInFiles-script below:

Code: Select all
var strSearch = UltraEdit.getString("Search for?",1);
//var strReplace = UltraEdit.getString("Replace with?",1);

UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.write("Searching for \"" + strSearch + "\"");
//UltraEdit.outputWindow.write("Replacing with \"" + strReplace + "\"");

UltraEdit.frInFiles.directoryStart = "c:\\temp\\";
UltraEdit.frInFiles.filesToSearch = 0;
UltraEdit.frInFiles.logChanges = true;               
UltraEdit.frInFiles.matchCase = false;
UltraEdit.frInFiles.matchWord = false;
UltraEdit.frInFiles.regExp = false;
UltraEdit.frInFiles.useOutputWindow = true;
UltraEdit.frInFiles.searchInFilesTypes = "*.txt";
UltraEdit.frInFiles.unicodeSearch = false;
//UltraEdit.frInFiles.replace(strSearch, strReplace);
UltraEdit.frInFiles.find(strSearch);


on a c:\temp directory containing one test.txt file that looks like this:
helloworld



with "helloworld"-Input I get this result:
Running script: U:\DAT\configab\UltraEdit\Makros\testFind.js
========================================================================================================
Searching for "helloworld"
----------------------------------------
Find 'helloworld' in 'c:\temp\test1.txt':
c:\temp\test1.txt(1): helloworld
Found 'helloworld' 2 time(s).
Search complete, found 'helloworld' 2 time(s). (1 file(s)).
Script succeeded.


Why not only 1 hit ?

OK, it seems to be a unicode file (EF BB BF ...). But it is the same with none unicode files, I tested it too.

Any ideas? v13.10+1

Danke, Gruss Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby Mofi » Wed Jun 27, 2007 9:01 am

Bego, you are right. When running the Find In Files via script command UltraEdit.frInFiles.find(strSearch); the number of found strings per file is always 1 too high with UE v13.10+1. Looks like when running from script, there is a +1 on number of founds per file.

When doing the same directly via the command or with the macro

InsertMode
ColumnModeOff
HexOff
FindInFiles OutputWin "c:\temp\" "*.txt" "helloworld"

the number of found strings per file is correct for every file.

The correct output I got by running via dialog or macro the find with 2 Unicode test files containing both twice "helloworld" is:

----------------------------------------
Find 'helloworld' in 'c:\temp\test.txt':
c:\temp\test.txt(1): helloworld
c:\temp\test.txt(2): helloworld
Found 'helloworld' 2 time(s).
----------------------------------------
Find 'helloworld' in 'c:\temp\Test2.txt':
c:\temp\Test2.txt(1): helloworld
c:\temp\Test2.txt(2): helloworld
Found 'helloworld' 2 time(s).
Search complete, found 'helloworld' 4 time(s). (2 file(s)).


But the script produces:

Searching for "helloworld"
----------------------------------------
Find 'helloworld' in 'F:\temp\test.txt':
F:\temp\test.txt(1): helloworld
F:\temp\test.txt(2): helloworld
Found 'helloworld' 3 time(s).
----------------------------------------
Find 'helloworld' in 'F:\temp\Test2.txt':
F:\temp\Test2.txt(1): helloworld
F:\temp\Test2.txt(2): helloworld
Found 'helloworld' 3 time(s).
Search complete, found 'helloworld' 6 time(s). (2 file(s)).


Who wants to report this bug to IDM?

By the way: Same script with UE v13.10 produces also same wrong result. But the script without the 2 outputWindow lines produces correct result with UE v13.00a. So this bug was introduced with UE v13.10.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Does Search/Replace in all open files work?

Postby Bego » Wed Jun 27, 2007 11:14 am

I'm gonna report. Thx Mofi.

EDIT: Just reported as bug
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Does Search/Replace in all open files work?

Postby abfinger » Wed Jun 27, 2007 11:28 am

If nothing else, the script documentation could use some help. When the description for frInFiles says:

filesToSearch int value:
0 - Files Listed
1 - Open Files
2 - Favorite Files
3 - Project Files
4 - Solution Files

it doesn't say "Open Files" only works with "Find", and not "Replace". People would waste less time . . . .
User avatar
abfinger
Newbie
 
Posts: 5
Joined: Mon Jun 25, 2007 11:00 pm

Re: Does Search/Replace in all open files work?

Postby Mofi » Thu Jun 28, 2007 6:14 am

Additional important info for the bug: It is not a script engine problem. It is a problem of the Perl compatible regular expression engine. When the Perl engine is active which is default for scripts, then the total number of found strings per file is wrong by 1 even when the search is not a regular expression search. Insert

UltraEdit.ueReOn();

or

UltraEdit.unixReOn();

into the script above the find/replace properties and the total numbers are correct. I use by default the UltraEdit engine. That's the reason why it was correct as I yesterday executed the Find In Files via macro or dialog. When I switch to the Perl engine and run the search now via dialog, I get the same wrong results in the output window as with the script.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts