Search and Replace in a text file

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

IanC
Posts: 6
Joined: Mon Nov 30, 2020 10:04 pm

Search and Replace in a text file

Post by IanC »

OK not strictly PS related, but I'm doing it from a javascript PS script:

So what's the simplest method to search and replace in a text file?

I could do it, but in a clumsy way! I'm unclear whether I need to read the whole file, S+R, and then re-write the whole file. I'm thinking there may be a simpler method?

IanC
Posts: 6
Joined: Mon Nov 30, 2020 10:04 pm

Re: Search and Replace in a text file

Post by IanC »

OK, well this works, but won't catch errors, and there's probably a simpler method:

Code: Select all

#target photoshop

f = new File('C:/Users/Ian/Desktop/test.txt')
f.open('r')
var conts = f.read()
f.close

f.open('w')
newConts = conts.replace(/oldtext/g, 'newtext')
f.write(newConts)
f.close
But I now have a new and different problem, again to do with external local files. I'd have thought it should be extremely simple, I just want to delete a file. This doesn't work, and I really cannot work out why:

Code: Select all

f = File('C:/Users/Ian/Desktop/test.txt')
f.Delete()
I get an error "f.Delete is not a function".

User avatar
txuku
Posts: 132
Joined: Thu Jan 01, 1970 12:00 am

Re: Search and Replace in a text file

Post by txuku »

Bonsoir

Try

Code: Select all

f.remove()
?

IanC
Posts: 6
Joined: Mon Nov 30, 2020 10:04 pm

Re: Search and Replace in a text file

Post by IanC »

Thanks! I just found that solution at the same time I read your reply.

User avatar
txuku
Posts: 132
Joined: Thu Jan 01, 1970 12:00 am

Re: Search and Replace in a text file

Post by txuku »

:)