Jump to content

Recommended Posts

Posted

I want to create a find and replace application that traverses deepest in a folder and finds and replaces text and also creates a log of the line number and file name. Any thoughts on where to get started? I need it to replicate deployments with minor changes.

Posted

Hi pranaynanda,

 

Have a look in my sig at Snippet Browser. This may get your brain juices running in the right direction...:P

This is an application specifically for searching  for specific pieces of code in au3 files but could be easily modified for txt, ini etc.

You would need to add the change function and...

...adding a logging function would also not be a huge deal.

 

Maybe it helps maybe not...

Good luck !

Bill

Posted

@JohnOne Seen Notepad++? What I want is the feature that replaces text in all files in a folder. Along with that I want to log the what was changed and where. I might even create a list view that can be edited and works something like a dictionary.

@l3ill Logging is the hardest part for me. I had a script that could find and replace in multiple files using python. I couldn't get across to use it log the changes.

Posted

@pranaynanda,

are you aware there are many such utilities already exist? here's one i've been using.

if you need to do it on your au3 scripts, then SciTE offers "Replace in Buffers" (in Search > Replace... menu) which can replace text in all currently open au3 files, as well as SciTE Jump (pinned in the "Example Scripts" forum).

if you want to script it yourself, as a leaning practice - then i advise you do it from scratch. you'll find it a lot easier than you might think. looping files (or listing them in advance), then FileRead, StringInStr, StringReplace and FileWrite is pretty much all you need.

Signature - my forum contributions:

  Reveal hidden contents

 

Posted

@orbs I need it to change configurations of various VMs or patch deployments with little variation. I've tried fnr.exe previously. I t would have been a perfect solution had it been able to log those changes.

I'm weak at programming so I think this thread will be dormant for many weeks to come. Here's a list of things I need to do to get started and develop the logic:

  1. Select a folder using windows dialog triggered by a "Browse" button.
  2. Create an editable listview with two columns. One to find a string, the other to replace it.
  3. I'll have challenges with browsing deepest through a folder
  4. Searching for editable files. Some files may have *.config or *.deployment as their extension that also need a bulk of changes so I want to find and replace in them too.
  5. Traversing through each file in the folder and it's each subfolder to find those multiple strings.
  6. Replace should be easy using StringReplace(). I'll have trouble using it for multiple files as each file can have multiple entries as per dictionary. For example, it can be possible that I need to replace "abc" with "ijk" and "pqr" with "xyz". There can be multiple files that can have both "abc" and "ijk". I will have trouble dealing with that.
  7. Finally, I'll have trouble logging all those changes with the files that were changed. Not a hard job but yes, I'll have trouble because I cannot figure out how to do it. Maybe everytime the code replaces a string? How will I deal with line numbers?

I think those are all challenges I can think of right now. Time to script. I'll be back soon.

Posted
  On 7/18/2016 at 9:38 AM, pranaynanda said:

I've tried fnr.exe previously. I t would have been a perfect solution had it been able to log those changes.

Expand  

look here for the complete list of command line arguments of fnr.exe. did you notice  --LogFile? have you tried it?

if you intend to script the entire thing yourself, then i stand by my earlier statement - it is actually quite easy. start doing it and you'll see. _FileListToArrayRec() can get you started; _FileReadToArray() for each file will get you the files line-by-line (if line numbers are a requirement). one StringReplace() call for each dictionary entry, on each line. if really is that simple.

Signature - my forum contributions:

  Reveal hidden contents

 

Posted
  On 7/19/2016 at 7:18 AM, pranaynanda said:

The items in find and replace columns can't be edited in real time.

Expand  

that's right. the behaviour of the ListView control is such that it cannot be edited in real time. unless, of course, you search the forum for how to do it, and you find this UDF.

Signature - my forum contributions:

  Reveal hidden contents

 

Posted (edited)

Thanks @orbs.BTW, fnr didn't work as expected AT ALL. I ran a simple find and replace in a sample text file which had only "1234" written in it as a test case and I asked it to replace it with "5678" and it couldn't even find a match.

2016-07-19 14_51_29-Find and Replace.png

2016-07-19 14_53_15-1.txt - Notepad.png

2016-07-19 14_55_50-1.txt Properties.png

Edited by pranaynanda
Posted
  On 7/15/2016 at 10:48 AM, pranaynanda said:

Logging is the hardest part for me. I had a script that could find and replace in multiple files using python. I couldn't get across to use it log the changes.

Expand  

Maybe you don't need to do the logging yourself - let another program do it.

For example, use a version-control-system, such as svn, git or hg.

Check-in all your configuration-files.

Do your changes, then check-in again (and input a sensible description to label that set of changes).

Then use the version-control-system to show you a diff.

 

-HaJo

Posted (edited)

So I had some time to play and updated my Snippet Browser to do what I said above.

If you download the Snippet Browser package  and add the below code to the root file you can test and play with it.

Please be careful, the new code has NO error checking and will change strings in your files.

Best to leave it in the test root folder until you understand it and change it to your needs and add some error checking etc.

Right now it works only with .au3 files but that can be changed with this parameter: line 134

Local $FileList_A = _FileListToArrayRec(@ScriptDir, "*au3", 1, 1, 0, 2)

Main code changes are on lines 90 - 105

  Reveal hidden contents

 

Type your search text top left Input Box and hit File Search - this will find all instances of this text in every File in the Folder and sub folders and list them in the top ListView

Now pick one and type your Find text and Replace text in the left and right Input boxes in the bottom part

Now when you click Replace With all instances of the Find text will be replaced with the Replace text and a log file will be created/appended with the necessary info.

Clipboard.jpg

 

Hope this helps you with your project, let me know if you have any issues.

Bill

 

edit: the GUI edit field will not update on the fly (immediately after pushing the button) only the files themselves...

Edited by l3ill
info add
Posted
  On 7/26/2016 at 8:32 AM, pranaynanda said:

I don't want to compare versions. I only want to log the changes that have been made by find and replace.

Expand  

Um... Such a diff will show all the changes.

Or do you want the list of changes in a custom format ?

-HaJo

  • 3 weeks later...
Posted

Hi Guys!

I need assistance with this. I can't understand how this works:

#include <File.au3>
#include <Array.au3>
Local $b=0
Local $a=_FileListToArrayRec("E:\Documents\AutoIt\fnr","*",$FLTAR_FILES,$FLTAR_RECUR)
;_ArrayDisplay($a)
For $i=1 To UBound($a)-1 Step 1
;Local $b=0
_FileReadToArray($a[$i],$b)
ConsoleWrite($b)
ConsoleWrite($a)
Next

Please help. Also, I couldn't find RESH.au3 therefore, I couldn't fiddle with @l3ill's snippet browser.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...