Create a 0kb file from an existing file.

So, I was wondering if this is possible to do. I want to take an existing file, a .doc, .jpg, or a .mov, and then create a file without data from it.

Essentially, it would create a file from a file name, so if I chose a folder, that had a bunch of different documents in it and ran the script, it would give me duplicates of all in another folder without any of the data.

They would still need to have all the right file types and everything as well.

I don’t know how easy this would be to do, but I’m really new to applescript, so if anyone could tell me how, I’d greatly appreciate it.

Like so, perhaps - works with documents anyway; I didn’t try applications:


set Source to (choose folder) -- the one with the real files
set N to name of (info for Source)
tell application "Finder"
	set Dest to make new folder at desktop with properties {name:N & "-Dupes"}
	set AllF to name of files of entire contents of Source
	repeat with F in AllF
		make new file at Dest with properties {name:F}
	end repeat
end tell

I tried doing it with a .dmg and instead of giving me a single file that looked like a .dmg it gave me a bunch of files that had each letter of the file name. So, I’m not really sure what’s wrong. I think that it essentially works, but I don’t know why I’m getting letters instead of the full filename for the file.

You hadn’t said what you wanted to do, but I’m sure that .dmg is not going to work this way. You can’t create an empty disk image (it can have nothing in it, but it has to have a size). The normal way to do one is to use the Disk Utility.

Why do you want to do this? If it’s just to get the icons, there are better ways.

Well, I have been trying multiple files, and instead of getting a file, I get files with a single letter as blank text documents.

So, I guess I should try to explain myself better. I was looking through the faq and I saw a way to create a new file by changing it’s properties. So, what I want to do is create a bunch of new files using filenames from a folder. So I’m think that I would need a variable to read the filename from the individual files then pass them to the make new file command into a new folder.

I feel like it should be easy, but since I haven’t ever programmed in applescript, I could be completely wrong, does that explain it any better Adam? I hope it does. I really appreciate the help, because I honestly don’t know how to do this.

Hi.

The Finder can be a bit peculiar sometimes. If there’s only one item in your folder, getting the ‘name of every item’ returns just that item’s name, not a list containing it. Consequently, the repeat will loop through the characters of the name, not the names in a list. You can get round this with an explicit coercion:

tell application "Finder"
	set AllF to (name of files of Source) as list
end tell

On the other hand, if you get more than one property at a time from ‘every item’ “ even if there’s only a single item “ each set of property values will be a list:

tell application "Finder"
	set {AllF, AllCT, AllFT} to {name, creator type, file type} of files of Source
end tell
-- > {{"Read me XMLLib.html"}, {missing value}, {missing value}}
--> Not: {"Read me XMLLib.html", missing value, missing value}

This is an inconsistency of implementation, so although you need to be aware of it, you don’t need to try and understand it. :slight_smile:

Since you actually want the file types as well, you could get them at the same time as the names and have list results:

set Source to (choose folder) -- the one with the real files
set N to name of (info for Source)
tell application "Finder"
	set Dest to make new folder at desktop with properties {name:N & "-Dupes"}
	update files of Source
	set {AllF, AllCT, AllFT} to {name, creator type, file type} of files of Source
	repeat with i from 1 to (count AllF)
		make new file at Dest with properties {name:item i of AllF, creator type:item i of AllCT, file type:item i of AllFT}
	end repeat
end tell

But note that the files are created with literally no data. They won’t necessarily be “empty documents” that can be opened by the applications with which they’re associated. Ultimately, the method used will depend on why you want to create empty files and what you want to do with them.