Skip to main content
Known Participant
June 28, 2013
Answered

remove file extension in a folder.

  • June 28, 2013
  • 1 reply
  • 802 views

HI Forum,

I have a strange request to you..

I have a list of files (xxxxx.jpg) in a folder XYZ in desktop.

I want to remove .jpg extension and leave the file name as it is.

Is this possible.

something i got after googling....but no worth.....

imglist = File(Folder("C:\\Documents and Settings\\Variables\\Desktop\\XYZ").getFiles);

               imglist.replace(/(.*)\.[^.]+$/, "$1");

thanks,...

shil

This topic has been closed for replies.
Correct answer dln385

Your description is unclear.

If you want to rename the files on your hard drive, make a backup of your folder and then use this code:

var myFiles = (new Folder("C:/Documents and Settings/Variables/Desktop/XYZ")).getFiles();

for (var i = 0; i < myFiles.length; i++) {

    var myFile = myFiles;

    if (myFile instanceof File) {

        var myFileName = myFile.displayName;

        myFile.rename(myFileName.slice(0, myFileName.lastIndexOf(".")));

    }

}

If you want an array of file names without extensions, use this code:

myFolder = new Folder("C:/Documents and Settings/Variables/Desktop/XYZ");

var myFiles = myFolder.getFiles(function(file) {return myFile instanceof File;});

for (var i = 0; i < myFiles.length; i++) {

    var myFileName = myFiles.displayName;

    myFiles = myFileName.slice(0, myFileName.lastIndexOf("."));

}

alert(myFiles.toString());

1 reply

dln385Correct answer
Inspiring
June 28, 2013

Your description is unclear.

If you want to rename the files on your hard drive, make a backup of your folder and then use this code:

var myFiles = (new Folder("C:/Documents and Settings/Variables/Desktop/XYZ")).getFiles();

for (var i = 0; i < myFiles.length; i++) {

    var myFile = myFiles;

    if (myFile instanceof File) {

        var myFileName = myFile.displayName;

        myFile.rename(myFileName.slice(0, myFileName.lastIndexOf(".")));

    }

}

If you want an array of file names without extensions, use this code:

myFolder = new Folder("C:/Documents and Settings/Variables/Desktop/XYZ");

var myFiles = myFolder.getFiles(function(file) {return myFile instanceof File;});

for (var i = 0; i < myFiles.length; i++) {

    var myFileName = myFiles.displayName;

    myFiles = myFileName.slice(0, myFileName.lastIndexOf("."));

}

alert(myFiles.toString());

shilpa25Author
Known Participant
June 29, 2013

Bingo!

dln385, fantastic!!!.

excellent explanation. The first approach is good to go with.

I just have a other question. If i'm not using extendScript tool kit,  on which paltform this code can be executed.

Because I used to live with extendscript tool kit. I just want to share this code to other user who don't have adobi software installed.

many thanks sir!...