Reputation: 2833
I need to replace text in a folder full of text files. How would I go about this?
Thanks!
Upvotes: 1
Views: 461
Reputation: 27603
tell application "Finder"
set l to files of desktop as alias list
end tell
repeat with f in l
set input to read f as «class utf8»
set text item delimiters to "search"
set ti to text items of input
set text item delimiters to "replace"
set b to open for access f with write permission
set eof b to 0
write (ti as text) to b as «class utf8»
close access b
end repeat
I mostly just use find in project in TextMate or Ruby scripts like this:
Dir["#{ENV['HOME']}/Desktop/*"].each do |f|
input = File.read(f).gsub(/search/, "replace")
File.open(f, "w") { |f| f.print(input) }
end
You could also use sed:
sed -i '' 's/search/replace/g' ~/Desktop/*
Upvotes: 1