shorten files based on content in a range

thinksuccess

New Member
Joined
Jan 29, 2003
Messages
16
Hi,

I have a list of image files that were auto generated and thus contain letters and or numbers that should not be there.

Here is an example of what I want to be able to do.

Rename: AT4040_t_16140.jpg

To: AT4040.jpg

Basically the "_t_and anything betweeen the .jpg is what I want to get rid of.


Thanks
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

DRJ

MrExcel MVP
Joined
Feb 17, 2002
Messages
3,853
lets say the file name was in A1 then type this in B1

=LEFT(A1,FIND("_",A1,1)-1)&".jpg"

This should get you started.

-Jacob
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
The other part you might need is the Name Statement from VBA. From VB Help:
Name Statement Example
This example uses the Name statement to rename a file. For purposes of this example, assume that the directories or folders that are specified already exist. On the Macintosh, “HD:” is the default drive name and portions of the pathname are separated by colons instead of backslashes.

Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' Define file names.
Name OldName As NewName ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' Move and rename file.
 

thinksuccess

New Member
Joined
Jan 29, 2003
Messages
16
(y)
Thanks DRJ! That worked perfect!

Now if I could just figure out how to actually rename the images on the server :)
 

DRJ

MrExcel MVP
Joined
Feb 17, 2002
Messages
3,853
See if Greg Truby's solution helps you. If not we can come up with something else.
 

thinksuccess

New Member
Joined
Jan 29, 2003
Messages
16
The problem is all my images are auto generated on a unix server so I would need some type of rename script. Actually the rename script I use now just for lowercase of the extensions is:

#!/usr/bin/perl -w
#
#

chomp($L = `ls -1 *.jpg *.JPG`);

@fls = split(/\n/, $L);

foreach $i (@fls) {
($name, $ext) = split(/\./, $i);
rename($i, uc($name).".".lc($ext));
}

If this could be modifed to do what I need that would be great!

I know this is an excel board but you guys are so smart I'm confident someone can come up with something.

Thanks Again!
 

Watch MrExcel Video

Forum statistics

Threads
1,123,088
Messages
5,599,664
Members
414,325
Latest member
kfg1287

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top