Jump to content
Macro Express Forums

replacing a block of text in notepad/wordpad


Go to solution Solved by acantor,

Recommended Posts

Hi All.

So i'm challenged in the logic dept and more into animation.

I'm looking for a way to this:

1) I have a folder of about 100 files containing text formatted in xml. The files are for example: "Test_1.xml" to "Test_100.xml"

2) I'd like to look for the first occurrence of the tag/text <metadata> and delete all text preceding that tag.

3) Then, paste the contents of a previously copied block of xml from the clipboard (which I'll manually copy to the clipboard) into the file from the first line in the file Text.xml - So basically the clipboard text is replacing the previously deleted block in Test_1.xml.

4) Once that is done, the macro will go down to the next file in the folder and repeat steps 2 and 3 until it reaches the end of the folder. (file Test_100.xml)

 

I don't mind sending a paypal fiver or more, and would be happy if the macro itself is posted here for the community.

Kind Regards.

 

Link to post
Share on other sites
  • Solution

What you are trying to achieve is not difficult, but the task is not trivial. It's a real scripting project. I don't think you will find too many volunteers!

 

However, I can tell you some of the steps you are likely to need if you try to do it yourself:

 

Type Text Control F // Open the Find dialog

Wait 200 milliseconds

Type Text <metadata>

Type Text Enter

Type Text Escape // Close the Find dialog

Wait 200 ms

Type Text <Left arrow> // Turn off selection

Type Text Shift Control Home // Select to start

Paste Clipboard

Wait 500 ms

Type Text Control s // Save

Wait 2000 ms

Type Text Alt F4 // Close the document

Wait 3000 ms

Activate XXXX // Make sure Windows Explorer is focused

Type Text <Down arrow> // Go to next file in Windows Explorer

Link to post
Share on other sites

Thanks Alan.

You've provided me some good logic paths to follow (especially the last two lines)

Would ME quit byitself after processing the last file in a folder or would it loop through around and try to re-edit?

Oh wait... I suppose I could put this whole thing in a repeat loop for the amount of files to process : -)

 

Meanwhile, as I was experimenting, here's what I did as a stop-gap solution and of course had to run the macro manually 100 times.

The tag <metadata> comes exactly 145 lines later, so this is not the most elegant way to do it, but for now that's what I've been doing.

 

 

macro.jpg

 

I'll try and refine it using what you've provided above.

Many thanks!

Link to post
Share on other sites

If you really have only 100 files, and this is a one-time thing, then don't bother coding the macro to progress through all 100 files. Alan's model macro already arrows down in Windows Explorer to highlight the next file, so just set up a hot key to start the macro again manually. It won't take much extra time to hit the key 100 times, and it will save you significant coding effort.

 

In fact, I would be tempted to break Alan's code up into two separate macros: first (hotkey) macro to do the find and paste -- then you eyeball it to make sure it has really done what you want -- then second (hotkey) macro to save the modified file and arrow down to the next file in Explorer. I say this because, whereever you build in time delays, there is the possibility of Windows screwing things up with an unusually long response time. Better you should do a quick visual check on each file before saving it, so if something has gone wrong you can cancel out without saving and start over with that file. If you were doing 10,000 files, no, but with only 100 play it safe. :)

  • Like 1
Link to post
Share on other sites

I agree 100%. Well, almost 100%. I don't think I would automate the entire process i.e., doing every file with a single macro. Activating the script 100 times will still save gobs of times. But I would not hesitate to do it in one fell swoop instead of two steps. I agree that spliting the operation in two may save aggravation if you are new to Macro Express. There is plenty that can go wrong, as you will likely discover.

 

Building the macro step by step, and adding judicious delays, will help contain the chaos that can occur!

Link to post
Share on other sites

Good advice rberq.

I went through a reboot, because I think there were not enough delays... at one point for some reason, the macro wanted to paste text into the chrome browser, even though chrome was not open.

 

Took Alan's advice and introduced some more delays. At least I was able to go make a coffee while the PC did it's thing. A bit slow, but beats the hell out of doing things manually.

Here's the end macro:

B7jNNnq.jpg

 

There is an additional ENTER after the CTRL S, because a warning dialog was popping up when saving the file via wordpad (that file was being saved as a txt file)

 

Thanks again Alan for putting me on the right logic path.

The macro can be improved for sure, but for my needs, it'll do just fine!

Link to post
Share on other sites

I would never use a UI of an application to do this task. I would use Repeat With Folder and read each file into a variable. Then use variable modify commands to change the content and then save it back to a file. Simpler and no timing issues so it would run without a hitch.

Link to post
Share on other sites

you could repeat the entire process 100 times... but I suggest adding more delays, especially when opening and closing windows.

 

If you really have only 100 files, and this is a one-time thing, then don't bother coding the macro to progress through all 100 files. Alan's model macro already arrows down in Windows Explorer to highlight the next file, so just set up a hot key to start the macro again manually. It won't take much extra time to hit the key 100 times, and it will save you significant coding effort.

 

On a similar one-time task last week I used a repeat loop with needed delays. However, to avoid having to spend a lot of time adjusting and optimizing the delays I made them extra large and went to lunch while it ran. In this case there were over 500 items to process and the delays were 3-4 times longer than necessary but, since it was running without any supervision, reliability was more important than speed.

Link to post
Share on other sites

I would never use a UI of an application to do this task. I would use Repeat With Folder and read each file into a variable. Then use variable modify commands to change the content and then save it back to a file. Simpler and no timing issues so it would run without a hitch.

Ahh.. but variables and substring extraction etc.. it goes goes beyond my skills-set at the moment ;)

Link to post
Share on other sites

Often when you want to allow for uncertain delays, you can pick a spot on the screen that you KNOW will change when the desired action completes. Use a repeat loop (with MANY repeats) to check for change of pixel color at that spot, then exit from the loop when it changes. Include a 100ms delay within the loop, which pretty much takes care of different speed computers. For example, repeat 1000 times with 100ms delay allows a (nominal) 100 seconds for the action to complete, but the macro will continue immediately when it does complete, be it one tenth of a second or the full 100 seconds. Of course if the 100 seconds elapses and the action still has not finished, you are SOL, so you have to program for that and display a text box error message and abort the macro.

 

But for this situation Cory's method is better. Play with substrings a little and you may find it's easier than you think.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...