Jump to content

Help making a file action to copy contents of folder


Recommended Posts

I want to make a file action that will take a directory as input, then copy all the files from it and give user ability to search for what directory he wants to insert copied files into.


Similar to 'Copy to...' command but instead of the folder copied, it will copy the files.


Here is the progress I have made so far.


I copied the contents of the passed in directory into 'files' array. Then I was thinking I could perhaps run a file filter for folders and and do 'mv $files $1' after but that doesn't seem to work.


The Alfred help on file filters doesn't really show how to do nested operations like this.


Thank you for any help.

Link to comment

I haven’t looked deep into the Workflow, but with your current approach you’re in for a really bad time down the line.


Always quote your variables. Never mv $file $1 unless you know how that can break and you want to take advantage of it. Use mv "${file}" "${1}" instead.


More importantly, file names can have a truckload of special characters than can be interpreted by the shell. An apostrophe in a file name can send everything to hell, if you’re not careful. Instead, take advantage of the combination of find and xargs (check their man pages). find has -print0 that can output its results separated by an ASCII NUL character. On the other end xargs has -0 that takes such output. If you check their manual pages, you’ll even see both these options exist precisely to be used with each other. They were made for these cases.


While you’re at it, check the documentation of find to do what you want. Instead of getting the contents of a directory as a multitude of strings, get the path to the directory itself and pass just that. Then tell find to get everything inside that directory not recursively.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...