Folders on the Desktop with Files Inside Them

please
i have folders in the desktop with files inside them
please i need macro that collect all of those files
that are inside all of those folders
and drop all of those files into the desktop
( the number of folder, and the names of the folder in the desktop , and the number of files inside the folders will change every time)

I don’t know if Keyboard Maestro can do this (probably, but I don’t know how)

You can do this with a simple shell command in Terminal, however:

find ~/Desktop -mindepth 2 -type f -exec mv -vn {} ~/Desktop/ \;

Translated to English that says: “find files in ~/Desktop that are in sub-folders, and move them (using mv -vn so you won’t overwrite files if they have identical filenames) to the Desktop.”


Unix Nerd Alert: It would be slightly more efficient to use xargs but it is not nearly as nice and neat:

find ~/Desktop -mindepth 2 -type f -print0 \
| xargs -0 -J % mv -vn % ~/Desktop

Unless you’re talking about thousands of files, I’d use the first method. Just be sure to go back and check for any files that had identical names. They will still be in the original folder.

Do the same thing as above, but also take into account files that might have the same filename

I wasn’t happy just leaving that caveat of “if there is more than one file with the same name then it won’t work.”

So I wrote a script. Because of course I did.

What this script does it check to see if there is a filename conflict, and if there is, then it adds “1” to the filename, so “foo.txt” becomes “foo.1.txt” (and if there’s a “foo.1.txt” then it will go to “foo.2.txt”, and so on).

Then it will delete any empty folders in the main directory too.

It will also ignore files in .git or .sync folders. Because that would be bad.

:warning: Use entirely at your own risk! :warning: I tested it, but I can’t guarantee it doesn’t have some bug I haven’t foreseen.

#!/usr/bin/env zsh -f
# Purpose: move all files from sub-folders in a directory to that directory
#
# From:	Timothy J. Luoma
# Mail:	luomat at gmail dot com
# Date:	2019-08-30

PATH='/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'

	# Change this to whatever directory want to work on
DIR="$HOME/Desktop"

find "$DIR" -mindepth 2 -type f -print \
| egrep -v '\.(sync|git)/' \
| while read line
do
	EXT="$line:e"

	TARGET="$DIR/$line:t"

	COUNT='0'

	while [[ -e "$TARGET" ]]
	do
		((COUNT++))

		if [[ "$EXT" == "" ]]
		then
			TARGET="$DIR/$line:t.${COUNT}"
		else
			TARGET="$DIR/$line:t:r.${COUNT}.${EXT}"
		fi

	done

	mv -vn "$line" "$TARGET"

done

	# This will delete any empty folders that now exist in "$DIR"
find "$DIR" -type d -mindepth 1 -depth -exec rmdir -p {} \;

exit 0
#EOF

Hey Greg,

This is easy enough to do with AppleScript.

I can't fully test it at the moment, because I have too much stuff on my own Desktop.

You can test it though. The only “destructive” thing about the script is that it will move your Desktop folders to the Trash.

I suggest you empty your Trash before testing, so it's easier to revert if desired.

You can comment-out or delete this line to prevent the folders from being sent to the Trash:

delete folderList

I have not accounted for collisions of same-named items, but that too can be done with relative ease.

-Chris


Download: Move Contents of All Folders on Desktop to Desktop v1.00 @ccstone.kmmacros
      (6.2 KB -- Created with KM v9.0.1)