Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Move files to the right folder.



On 10/16/06, Jan Bultereys <email@hidden> wrote:

Hi, I am looking for a script which moves file to the right folder based on the name of the files.

The most annoying part of doing that with AppleScript is getting the leading 0s into the folder names. :) Here's a solution, which the more knowledgeable on the list may pick apart. Note that it will create the appropriate Vendor_Code folder if it doesn't already exist.

set sourceFolder to choose folder with prompt "Select folder
containing files to move:"
set destRoot to choose folder with prompt "Select folder containing
vendor-code subfolders:"
tell application "Finder"
	set fileList to (get files of sourceFolder)
end tell
set oldDelim to text item delimiters
set text item delimiters to "_"
repeat with aFile in fileList
	set fileName to (name of aFile as string)
	set vendorCode to text item 2 of fileName as number
	set lowerLimit to pad(5, vendorCode div 100 * 100)
	set upperLimit to pad(5, vendorCode div 100 * 100 + 99)
	set folderName to lowerLimit & "-" & upperLimit & "_Vendor_Code"
	tell application "Finder"
		try
			set targetFolder to folder folderName of destRoot
		on error e number n partial result p from f to t
			if n = -1728 then
				set targetFolder to (make new folder in destRoot with properties
{name:folderName})
			else
				
				error e number n partial result p from f to t
			end if
		end try
		move aFile to targetFolder
	end tell
end repeat
set text item delimiters to oldDelim

on pad(limit, num)
	set resultStr to (num as string)
	repeat while length of resultStr < limit
		set resultStr to "0" & resultStr
	end repeat
	return resultStr
end pad

 Folders:

  00000-00099_Vendor_Code
  00100-00199_Vendor_Code
  00200-00299_Vendor_Code
  00300-00399_Vendor_Code
  00400-00499_Vendor_Code
  00500-00599_Vendor_Code
 .....
  12000-12099_Vendor_Code

 Name of the files:
 BE_00097_M3200W
 BE_00158_M1370

 So based on the number between BE_ and _M3200W the file should be moved to
the right folder, in this case to the folder (for the 1st example):
 00000-00099_Vendor_Code

 Any ideas are much appreciated
 Jan
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list
(email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden




--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden
References: 
 >Move files to the right folder. (From: Jan Bultereys <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.