Rename and delete files

Hi everyones, i’m new to this forum and to applescript

i need help to make a little script. i got a folder that look like this :

FOLDER_TOP
FOLDER_1111a
AAAAA_1.jpg
BBBBB_2.jpg
PHOTO_3.jpg
FOLDER_2222b
ABCDE_111.jpg
BBBB_233.jpg
PHOTO_456.jpg

The folder FOLDER_TOP can contain hundred of subfolder like FOLDER_1111a and FOLDER_2222b and each subfolder contain a lot of photos like AAAAA_1.jpg, BBBBB_2.jpg and PHOTO_3.jpg

I need to be able to rename the first photo alphabetically of each subfolder by the name of the subfolder and delete the remaining ones (AAAAA_1.jpg must became FOLDER_1111a.jpg. BBBBB_2.jpg and PHOTO_3.jpg must be deleted)

Many thanks for yours help :slight_smile:

Model: MacPro
AppleScript: 2
Browser: Safari 525.26.12
Operating System: Mac OS X (10.5)

Hey, BOBaGINETTE. Welcome on our forum !

set y to choose folder with prompt “Choose the main folder where contains the subfolders with your images”
–tell application “System Events” to set y to path to pictures folder of user domain

tell application “Finder”
repeat with i from 1 to number of items in y
set this_item to item i of y
if class of this_item is folder then
set the name of this_item to (the name of first document file of this_item as text)
delete items 2 thru -1 of this_item
end if
end repeat
end tell

Thanks JOY :slight_smile:

i got a error message : “Finder got an error: Expected a reference” and the “number” on the line “the repeat with i from 1 to number of items in y” is highlighted

Many many thanks :smiley:

Ops, a little error, nothing to worry. You can verify this script with the includet dialog. Delete or comment (–)what you want. Again welcome in this forum.

set y to choose folder with prompt “Choose the main folder where contains the subfolders with your images”
–tell application “System Events” to set y to path to pictures folder of user domain

tell application “Finder”
repeat with i from 1 to count of items in (y as text)
set this_item to item i of y
if class of this_item is folder then
display dialog ((this_item as text) & (name of document file 1 of this_item) as text)
–set the name of this_item to (the name of first document file of this_item as text)
–delete items 2 thru -1 of this_item
end if
end repeat
end tell

Thanks you so much Joy :slight_smile:

I still have a little problem.

i got this error message :

Finder got an error: Can’t get item 2 of folder “3501web-a_01.JPG” of folder “fusion copy” of folder “Desktop” of folder “Leo” of folder “Users” of startup disk.

Thanks for your time

This script works.

set y to choose folder with prompt "Choose the main folder where contains the subfolders with your images" --tell application "System Events" to set y to path to pictures folder of user domain

tell application "Finder"
	set the_path to alias (y as text)
	set c_fold to count items in the_path
	repeat with i from 1 to count of items in the_path
		set this_item to item i of the_path
		if i ≠ c_fold then
			my main(this_item)
		else if i = c_fold then
			my main(this_item)
			return 0
		end if
	end repeat
end tell


on main(this_item)
	tell application "Finder"
		if class of this_item is folder then
			set first_i to (first document file of this_item)
			set c_1 to count characters of (name of first_i as text)
			set c_2 to count characters of (name extension of first_i as text)
			set min_ to (c_1 - c_2 - 1)
			copy (characters 1 thru min_ of (name of first_i as text)) as text to final_name
			set the name of this_item to (final_name as text)
			try
				delete items 2 thru -1 of this_item
			end try
		end if
	end tell
end main

It work without any error but the results are not exactly what i need

the script rename the subfolder by the name of the first files on it, but i need the contrary. I need that the first files on each subfolder is renamed by the name of the subfolder plus the extension of the file. The rest of the files of the subfolder must be removed :slight_smile:

by the way, the file in the subfolder can be named randomly (ex. 546_sdg.jpg or photo_out.jpg)

Thank you so much for your time

Hi

Does this work:

tell application "Finder"
	set t to every folder of (choose folder)
	repeat with i in t
		set (name of first file of i) to name of i & ".jpg" as string
		delete (files of entire contents of i whose name does not contain "folder")
	end repeat
end tell

if it’s named randomly then it may not all ways be the first photo! thats gonna cause a problem.

Hi, pidge,
Congratulations .
Its short, but good (in other words, its self sufficient).

For BOBaGINETTE:

try to play a bit with the given scripts and do this with copies, not with the original files. Its funny to write applescripts ! We will help mutually, but we have to write scripts too. :smiley:

Thank you Pidge1

The script you gave me erase every files of all subfolder :frowning:

The files that need to be rename by the name of the subfolder must be the first alphabetically of the subfolder. Like i said, the subfolder can contain any numbers of files and the files can be named randomly. The only that matters, it’s that one files of the subfolder must be named by the subfolder name plus the extension of the files :slight_smile:

Thanks for your time

Hi

you need to make your mind up!

if you have have other subfolders in there that don’t need to be deleted then mentioning that up front would have been good. your original folder hierachy was this:
FOLDER_TOP
FOLDER_1111a
AAAAA_1.jpg
BBBBB_2.jpg
PHOTO_3.jpg
FOLDER_2222b
ABCDE_111.jpg
BBBB_233.jpg
PHOTO_456.jpg

the line:

set (name of first file of i) to name of i & ".jpg" as string

should get the first file are you saying that the file may begin with a number in which case that wasn’t mentioned in your original folder hierachy.

Hi pidge1,

Sorry for the confusion, my english is not perfect :wink:

The problem i have is that all subfolder cans be name anything and the files on each one can be name anything too …

What i try to make is simple :

  1. open the first subfolder
  2. take the first files (by name)
  3. Rename it by the name of it’s subfolder plus it must conserve the same extension
  4. delete all remaining files of this subfolder (if there are more than one files)
  5. go to the next subfolder and do the step 2 to 4 until there no more subfolder

it think this time is clear :slight_smile:

Thanks

Hi, i am new to mac and apple script.

I have a folder which has .giff, .tiff and .jpg files. I want to delete all of then except .jpg files.
Please help me out.

Thanks for your help
Venkata

Hi, Venkata. Welcome to these fora.

What you want can nominally be achieved with this:

-- Choose the folder containing the files.
set topFolder to (choose folder)
tell application "Finder"
	-- Make sure the Finder has the necessary info on the files. (This may not be necessary.)
	update files of topFolder
	-- Delete all non-jpeg files from the folder.
	delete (files of topFolder whose name extension is not in {"jpg", "jpeg"})
end tell

If the folder contains thousands of files, it may be necessary to try other methods, but this is the simplest.

This would be a solution to BOBaGINETTE’s original query:

set topFolder to (choose folder)
tell application "Finder"
	set theSubfolders to topFolder's folders
	repeat with thisSubfolder in theSubfolders
		set sortedFiles to (sort (get thisSubfolder's files) by name) -- Sort to *ensure* alphabetical order. 
		set firstFile to beginning of sortedFiles
		set firstFile's name to (thisSubfolder's name & "." & firstFile's name extension)
		delete rest of sortedFiles
	end repeat
end tell

Thanks Dude,
It’s working.
Appreciate your help

Hey dude i need one more help. I dont know if this would be possible in apple scripts or not. Please help me out

I got an account with snap fish and i wanna save some 300 pictures at the same time from one link. Every time if i have to save a image, i have to click that link, open the image then save it and finally go back. I have to follow the same procedure for each and every picture. Is there a way that i can automate this procedure or script it.

Thanks for your help

Hi, Venkata.

As this has nothing to do with the subject of this thread, you should post it as a new topic with a suitable subject line (assuming you can’t find an answer in the archives). That way it’s more likely to be seen by someone who knows the answer or who can research it more quickly than I have time to today. (It may even help someone else in the future who’s looking for an answer to the same problem.) Good luck. :slight_smile: