I need to batch convert multiple docx files to txt files and place them in a custom directory using textutil.
It seems I can do this with a single file, but not with multiple files. If I set an -output
path while converting multiple files, it will only convert the first file.
I realise I could just move the files after the conversion, but it would save time in the future if this were possible.
textutil -convert txt /Users/evanjohn/Desktop/docs/original/*.docx -output /Users/evanjohnmeredith-davies/Desktop/docs/converted/*.txt
Before textutil
gets to do anything, the shell expands the globs; because converted/*.txt
doesn't match anything, it'll expand to just itself1, creating a file called *.txt
, which isn't what you want.
Since there doesn't seem to be an option to specifiy multiple output filenames, you probably have to loop over the files one by one:
for fpath in /Users/evanjohn/Desktop/docs/original/*.docx; do
fname=${fpath##*/}
textutil -convert txt "$fpath" -output "${fpath%/*}/converted/${fname%.docx}.txt"
done
This extracts the filename first and then uses parameter expansion to get the desired paths.
1Or, if the nullglob
shell option is set, to the empty string.