Skip to main content
Gotta Dance
Inspiring
January 1, 2022
Answered

Writing a Conditional Argument Based on Document K-Size...Possible?

  • January 1, 2022
  • 3 replies
  • 409 views

Hi everybody,

 

Is it possible for Javascript (JSX) to determine the file size of a particular file, and then conditionally from that point read that value in order to execute a command?

 

Thanks for any feedback.

This topic has been closed for replies.
Correct answer Stephen Marsh

No need to reinvent the wheel, the following code should provide an example of a conditional based on file size on drive.

 


function main() {
    var folder = Folder.selectDialog("Select Folder");
    var files = folder.getFiles("*jpg");
    var smallFilesFolder = Folder(folder.fsName + "/Folder A");
    if (!smallFilesFolder.exists) {
        smallFilesFolder.create();
    }
    var largeFilesFolder = Folder(folder.fsName + "/Folder B");
    if (!largeFilesFolder.exists) {
        largeFilesFolder.create();
    }
    var len = files.length
    for (var i = 0; i < len; i++) {
        var _file = files[i];
        if (_file instanceof File) {
            if (_file.length <= 200000) { // less than equal to 200 KB
                _file.copy(smallFilesFolder + "/" + _file.name);
            } else if (_file.length > 200000) { // greater than 200 KB
                _file.copy(largeFilesFolder + "/" + _file.name);
            }
            _file.remove();
        }
    }
}

main();​

3 replies

rob day
Adobe Expert
January 2, 2022

Also, this would get the active document’s file size

 

var fsize = File(app.activeDocument.path + "/" + app.activeDocument.name).length

if (fsize > 1048576) {
    $.writeln("Active document is larger than 1MB")
    //do something
} 

 

Stephen Marsh
Adobe Expert
January 1, 2022

My first question would be; 

 

1) Size on drive

or

2) Size of open file

 

Gotta Dance
Inspiring
January 1, 2022

To answer your question, file on drive.

Stephen Marsh
Stephen MarshCorrect answer
Adobe Expert
January 1, 2022

No need to reinvent the wheel, the following code should provide an example of a conditional based on file size on drive.

 


function main() {
    var folder = Folder.selectDialog("Select Folder");
    var files = folder.getFiles("*jpg");
    var smallFilesFolder = Folder(folder.fsName + "/Folder A");
    if (!smallFilesFolder.exists) {
        smallFilesFolder.create();
    }
    var largeFilesFolder = Folder(folder.fsName + "/Folder B");
    if (!largeFilesFolder.exists) {
        largeFilesFolder.create();
    }
    var len = files.length
    for (var i = 0; i < len; i++) {
        var _file = files[i];
        if (_file instanceof File) {
            if (_file.length <= 200000) { // less than equal to 200 KB
                _file.copy(smallFilesFolder + "/" + _file.name);
            } else if (_file.length > 200000) { // greater than 200 KB
                _file.copy(largeFilesFolder + "/" + _file.name);
            }
            _file.remove();
        }
    }
}

main();​
c.pfaffenbichler
Adobe Expert
January 1, 2022

I suppose one could get the length and then use that in if-clauses or a while-clause. 

var aFile = selectFile(false, "pat")
alert (aFile.length);
////////////////////////////////////
////////////////////////////////////
////////////////////////////////////
////// select file //////
function selectFile (multi) {
if (multi == true) {var theString = "please select files"}
else {var theString = "please select one file"};
if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog (theString, '*.jpg;*.tif;*.psd;*.png', multi)}
else {var theFiles = File.openDialog (theString, getFiles, multi)};
////// filter files  for mac //////
function getFiles (theFile) {
    if (theFile.name.match(/\.(jpg|tif|psd|png)$/i) || theFile.constructor.name == "Folder") {
        return true
        };
	};
return theFiles
};