2

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.

  • All your previous questions and their subsequent answers can be found by clicking your avatar in the top-bar. – grg Mar 29 '15 at 12:07
  • Why don't you, in Finder, simply sort the files by size and move them to another directory or delete them? – Vic Mar 29 '15 at 14:42
  • You can do this in either Applescript, Shell or Automator. But @Patrix has given a good example that writes the text to the actual files. But If you want to do it from selection in the finder (safer) then ask and I am sure his example can be adapted. – markhunte Mar 29 '15 at 14:58
  • With Finder, identifying and placing the files in a folder for processing is easy. Some files are corrupted for different reasons and need be replaced with a placeholder text file indicating why the original file has been removed. Without the placeholder, the new database will lose reference of the file and when it tries to show it, may simply produce an error for which I may get blamed. What I need help with is replacing each of the wonky files with the same text file called 'placeholder.txt' (for example). However, the original wonky file's name must be kept. – Steve K Apr 6 '15 at 4:29
1

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
  • This only works with files that have very simple names (no spaces, no line breaks etc.) -- not a problem given the specific question, but other readers should be aware of that. – Daniel Beck Mar 29 '15 at 15:43
  • oops, right. fixed, thanks for reminding me :-) – nohillside Mar 30 '15 at 6:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .