Creating subfolders from filenames

I’ve got a folder called “VideoFile” containing the following files.

/VideoFile
M2U00101.MPG
M2U00102.MPG
M2U00201.MPG
M2U00202.MPG
M2U00203.MPG
M2U02001.MPG
M2U02002.MPG

I need to loop through the files within the VideoFile folder. Each time the number found in position 5-6 change I need to do the following:

  1. Create a folder for it. In this example three folders would then be created
    _01
    _02
    _20

  2. Copy MPG files in the appropriate folder. All of M2U001*.mpg to subfolder _01, all of M2U002*.mpg to subfolder _02 and all of M2U020*.MPG to subfolder _20

  3. In the meantime I need to inform the user the following:

    • 2 files were copied onto subfolder _01
    • 3 files were copied onto subfolder _02
    • 2 files were copied onto subfolder _20

Next time I will run the script files might have been added to the VideoFile folder. For example the file M2U02003.MPG and M2U02004.MPG might have been added.

In running the script no subfolder will need to be created at this time. However, all files with M2U020*.MPG will need to be copied. MPG files that already exist should not be copied again.

What would be the applescript command to :

  • Look at at each files contained in the VideoFile folder and find the number located at position 5 and 6 of a string such as “M2U02001.MPG”.
  • Copy all files starting with the characters located from position 1 to 6 of the same string
  • Copy only new files without replacing or recopying the files that already exist. The user does not need to be prompted. At this time the script would inform that the user that only two files were added to subfolder _20 and no new subfolder were created.

This script will be executed many time in a same day.

Thanks again!
Daniel

property kFileList : {}

tell application "Finder"
	set source_folder to choose folder with prompt "Please select directory."
	my createList(source_folder)
end tell

return kFileList

on createList(mSource_folder)
	set item_list to ""
	
	tell application "System Events"
		set item_list to get the name of every disk item of mSource_folder
	end tell
	
	set item_count to (get count of items in item_list)
	display dialog "This folder contain : " & item_count & " files"
	
	repeat with i from 1 to item_count
		set the_properties to ""
		
		set the_item to item i of the item_list
		display dialog "The item: " & the_item
		
		set theSubfolder to Mid(the_item, 5, 2)
		display dialog "Folder: /_" & theSubfolder
		
		set the_item to ((mSource_folder & the_item) as string) as alias
		
	end repeat
end createList

With this code I am able to point to the required folder. For the purpose of testing I am also able to display (one by one) the name of each file contained in the selected folder.

I am trying to use the Mid function to get the number of the subfolder I have to create if it does not exist. Would someone know why my Mid command does not work?

Would someone what applescript command I have to used in order to copy all files starting with the characters located from position 1 to 6 of the name file?

Thanks!
Daniel

Here’s how I would do what you asked in the first question. If I had a folder and a list of file names in that folder, you can move them and keep track of what you did like this.

set theFolder to path to desktop folder as text
set theFiles to {"M2U00101.MPG", "M2U00102.MPG", "M2U00201.MPG", "M2U00202.MPG", "M2U00203.MPG", "M2U02001.MPG", "M2U02002.MPG"}

set actionString to ""
repeat with aFile in theFiles
	set folderName to text 5 thru 6 of aFile
	
	-- create the folder is it doesn't exist
	tell application "Finder"
		if not (exists folder (theFolder & folderName)) then
			make new folder at folder theFolder with properties {name:folderName}
			set actionString to actionString & "Created folder: " & theFolder & folderName & return
		end if
	end tell
	
	-- move the file to the folder if the file isn't already in the folder
	tell application "Finder"
		if not (exists file (theFolder & folderName & ":" & aFile)) then
			move file (theFolder & aFile) to folder (theFolder & folderName)
			set actionString to actionString & "Moved File: " & theFolder & aFile & " to folder: " & theFolder & folderName & return
		end if
	end tell
end repeat

if actionString is "" then
	display dialog "The script did nothing!"
else
	display dialog "The following actions took place:" & return & actionString
end if

Hi,

try this


set folderList to {}
set numberList to {}

set sourceFolder to choose folder with prompt "Please select directory."
set POSIXsourceFolder to POSIX path of sourceFolder
tell application "Finder" to set theFiles to files of sourceFolder
repeat with oneFile in theFiles
	set fileName to (get name of oneFile)
	set subfolder to text 5 thru 6 of fileName
	set subfolderPath to POSIXsourceFolder & subfolder
	try
		tell application "Finder" to set fileExists to file fileName of folder ((sourceFolder as text) & subfolder)
	on error
		set fileExists to false
	end try
	if not fileExists then
		set sourceFile to quoted form of POSIX path of (oneFile as text)
		do shell script "/usr/bin/ditto " & sourceFile & space & quoted form of (subfolderPath & "/" & fileName)
		do shell script "/bin/rm -r " & sourceFile
		set flag to false
		repeat with i from 1 to count folderList
			if item i of folderList is subfolder then
				set flag to true
				exit repeat
			end if
		end repeat
		if flag then
			set item i of numberList to (get item i of numberList) + 1
		else
			set end of folderList to subfolder
			set end of numberList to 1
		end if
	end if
end repeat
set resultList to {}
repeat with i from 1 to count folderList
	set end of resultList to (item i of numberList as text) & " files were copied onto subfolder " & item i of folderList
end repeat
if resultList is {} then
	set resultString to "No files were copied"
else
	set {TID, text item delimiters} to {text item delimiters, return}
	set resultString to resultList as text
	set text item delimiters to TID
end if
display dialog resultString

pure AppleScript doesn’t know any functions like mid()

Thanks for the code, I am looking at it very closely.

Subfolders needs to be created under a folder called “VideoFile” which is located under my Username.

NX5U is the name of an external disk. Files are to be copied under the appropriate subfolder within VideoFile on the desktop. Once copied over, all MPEG files have to remain on the external disk.

Next time the script is being run, new folder might needs to be created and only files that have been added since the last execution needs to be copied.

The external disk is always to remain the same (content and directory structure), files located on that external disk have to be copied under a specific subfolder.

In getting at this, I’ve added to following two lines at the beginning of the script i.e global value and set DestinationFolder. For some reason the code would not compile because of something with the Set DestinationFolder. I do not know why. This statement work very well and other AppleScript I’ve got.

Regards, this is REALLY helping me!

set folderList to {}
set numberList to {}
global DestinationFolder

Set DestinationFolder to folder "Users:DanielPaquin:VideoFile:" of startup disk
set sourceFolder to choose folder with prompt "Please select directory."
set POSIXsourceFolder to POSIX path of sourceFolder
.
.
.

Daniel

I want to thank StefanK who provided me with the code down below.

It does exactly what I wanted to do. I’ve inserted the code in the Applescript editor and it worked instantly.

Regards!
Daniel

I also wanted to thank regulus6633 for also helping me.

set folderList to {}
set numberList to {}

set DestinationFolder to (path to home folder as text) & "VideoFile:"
set sourceFolder to choose folder with prompt "Please select directory."
set POSIXdestinationFolder to POSIX path of DestinationFolder
tell application "Finder" to set theFiles to files of sourceFolder
repeat with oneFile in theFiles
   set fileName to (get name of oneFile)
   set subfolder to text 5 thru 6 of fileName
   set subfolderPath to POSIXdestinationFolder & subfolder
   try
       tell application "Finder" to set fileExists to exists (file fileName of folder (DestinationFolder & subfolder))
   on error
       set fileExists to false
   end try
   if not fileExists then
       set sourceFile to quoted form of POSIX path of (oneFile as text)
       do shell script "/usr/bin/ditto " & sourceFile & space & quoted form of (subfolderPath & "/" & fileName)
       do shell script "/bin/rm -r " & sourceFile
       set flag to false
       repeat with i from 1 to count folderList
           if item i of folderList is subfolder then
               set flag to true
               exit repeat
           end if
       end repeat
       if flag then
           set item i of numberList to (get item i of numberList) + 1
       else
           set end of folderList to subfolder
           set end of numberList to 1
       end if
   end if
end repeat
set resultList to {}
repeat with i from 1 to count folderList
   set end of resultList to (item i of numberList as text) & " files were copied onto subfolder " & item i of folderList
end repeat
if resultList is {} then
   set resultString to "No files were copied"
else
   set {TID, text item delimiters} to {text item delimiters, return}
   set resultString to resultList as text
   set text item delimiters to TID
end if
display dialog resultString
set sourceFolder to choose folder with prompt "Please select directory."

I am trying to replace this prompt with the following command

set sourceFolder to folder “MP_ROOT:101PNV01:” of disk “NX5U”

I am using this syntax in another AppleScript and I do not have any problems. This time I am getting an error message. Why would that be?

Thanks!
Daniel

First, that style of syntax is only used with the Finder, so that code has to be in a Finder tell block. Second, you can’t have 2 folders joined by “:”, that’s not the syntax. You want this…

tell application "Finder"
	set sourceFolder to folder "101PNV01" of folder "MP_ROOT" of disk "NX5U"
end tell

Thanks regulus6633.

It worked!