r/OfficeScripts • u/SoberIRL • Mar 06 '13
[Request] Replace all instances of text "xyz" with "abc" in multiple text files across multiple directories
I decided this week to start learning Python, because I'm certain I can do this. I guess I'm looking more for guidance than I am for someone to just give me code. If this task is way beyond what a person can learn to do in, say, 4 weeks - those comments would also be appreciated.
My office has a daily task to download eight text documents from a website. The first tedious, repetitive step in this process is that we must do a bit of text replacement. Notepad has a decent 'find' and 'replace all' setup, but I imagine this can be completely automated.
Once I have a solid grasp of Python, where will I go from there? How would this code be implemented for the other people in my office? Ideally, there would be a .exe or something they'd double-click, and then they'd browse to the parent directory, and hit 'run.' In what framework will I be building that simple GUI?
I assume that, as I get deeper into understanding Python, my understanding of the related tools and so forth will come. Thanks!
5
u/AeroNotix Mar 06 '13
This can be done in with unix tools easily, very easily.
find . -name *.txt | xargs sed -i s/original/replacement/g3
u/OCHawkeye14 Mar 06 '13
This would maybe be better posted in /r/learnpython but I can tell you that some useful code snippets would be the following:
This would open a file named "file.txt", dump the contents to a variable named "contents", and close the file.
Now that we have the contents, we do stuff with those contents. Want to know how many times "xyz" shows up in those contents?
Want to replace that string with another string "abc"? That's not hard, but is a multi-step process. 1) Find where "xyz" starts, 2) find where "xyz" ends, 3) delete start->end, 4) add "abc" to the start location, 5) continue through the document and find the next occurrence of "xyz"
Now just rewrite the file.
Hopefully this helps.
2
u/throwOHOHaway Mar 06 '13
I'm assuming this post is just asking information regarding that particular task? Correct me if I'm wrong.
Once you are able to write code for it, you could turn it into an executable, for Windows, or since Python is already installed on most Linux computers, you could simply run the script on the command line.
A GUI is not necessarily in most cases, it would definitely make it a lot easier to use for those not terribly computer literate, but you could definitely get by with a simple command line tool.
Regarding learning Python; I found it to be an incredibly easy language to pick up (YMMV!), given that it is extensively documented, and has a large learning community.
Even for someone who doesn't know Python at all, I can't see anyone working on this working past two to three weeks. If this is something you'd like to work on, we'd like to help you work on it :) Check out the learners resources here
There is actually already a request put out for a script that does what you've mentioned, I will let you know if someone decides to start work on it.
3
u/badpokerface12 Mar 06 '13
Once you get the script they way you want it. Converting it to an exe that anyone can use is simple with py2exe it works wonderfully
1
u/silentarrow Mar 18 '13
wow! I am already doing this. I am creating a tool that does a search and replace for multiple files. so, I think I can do this job after a few finishing touches on this.