Renaming files based on timestamp

I have video files being created simultaneously onto two different folders. Each files represent the same thing.

The extension of the file is what makes them to be different. For example, file in folder “MPGfiles” are named : MP200101.MPG, MP200102.MPG, MP200103.MPG, MP200104.MPG.

Files in folder “AVIfiles” are name : AV10001.AVI, AV10002.AVI, AV10003.AVI, AV10004.AVI

I currently have a applescript program which copy files in the right folder and which also look at the size of the files. Any files that are less than 35MB are being deleted.

In the current example, only the files MP200102.MPG and AV10002.AVI are smaller than 35 MB, therefore being deleted.

In the end I want the video files contained in AVIfiles to have the same prefix as files contained in MPGfiles and to keep their “.AVI” extension.

rename AV10001.AVI to MP200101.AVI
rename AV10003.AVI to MP200103.AVI
rename AV10004.AVI to MP200104.AVI (MP20102.MPG and AV10002.AVI are smaller than 35 MB, therefore have been deleted in both folders).

Files in both folders are supposed to have the exact same timestamp. Is there a way in applescript to sort the file by timestamp. Look in folders the file that have the same timestamp and to use this as a way to rename files.

Thanks!

Daniel

 set PGMname to "pDumpNONAME"
set PGMversion to "v beta1.0"

set HomeFolder to (path to home folder as text)
set MPGDestinationFolder to (path to home folder as text) & "_MPGfiles:"
set POSIXMPGFolder to POSIX path of MPGDestinationFolder
set AVIDestinationFolder to (path to home folder as text) & "_AVIfiles:"
set POSIXAVIFolder to POSIX path of AVIDestinationFolder

set cardVolume to "NONAME"
set MPGfolder to "MPGfiles"
set AVIfolder to "AVIfiles"

tell application "Finder"
	with timeout of 0 seconds
		
		set sourceFolder to folder MPGfolder of disk cardVolume as string
		set theCardFiles to files of folder sourceFolder
		try
			duplicate theCardFiles to folder MPGDestinationFolder with replacing
		on error
			display dialog "IMPORTANT - Error while copying files onto the computer, call your system administrator"
			return
		end try
		
		set sourceFolder to folder AVIfolder of disk cardVolume as string
		set theCardFiles to files of folder sourceFolder
		try
			duplicate theCardFiles to folder AVIDestinationFolder with replacing
		on error
			display dialog "IMPORTANT - Error while copying files onto the computer, call your system administrator"
			return
		end try
		
		do shell script "find " & POSIXMPGFolder & " -name *.MPG -size -35M -exec rm {} \\; "
		do shell script "find " & POSIXAVIFolder & " -name *.AVI -size -35M -exec rm {} \\; "
		
		set AVIcount to count of (files in folder AVIDestinationFolder whose name extension is "avi")
		set MPGcount to count of (files in folder MPGDestinationFolder whose name extension is "mpg")
		if AVIcount is not equal to MPGcount then
			display dialog "The number of files in AVI and MPG folders are not the same. Conact your system administrator"
			return
		end if
		
		--sort file in folder MPG by date
		set theMPGFiles to files of folder MPGDestinationFolder whose name extension is "mpg"
		--sort file in folder AVI by date
		
		set theAVIFiles to files of folder AVIDestinationFolder whose name extension is "avi"
		
		--set IndexI to 1
		--repeat until all files have been renamed
		--  if date of theMPGfiles (IndexI) is equal to date of theAVIFiles (indexI) then
		--    set newPrefix to (pos 1 to 8 of theMPEGfiles (IndexI)
		--    rename AVIDestinationFolder & ":" & theAVIfiles (indexI) to AVIDestinationFolder & ":" & newPrefix & ".AVI"
		--  end if
		-- set IndexI to IndexI + 1
		--end repeat
		
	end timeout
	
end tell

I have been able to sort the two folders correctly.

I am getting MP200101.MPG MP200103.MPG and MP200104.MPG in sortedMPGFiles
I am getting AV10001.AVI, AV10003.AVI and AV10004.AVI in sortedAVIfiles

Using a repeat loop while there are files in sortedMPGFiles and sortedAVIfiles
i equal to 1 get MP200101.MPG and AV10001.AVI
i equal to 2 get MP200103.MPG and AV10003.AVI
i equal to 3 get MP200104.MPG and AV10004.AVI
end repeat

next I will rename AV10001.AVI to MP200101.AVI…

my repeat loop is not working, would someone know why?

Thanks!

set PGMname to "pDumpNONAME"
set PGMversion to "v beta1.0"

set HomeFolder to (path to home folder as text)
set MPGDestinationFolder to (path to home folder as text) & "_MPGfiles:"
set POSIXMPGFolder to POSIX path of MPGDestinationFolder
set AVIDestinationFolder to (path to home folder as text) & "_AVIfiles:"
set POSIXAVIFolder to POSIX path of AVIDestinationFolder

set cardVolume to "NONAME"
set MPGfolder to "MPGfiles"
set AVIfolder to "AVIfiles"

tell application "Finder"
	with timeout of 0 seconds
		
		set sourceFolder to folder MPGfolder of disk cardVolume as string
		set theCardFiles to files of folder sourceFolder
		try
			--duplicate theCardFiles to folder MPGDestinationFolder with replacing
		on error
			display dialog "IMPORTANT - Error while copying files onto the computer, call your system administrator"
			return
		end try
		
		set sourceFolder to folder AVIfolder of disk cardVolume as string
		set theCardFiles to files of folder sourceFolder
		try
			--duplicate theCardFiles to folder AVIDestinationFolder with replacing
		on error
			display dialog "IMPORTANT - Error while copying files onto the computer, call your system administrator"
			return
		end try
		
		do shell script "find " & POSIXMPGFolder & " -name *.MPG -size -35M -exec rm {} \\; "
		do shell script "find " & POSIXAVIFolder & " -name *.AVI -size -35M -exec rm {} \\; "
		
		set AVIcount to count of (files in folder AVIDestinationFolder whose name extension is "avi")
		set MPGcount to count of (files in folder MPGDestinationFolder whose name extension is "mpg")
		if AVIcount is not equal to MPGcount then
			display dialog "The number of files in AVI and MPG folders are not the same. Conact your system administrator"
			return
		end if
		
		set sortedMPGFiles to paragraphs of (do shell script "ls -t " & (quoted form of (POSIX path of (MPGDestinationFolder))))
		set sortedAVIFiles to paragraphs of (do shell script "ls -t " & (quoted form of (POSIX path of (AVIDestinationFolder))))
	
		repeat with i in sortedAVIFiles
			--set j to sortedMPGFiles(i)
			display dialog i & "---" & j
		end repeat

		
		--set IndexI to 1
		--repeat until all files have been renamed
		--  if date of theMPGfiles (IndexI) is equal to date of theAVIFiles (indexI) then
		--    set newPrefix to (pos 1 to 8 of theMPEGfiles (IndexI)
		--    rename AVIDestinationFolder & ":" & theAVIfiles (indexI) to AVIDestinationFolder & ":" & newPrefix & ".AVI"
		--  end if
		-- set IndexI to IndexI + 1
		--end repeat
		
	end timeout
	
end tell

Hi,
command "ls -t " will give only a list of filenames.
so you can’t get any result with

       -- if date of theMPGfiles (IndexI) is equal to date of theAVIFiles (indexI) then

asking for a creation date requires alias “fullPathasText” and the finder … (at least at 10.4.11 :frowning: )
try this:

repeat with i from 1 to count of theMPGfiles
			tell application "Finder"
				if creation date of alias ((MPGDestinationFolder & item i of theMPGfiles) as text) is equal to creation date of ((AVIDestinationFolder & item i of theAVIFiles) as text) then
					set newPrefix to (text 1 thru 8 of item i of theMPGfiles)
					set name of file (item i of theAVIFiles) of folder AVIDestinationFolder to newPrefix & ".AVI"
				end if
			end tell
		end repeat

Have to say that i’ve not tested it at all …

Thanks Hans-Gerd Classen,

It works as I wished, here is the modifications I’ve made to the script you’ve provided me with.

Regards!
Daniel

		repeat with i from 1 to count of sortedMPGFiles
			tell application "Finder"
				if creation date of alias ((MPGDestinationFolder & item i of sortedMPGFiles) as text) is equal to creation date of alias ((AVIDestinationFolder & item i of sortedAVIFiles) as text) then
					set newPrefix to (text 1 thru 8 of item i of sortedMPGFiles)
					set name of file (item i of sortedAVIFiles) of folder AVIDestinationFolder to newPrefix & ".AVI"
				end if
			end tell
		end repeat

uaaahhh, sorry that I’ve compared the wrong lists … but you’ve done well :slight_smile: