Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can I append wildcards to a variable?

Making a little automator app that takes input from a dragged file and I want to then copy other files in a standard directory structure related to the original file.


The problem here is the *_denoised.*


I'm trying to copy any file named *_denoised.* into another folder, but just tacking on the wildcard string to the variable is obviously not the way to do this.


for f in "$@"


do


DNOIS=`printf "$f" | cut -d'/' -f-6`/Media/Plates/*_denoised.*

# *** the cut works perfectly, just not the wildcard extension


mkdir -p /Volumes/TMMC3/Staging/To\ Remote\ Tracking/$SHTNM/{Media,Project\ Files}


cp $DNOIS /Volumes/TMMC3/Staging/To\ Remote\ Tracking/Media

# *** this cp fails due to the bad file path


Any help would be greatly appreciated.

Posted on Jan 7, 2023 11:47 AM

Reply
5 replies

Jan 7, 2023 3:48 PM in response to Mattmerk

Mattmerk wrote:

Let me make it simpler:

How can I append a string to another string so they can be interpreted as a file path in zsh?


Here is an example of two ways to create strings from other strings:

#!/bin/zsh
string1="$HOME"
string2="Music"
ls "$string1/$string2"
string3=$string1
string3+="/"
string3+=$string2
ls "$string3"


There’s nothing particularly special about paths in this context, other than that the shell has some string-handling features (zsh calls these modifiers) to make parsing those paths easier.


Here’s a writeup on string handling in zsh, including some of : modifier stuff that makes path handling easier.

https://flyingmb.com/2022/07/07/zsh-development-guide-part-2-common-operations-for-string-processing/


The canonical source of zsh documentation:

https://zsh.sourceforge.io/Doc/Release/zsh_toc.html


Apple has a nice shell scripting primer available, though it’s older (bash-ish) and hasn’t seen active updates.

Shell Script Basics




Jan 7, 2023 1:34 PM in response to Mattmerk

Use find -iname “*.whatever” to locate the files (BTW: double vertical quotes are usually best around paths and patterns), and with xargs on the same find command to execute an mv move.


This approach is strictly using bash or zsh scripting, with no Automator involvement. Or the script is invoked from an Automator app, if that’s needed.


https://stackoverflow.com/questions/13899746/use-xargs-to-mv-a-directory-from-find-results-into-another-directory#13899794

Jan 7, 2023 2:00 PM in response to MrHoffman

I contemplated including the output of my script as a text file to show my results. Looks like I should have done that to avoid any confusion.


Here's a bit from the text file:


DNOISPTH variable:

/Volumes/TMMC4/Automator Sandbox/Client - SHW101/shw101_015_010/Media/Plates/*_denoised.*

(That's the variable I want to contain the full path to the denoised Quicktime movie)


I want to make the DNOSPTH variable, that path, followed by whatever is in that path that matches the wildcards, not the literal string "*_denoised.*"


The string I want to append is the quicktime movie at the end of this path:

/Volumes/TMMC4/Automator\ Sandbox/Client\ -\ SHW101/shw101_015_010/Media/Plates/shw101_015_010_plate-main_001_denoised.mov


In my case I will always be looking for Quicktime movies, so I could have made the string *_denoised.mov, but you get my drift.


I will also be rewriting all the paths when I finalize this script, so I am using cut -d'/' -f-6 to get me exactly what I need regardless of where the actual file is located.


So my question, more simply stated, is how do I add the actual file name that matches the wildcard pattern ...


*_denoised.* (which in this case is "shw101_015_010_plate-main_001_denoised.mov")


... to the string /Volumes/TMMC4/Automator\ Sandbox/Client\ -\ SHW101/shw101_015_010/Media/Plates/


to make


/Volumes/TMMC4/Automator\ Sandbox/Client\ -\ SHW101/shw101_015_010/Media/Plates/shw101_015_010_plate-main_001_denoised.mov


?


Jan 7, 2023 2:25 PM in response to Mattmerk

Post the intended “from” paths and the intended “to” paths, please.


Or we can go the other way, and discuss globbing and wildcards.


I’d also suggest simplifying the example for the purposes of discussion, as your /this/that/the/other/thing/more/something_somethingelse.thisandthat path is just adding more text to sift through, to what is already a whole lot of text.


For example: ~/tmp/this.that


But as it seems Automator is a hard requirement here (and FWIW, xargs inserts the matching filename into the command or script to be executed, sans wildcards), I’ll bow out of the discussion.

How can I append wildcards to a variable?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.