Jump to content

Find, modify and replace | Will AutoIt be our friend?


rrandom
 Share

Go to solution Solved by Melba23,

Recommended Posts

Hello all.

We run into problems, modifying a large amount of strings the following way:

[1]  should become  <a href="footnotes.html#1">[1]</a>

[2]  should become  <a href="footnotes.html#2">[2]</a>

...

[570] should become  <a href="footnotes.html#570">[570]</a>

Thank you a lot for any kind of help, hint or advice

rRaNDom

Link to comment
Share on other sites

What seems to be the problem?

If you are asking what I think you are asking, use a loop

For $i = 1 to 570
    ConsoleWrite('<a href="footnotes.html#' & $i & '">[' & $i & ']</a>' & @CRLF)
Next

or if its an array you are on about

Local $vArr[571]

For $i = 1 to Ubound($vArr) -1
    $vArr[$i] = '<a href="footnotes.html#' & $i & '">[' & $i & ']</a>'
    ConsoleWrite('$vArr[' & $i & '] = ' & $vArr[$i] & @CRLF)
Next
Edited by mrflibblehat

[font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font]

Link to comment
Share on other sites

Hello mrflibblehat,

Thank you for your interest and your answer.

The problem is, that the [n]s are text-parts in a very large html-document.

We want to build up a search-replace function to modify all the parts in a single action.

 

Greetings

RrANdom

Link to comment
Share on other sites

Hello mrflibblehat and Malkey,

as a sample I post some html source: (all.html)

At this time, the footnotes are just named, but they should become linked to the footnotes document (as described above).

Our idea is, that we open the doc. via an autoit.exe and let it modify the all.html in the desired way.

Thank you for your interest and help

rrANdoM

------------------------------------------

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:epub="http://www.idpf.org/2007/ops"
  xmlns:ev="http://www.w3.org/2001/xml-events">

<head>
<title>Photobook</title>
<link href="styles/style.css" rel="stylesheet" type="text/css"/>
</head>
    
<body>

<div id="all.html" xml:lang="de-DE">
<div>

<p class="bild_kap"><img class="b18" src="images/kapitelzeichen.jpg" alt="kapitelzeichen.jpg"/></p>

<h1 id="all" class="kapitel-titel">Preface</h1>

<p class="text-start">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. [1] Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. </p>

<div class="group"><p class="bild"><img class="b100" src="images/002.jpg" alt="Foto"/></p>

<p class="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. [2] Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.  </p>



......



<p class="text-start">In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.[570] Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. </p>

</div>
</div>

</body>
</html>
Edited by rrandom
Link to comment
Share on other sites

I think I see what you are trying to do. The _ReplaceStringInFile is our friend when trying to find and replace ina file with AutoIT.

Start with an input box FileOpenDialog to choose the file that needs to be modified.

Good Luck.

Bill

Edited by billo
Link to comment
Share on other sites

Thank you for all your ideas, but the needed point is missing.

Sure we have to start with an open-dialog. ;)

#include <array.au3>
#include <file.au3>
Dim $aArray
$ret = ""
$filename = FileOpenDialog ("Open...", "", "HTML Documents (*html)")

_FileReadToArray ($filename, $aArray)

$file = FileOpen ($filename, 2)

MISSING LINK ;)

FileWrite ($file, $ret)
FileClose ($file)

Any further help would be very welcome.

Best wishes

rraNdoM

Link to comment
Share on other sites

Try Playing around with this and see if you can get it to do what you want: It works as is but only as a test.

This is set up for 2 different find and replace searches, you can add more...

#include <File.au3>

Local $find = 'Photobook'
Local $replace = 'Jakofasaures'
Local $find_1 = 'styles'
Local $replace_1 = 'Deedledooo'

Local $filename = FileOpenDialog("Your File", @DesktopDir & "\", "", 1)

If @error Then
    MsgBox(4096, "", "No File(s) chosen")

EndIf


Local $retval = _ReplaceStringInFile($filename, $find, $replace)
If $retval = -1 Then
    MsgBox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error)
    Exit
Else
    MsgBox(0, "INFO", "Found " & $retval & " occurances of the pattern: " & $find & " in the file: " & $filename)
EndIf

Local $retval_1 = _ReplaceStringInFile($filename, $find_1, $replace_1)
If $retval = -1 Then
    MsgBox(0, "ERROR", "The pattern could not be replaced in file: " & $filename & " Error: " & @error)
    Exit
Else
    MsgBox(0, "INFO", "Found " & $retval_1 & " occurances of the pattern: " & $find_1 & " in the file: " & $filename)
EndIf

let me know if you have any trouble.

viel spass ;-)

Bill

Link to comment
Share on other sites

Thank you Bill,

this part is the core, is the problem:

Local $find = 
Local $replace =

As we're designers, we're neither familar working with regular expression nor able to transfer that - not given - knowledge to variables (1-570 as mentioned).

Thank you for wishing us »viel spass« but if I'm honest you should better wish us little tribulation ;[

We hoped that it's only a little problem for someone who is living with code; we don't want to go the hard way and do this job step by step.

RrAndoM

Link to comment
Share on other sites

No Problem,,

  did you try the code? It is setup to run as is "as a test".

Find = whatever you want to find

Replace = what you want to replace it with

If after running the code on your test.html you have a look at the variables or things you want changed and see that they are...

well the rest is a matter of changing the code to your liking.

Link to comment
Share on other sites

  • Moderators
  • Solution

rrandom,

Save the example HTML you posted above and then run this script on the file:

#include <Constants.au3>

$sData = FileRead("HTML.txt")

$sNewData = StringRegExpReplace($sData, '\[(\d+)\]', '<a href="footnotes.html#$1">[$1]</a>')

MsgBox($MB_SYSTEMMODAL, "New HTML", $sNewData)
When I run it, all '[**]' are replaced by '<a href="footnotes.html#**">[**]</a>'. So there is no need for any loop - every instance is replaced regardless of the number inside. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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