move file

Hi All,

I have a folder named “archive” which contains so many folders like cho1, ch02, ch03… And there is another folder named containing files c01.pdf, c02.odf, c03.pdf… like that. I want to perform a script so that each pdf file should be move in their specific folder i.e. c01 should be in ch01 etc.

Regards,
Rajeev Kumar

As long as your filenames are as you described, this will work with up to 99 folders.


set FilesFolder to choose folder with prompt "Select the folder of files"
set Archive to choose folder with prompt "Select the master folder with the destination folders"


tell application "Finder"
	set TheFolders to every folder of Archive
	set TheFiles to every file of FilesFolder
end tell

repeat with thisFile in TheFiles
	set ThisFilename to name of thisFile
	set AppleScript's text item delimiters to "."
	set Basename to text item 1 of ThisFilename
	set AppleScript's text item delimiters to ""
	set FileBaseNum to (characters 2 thru 3 of Basename) as string
	
	repeat with thisFolder in TheFolders
		
		set ThisFoldname to name of thisFolder
		set ThisFoldNum to (characters 3 thru 4 of ThisFoldname) as string
		if FileBaseNum is ThisFoldNum then tell application "Finder"
			move thisFile to thisFolder
		end tell
		
	end repeat
end repeat

SC

Hi thanks,
It is working, but since I am a new starter so I am getting some problem to understand about thiFile and thiFolder. In the mean time I have also developed a program please analyze it an can you correct me where I am making mistake.

set target_folder to (choose folder with prompt "choose source folder")
set target_folder_path to target_folder as string
set target_files to (list folder target_folder without visible)

set source_folder to (choose folder with prompt "Choose source folder")
set source_folder_path to source_folder as string
set source_files to (list folder target_folder without visible)

set tot to count of every item of source_files
repeat with i from tot to 1 by -1
	if item i of target_files is equal to item i of source_files then
		tell application "Finder"
			move file source_files to folder target_folder
			
		end tell
	end if
end repeat

As a beginner, its time to meet your new friend, the Result window. Open the result window from the Script Editor menu, “Show Result”.

Now, run these one at a time and look at the result. That is what you have successfully (or not) communicated to Applescript.


set target_folder to choose folder

That’s not just the folder name, it’s a full path to it.

list folder gives you the item names, not the paths. You will get the names, but not be able to do anything with them. You need a file returned with a full path:


set target_folder to choose folder

set target_files to every file of target_folder

Popped up an error, didn’t it? That’s because now your doing a Finder job.


set target_folder to choose folder
tell application "Finder"
	set target_files to every file of target_folder
end tell

See how the result is not only filenames, but the paths to them as well? That’s what you need to move files. The folder they move to needs to be a full path as well.
So-
FilesFolder is a folder you select. I set theFiles to every file of FilesFolder. In the first repeat loop, each file is processed one by one, by the name I gave it, ‘thisFile’.

Archive is a folder you select. I set theFolders to every folder of archive. In the second repeat loop, each folder is processed one by one, by the name I gave it, ‘thisFolder’.

So you see the pattern for the repeat loop needs no count, because you’ve defined how many times with
repeat with Any_Variable in A_List
–actions
end repeat
You can name the variable anything that isn’t an AS term, ie you can’t use the word “item”. That variable will be the reference to each list item, so choose it’s name to help you.
SC

Thanks I understand well. There is a littile problem “Archive” folder contains a folder named “pdf” for the character 3 to 4 is not recognised. Then I increased its string length and then compile it. But without increasing the string, can we compile this program?

One another problem in the second Archive folder each “ch01, ch02, …” contains “text_s” folder and I want to move that files in that folder.

Regards,
Rajeev

This has been an “issue” for me lately… People post with the topic, write a script, and then they tell you “By the way… this won’t work because I forgot to mention…”
Take some time to look over exactly what the parameters are; the filenames involved, and any complications… It’s a waste of energy to write a script for “Condition 1,2,3” when you’ll have to go back and alter it later for “Conditions 4 and 5”, possibly even re-write it.
So I am going to ask you to take another look at what you have. Make sure you have considered every option and limitation so I don’t have to re-write this 5 times, as I’ve done before.

“Hey, hand me that thing over there.”

Q. What is “thing” and “there”?
A. I’ll never know if you don’t tell me

SC

Hi Siticom,
I really sorry for that, actually my production team has given me the wrong information, otherwise I never want to waste your time & energy.
I really thanks you to help me a lot.

Now see what they direct me:

Archive contains folder ch01, ch02, ch03…. Each folders conatins text_s folder.

There is another folder containing c01.pdf, c02.pdf, c03.pdf… files.

I want to move c01.pdf, c02.pdf, c03.pdf… files into text_s of their respective folders ch01, ch02, ch03….

I telling you truth that I try to develop before asking to you, but again I fail to do it.

Regards,
Rajeev


set FilesFolder to choose folder with prompt "Select the folder of files"
set Archive to choose folder with prompt "Select the master folder with the destination folders"


tell application "Finder"
	set TheFolders to every folder of Archive
	set TheFiles to every file of FilesFolder
end tell

repeat with thisFile in TheFiles
	set ThisFilename to name of thisFile
	set AppleScript's text item delimiters to "."
	set Basename to text item 1 of ThisFilename
	set AppleScript's text item delimiters to ""
	set FileBaseNum to (characters 2 thru 3 of Basename) as string
	
	repeat with thisFolder in TheFolders
		
		set text_sFolder to (thisFolder as string) & "text_s:" as string--change made here
		
		set ThisFoldname to name of thisFolder
		set ThisFoldNum to (characters 3 thru 4 of ThisFoldname) as string
		if FileBaseNum is ThisFoldNum then tell application "Finder"
			move thisFile to text_sFolder--change made here
		end tell
		
	end repeat
end repeat

No need to be sorry, I appreciate that you try to communicate in another language. I only speak English.
This should work and post back for any tune-ups.
CS

Thanks it is working fine.
Regards,
Rajeev