Copying Files

I need help with this script. I need to copy pdf files from one folder to differnet sub folders in another folder. The file names will be wta, wtb, wtc, gta, gtb, gtc. The common letter will be A, B, C, D, E, F. But when it gets down to D it will copy all files since they are pdfs rather than just copy the ones named wtd.pdf or gtd.pdf
Any help would be greatful.

Thanks

– Get todays month
set themonth to month of (current date)
– Get todays year
set theYear to year of (current date)
–Get today’s day
set WDay to weekday of (current date) as text
– Selecting the day Folder if it is less than 10 it adds a 0 in front
if day of (current date) is less than 10 then
set theDay to “0” & day of (current date)
else
set theDay to day of (current date) as text
end if
– Set the folder names
set the_folders to {“ASection”, “BSection”, “CSection”, “DSection”, “ESection”, “FSection”} as list

set theSource to alias “Macintosh HD:Users:comp1:Desktop:Test 1:”

– Selecting the Month Folder
if month of (current date) is March then
set theInput to "03 " & themonth & " " & theYear
else if month of (current date) is April then
set theInput to "04 " & themonth & " " & theYear
else if month of (current date) is May then
set theInput to "05 " & themonth & " " & theYear
else if month of (current date) is June then
set theInput to "06 " & themonth & " " & theYear
else if month of (current date) is July then
set theInput to "07 " & themonth & " " & theYear
else if month of (current date) is August then
set theInput to "08 " & themonth & " " & theYear
else if month of (current date) is September then
set theInput to "09 " & themonth & " " & theYear
else if month of (current date) is October then
set theInput to "10 " & themonth & " " & theYear
else if month of (current date) is November then
set theInput to "11 " & themonth & " " & theYear
else if month of (current date) is December then
set theInput to "12 " & themonth & " " & theYear
else if month of (current date) is January then
set theInput to "01 " & themonth & " " & theYear
else if month of (current date) is February then
set theInput to "02 " & themonth & " " & theYear
end if

tell application “Finder”
activate
with timeout of 600 seconds
if not (exists folder theInput of disk “PDF Archive”) then make new folder of disk “PDF Archive” with properties {name:theInput}
if not (exists folder theDay of folder theInput of disk “PDF Archive”) then make new folder of folder theInput of disk “PDF Archive” with properties {name:theDay}
repeat with i from 1 to number of items in the_folders
make new folder at folder theDay of folder theInput of disk “PDF Archive” with properties {name:item i of the_folders}
end repeat
set These_FilesA to (files of folder theSource whose name contains “A”)
set These_Filesb to (files of folder theSource whose name contains “B”)
set These_Filesc to (files of folder theSource whose name contains “C”)
set These_Filesd to (files of folder theSource whose name contains “D”)
set These_Filese to (files of folder theSource whose name contains “E”)
set These_Filesf to (files of folder theSource whose name contains “F”)

	duplicate These_FilesA to folder "ASection" of folder theDay of folder theInput of folder "PDF Archive"
	
	
end timeout
empty trash

end tell

Try something like this, tribunescripter:


to |get folder| for n at c
	tell application "Finder" to tell item c
		tell folder n to if exists then return it as alias
		(make new folder at it with properties {name:n}) as alias
	end tell
end |get folder|

tell (current date) to tell (year * 10000 + (its month) * 100 + day as string) & its month to ¬
	set {theInput, theDay} to {text 5 thru 6 & space & text 9 thru -1 & space & text 1 thru 4, text 7 thru 8}

set srcFolder to (path to desktop as Unicode text) & "Test 1"
set tgtFolder to (|get folder| for theDay at (|get folder| for theInput at "PDF Archive"))
tell application "Finder" to repeat with i in {"A", "B", "C", "D", "E", "F"}
	with timeout of 600 seconds
		duplicate (files of folder srcFolder whose name ends with i & ".pdf") to my (|get folder| for i & "Section" at tgtFolder)
	end timeout
end repeat


kai, when I run the script it only creates the folder it doesn’t copy the files.
what do I need to change?

Thanks

Let’s start by checking a couple of my assumptions, tribunescripter:

¢ On your machine, the following statement returns the correct path:

set srcFolder to (path to desktop as Unicode text) & "Test 1"

--> "Macintosh HD:Users:kai:Desktop:Test 1" (* for me *)
--> "Macintosh HD:Users:comp1:Desktop:Test 1:" (* for you *)

¢ Your folder “/Users/comp1/Desktop/Test 1/” contains pdf files with names like the following:

set srcFolder to (path to desktop as Unicode text) & "Test 1"
tell application "Finder" to name of files of folder srcFolder whose name ends with ".pdf"

--> "gta.pdf", "gtb.pdf", "gtc.pdf", "wta.pdf", "wtb.pdf", "wtc.pdf", "wtd.pdf"} (* or similar *)

If that all works, then we clearly have another issue to address (but at least we can be sure we’re starting from roughly the same point). :slight_smile:

Yes everything looks good, the file names will be wta001.pdf, or gta001.pdf same with the B, C, D, E, and F. and what I need for it to do is copy the section A’s into the ASection folder, B’s into BSection folder, etc.

Again thanks for your help, as I just barely starting working with script language. :slight_smile:

Ah yes ” that would explain the problem, tribunescripter.

I was working on the basis that the filenames looked like:

However, it turns out they’re more like:

While this might seem like a small difference, my suggestion was actually looking for filenames that end like this:

But obviously, since the names have numeric characters between the search character and the name extension, that won’t work for you.

Normally, we’d now have to determine the precise naming convention that you’re using ” and then devise a parsing method to identify the search character in each name. For example, from what I’ve now gleaned, I might assume that the filenames are constructed like this:

The kind of questions that could qualify this further might include:

In this case, however, I wonder if we can use a little trick that avoids the need to define such a comprehensive set of parsing rules.

The version below temporarily hides name extensions ” so that displayed (rather than full) filenames can be searched for the relevant characters. (I’ve also added a few comments to give some indication of what’s happening where.)


to |get folder| for folder_name at container_name
	
	(* if the required folder exists, then return it - otherwise make it *)
	tell application "Finder" to tell item container_name
		tell folder folder_name to if exists then return it as alias
		(make new folder at it with properties {name:folder_name}) as alias
	end tell
	
end |get folder|

to |duplicate files| of extn from source_folder to target_path
	
	(* define a month/year string and day string derived from the current date *)
	tell (current date) to tell (year * 10000 + (its month) * 100 + day as string) & its month to set {|month/year string|, |day string|} to {text 5 thru 6 & space & text 9 thru -1 & space & text 1 thru 4, text 7 thru 8}
	
	(* make sure required targets exist - named after the above date-based strings *)
	set target_folder to (|get folder| for |day string| at (|get folder| for |month/year string| at target_path))
	
	tell application "Finder" to tell folder source_folder
		
		(* make sure the source folder contains relevant files, check if extensions are currently hidden - and then hide extensions *)
		tell (files whose name extension is extn)
			if not (exists) then error number -128
			set show_ext to not extension hidden of file 1
			set extension hidden to true
		end tell
		
		(* with extensions hidden, they can be ignored while displayed names are checked *)
		try (* in case something goes wrong *)
			repeat with i in {"A", "B", "C", "D", "E", "F"} (* check filenames for these search characters *)
				
				(* for each search character, check that files exist, make a section folder - and duplicate the relevant files to it *)
				tell (files whose name extension is extn and displayed name contains i) to if exists then
					with timeout of 600 seconds
						set section_folder to my (|get folder| for i & "Section" at target_folder)
						duplicate to section_folder
						if show_ext then set extension hidden of section_folder's files to false
					end timeout
				end if
			end repeat
			
		on error error_msg number error_num
			
			(* restore extension hidden property of original files and report error *)
			if show_ext then set extension hidden of (files whose name extension is extn) to false
			error error_msg & return & "(Error number: " & error_num & ")"
			
		end try
		
		(* restore extension hidden property of original files *)
		if show_ext then set extension hidden of (files whose name extension is extn) to false
		
	end tell
end |duplicate files|

(* define source and target - and invoke duplication of relevant files from one to the other *)
set source_folder to (path to desktop as Unicode text) & "Test 1"
set target_path to "PDF Archive"

|duplicate files| of "pdf" from source_folder to target_path