Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Create Placeholders for downloaded files Web Browsers
Since I work in IT in the education sector, I'm always looking for whatever things I can find that are free that help me get things done.

I often have a hard time keeping track of what I've downloaded, tested, and then discarded. This is what I use to address that issue.

I wanted a simple way to remind myself which not-so-useful files (driver, utility, whatever) I have downloaded without leaving them on the machine taking up space. Since I already use file labels and they're a very quick way to categorize files, I decided to use them to mark which files to create 'Placeholders' for.

The following command that will delete all files in a directory that are labeled 'Gray' and then will create a new file that does not use disk space as a reminder.

Open Terminal.app and type cd followed by a space, then drag and drop the folder containing the files you've labeled onto Terminal.app's window (which will enter the full path for you), and then hit return. Paste the following command into Terminal.app and hit enter and the files that are 'Gray' will be replaced with 'Placeholder' files with the same name.

[crarko adds: There were some errors in the scripts listed in the comments below; I've updated the scripts to reflect the changes.]

find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \; | awk -F " = " ' $2 ~ /1/{ getline; print $2 }' | sed -ne '/^".*/p' | awk '{ print "rm "$0"; touch "$0 }' | bash
The following command will show what files will be deleted.
find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \; | awk -F " = " ' $2 == "1"{ getline; print $2 }' | sed -ne '/^".*/p' | awk '{ print "rm "$0"; touch "$0 }'
If your web browser is set to ask where to save downloaded files, when you go to download the same file again it will append '-1' (Firefox) or '(1)' (Chrome) to the end of the file name, letting you know you've already downloaded this before.

The '1' in $2 ~ /1/{ getline refers to the label 'Gray.' The label colors can be specified by the following numbers:
  • 0 = No Label
  • 1 = Gray
  • 2 = Green
  • 3 = Purple
  • 4 = Blue
  • 5 = Yellow
  • 6 = Red
  • 7 = Orange

[crarko adds: I haven't tested this one.]
    •    
  • Currently 2.67 / 5
  You rated: 5 / 5 (6 votes cast)
 
[4,979 views]  

Create Placeholders for downloaded files | 8 comments | Create New Account
Click here to return to the 'Create Placeholders for downloaded files' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create Placeholders for downloaded files
Authored by: postglock on Mar 11, '11 06:06:41PM

I think you copy-pasted something incorrectly. The two lines of code are identical.



[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: Magnus Dredd on Mar 13, '11 09:09:07PM

You're correct.

The following will show what will be removed:
find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \; | awk -F " = " ' $2 == "1"{ getline; print $2 }' | sed -ne '/^".*/p' | awk '{ print "rm "$0"; touch "$0 }'

Will do it:
find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \; | awk -F " = " ' $2 == "1" { getline; print $2 }' | sed -ne '/^".*/p' | awk '{ print "rm "$0"; touch "$0 }' | bash

The command has been updated in response to a comment below.



[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: crarko on Mar 15, '11 05:57:02AM

I've edited the hint to reflect these corrections. Thanks!

Craig A.



[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: tedw on Mar 12, '11 08:11:07PM
For those of you who want to do this without having to parse that mind-boggling block of unix code, here's an applescript to do the same thing.
on run
	tell application "Finder"
		try
			set theFolder to (the target of the front window) as alias
		on error
			return
		end try
		
		set theFiles to every file of theFolder whose label index is 7
		repeat with thisFile in theFiles
			set theName to name of thisFile
			delete thisFile
			make new file at theFolder with properties {name:theName}
		end repeat
		-- uncomment the following line if you want the trash to empty immediately
		-- empty trash
	end tell
end run
The is designed to be used as a Finder menubar item: copy the above code into Script Editor, save it as an application, and then drag the application icon onto the toolbar of a Finder window. The icon will stick there, and you can click it in any open Finder window to run the script on that folder.

[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: vczilla on Mar 13, '11 11:29:48AM
Be warned this script is buggy.The awk command matches all files containing a '1' in their name
awk -F " = " ' $2 ~ /1/ { getline;print $2; }'
matches all second fields containing '1' ( ie "My Download1.pdf"). What the author meant was
awk -F " = " ' $2 == "1" { getline;print $2; }'
Don't use it uncorrected you'll erase all of your files in the current directory containing a '1' in their name!

[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: Magnus Dredd on Mar 13, '11 09:01:04PM

There are a substantial number of files containing a "1" in my download directory which haven't been deleted.

To be sure however, I just tried a test set again... Repeatedly...

You're not completely incorrect, but almost.

List of files (Every single one contain a "1"):
Getting Started With Oracle Solaris 11 Express Guide 820-7799.pdf
M3291 InstallGuide.pdf
Prioritized_Approach_PCI_DSS_1_2.pdf
file-1.pdf
hdama bios 188.zip
information_supplement_11.3.pdf

The first part of the script
find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \;
Outputs this:

kMDItemFSLabel = 1
kMDItemFSName = "file-1.pdf"
kMDItemFSLabel = 0
kMDItemFSName = "Getting Started With Oracle Solaris 11 Express Guide 820-7799.pdf"
kMDItemFSLabel = 0
kMDItemFSName = "hdama bios 188.zip"
kMDItemFSLabel = 0
kMDItemFSName = "information_supplement_11.3.pdf"
kMDItemFSLabel = 0
kMDItemFSName = "M3291 InstallGuide.pdf"
kMDItemFSLabel = 0
kMDItemFSName = "Prioritized_Approach_PCI_DSS_1_2.pdf"

find . -exec mdls -name kMDItemFSLabel {} \; -exec mdls -name kMDItemFSName {} \; | awk -F " = " ' $2 ~ /1/{ getline; print $2 }' | sed -ne '/^".*/p' | awk '{ print "rm "$0"; touch "$0 }'

Outputs this:
rm "file-1.pdf"; touch "file-1.pdf"
rm "Prioritized_Approach_PCI_DSS_1_2.pdf"; touch "Prioritized_Approach_PCI_DSS_1_2.pdf"

All of the files contain a "1".
One file was "grey"
One file contained a "1" and it was the last one found!!!

When the script finds a "1" in the part of a line after the " = " it then grabs the <b>next line</b> and ignores the current one.

So if it matches a "1" in a file name:
kMDItemFSName = "hdama bios 188.zip"
It gets the next line which is:
kMDItemFSLabel = 0
Of which it would try to delete "0", except that I use sed to filter any line not containing a `"` (quote)

The problem is that if it matches the file name on the last line.... "getline" cannot get the next line and returns the current one.

The bottom line is that your change is a good idea.
Thanks...



[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: vczilla on Mar 15, '11 04:21:04AM

Yes sorry if that wasn't clear my comment was only on the $2 ~ /1/ part that matches every second field containing a '1' (so it matches filenames containing a '1' but it also matches kMDItemFSLabel = 1 ) not on the overal effect of your script. Glad you liked the fix.



[ Reply to This | # ]
Create Placeholders for downloaded files
Authored by: dyjo on Mar 26, '11 04:41:48AM
more strait forward I think, honores space chars in path names
mdfind -onlyin . kMDItemFSLabel == 1 | \
while read file; do
    test -f "$file" && echo "$file" && > "$file"
done
... as one liner
mdfind -onlyin . kMDItemFSLabel == 1 | while read file; do test -f "$file" && echo "$file" && > "$file"; done 


[ Reply to This | # ]