MacOS – How to replace all Finder files with a placeholder file for each

applescriptautomatorfindermacosterminal

I have lots of files from a database which are to be copied to a new database, but the files for these are corrupt (They are zero-byte in size and contain no data). Rather than the new database showing an error when the file is encountered, I want a text file to read "This file is empty".

Making the placeholder text file is easy, but how can the file be renamed for each of the potentially hundreds of files it is to replace?

The source files are numbered like 1234.txt 2345.txt 34455.txt etc

The replacement files need the same name but need all have the exact same content (message) wording I specify.

I thought AppleScript could simply record and repeat my actions of file selecting, name copying, file deleting, duplicating, moving etc but that didn't work (AppleScript didn't even notice me selecting the files).

I thought I could do it with Automator, too, but surprisingly could find no rename function that would replace the file name with text copied to the clipboard, so that didn't work either.

I know Terminal can do this in a flash and have asked a similar question like this about a year ago elsewhere but can't find any record of the answer now.

I am using Mac OS X 10.7.5. Any help using any method is appreciated.

Best Answer

Basically you want to find a bunch of files which are empty (have size 0 bytes) and then change their content to "This file is empty".

cd to/wherever/the/files/are
find . -type f -size 0 -print | while IFS= read -r line; do
    echo "This file is empty" > "$line"
done

If instead you want to remove them, run

cd to/wherever/the/files/are
find . -type f -size 0 -delete