Aco-gt
Aco-gt
TTask
Created by Aco-gt on 8/13/2024 in #help
How to use for loop with filenames containing spaces?
Hi @andreynering , Thanks to your advice, it worked perfectly. However, the find part ended up being quite a tricky method.
sample-for-cmd:
desc: Sample how to use for cmd
vars:
PDF_PATH: "documents/pdf_files"
MY_VAR:
sh: find {{.PDF_PATH}} -type f -name *.pdf -print0 | tr '\0' '@'| head -c -1
cmds:
- for: { var: MY_VAR, split: '@' }
cmd: ls {{ shellQuote .ITEM}}
sample-for-cmd:
desc: Sample how to use for cmd
vars:
PDF_PATH: "documents/pdf_files"
MY_VAR:
sh: find {{.PDF_PATH}} -type f -name *.pdf -print0 | tr '\0' '@'| head -c -1
cmds:
- for: { var: MY_VAR, split: '@' }
cmd: ls {{ shellQuote .ITEM}}
$ ls documents/pdf_files a-sample.pdf 'b sample.pdf' 'c sample d.pdf' $ task sample-for-cmd task: [sample-for-cmd] ls 'documents/pdf_files/b sample.pdf' 'documents/pdf_files/b sample.pdf' task: [sample-for-cmd] ls documents/pdf_files/a-sample.pdf documents/pdf_files/a-sample.pdf task: [sample-for-cmd] ls 'documents/pdf_files/c sample d.pdf' 'documents/pdf_files/c sample d.pdf' Explanation find {{.PDF_PATH}} -type f -name "*.pdf" -print0: Finds all PDF files in the specified path and outputs the filenames separated by a NULL character (\0). tr '\0' '@': Translates NULL characters (\0) to the @ symbol. head -c -1: Removes the last character, which is the trailing @. This command sequence effectively lists the PDF files with @ as the delimiter and removes the unnecessary trailing @.
3 replies