Skip to main content
kareyh56356757
Participating Frequently
January 24, 2017
Answered

Save in directory based on filename.

  • January 24, 2017
  • 3 replies
  • 1496 views

I am very new to scripting.  I am looking to create a script that will save a .tif file with LZW compression to a file in a directory based on the current image filename.  If that directory doesn't exist it will create it.  All of my filenames are based on a 7 digit system. 

For example... I want to take file name  "123-456_A"  and put it in the following folder system...

Product(Starting folder on the server)
-1
--12
---123
----123-
-----123-4
------123-45
-------123-456(Final Folder)

The "_A" etc will be ignored. Is this possible? 

This topic has been closed for replies.
Correct answer pixxxelschubser

kareyh56356757​,

a folder hierarchy like yours doesn't make any sense for me, sorry. But anyway, try this:

// folder_saveTif_inFolderBasedOnFilename.jsx

// https://forums.adobe.com/thread/2268221

// regards pixxxel schubser

// only file names eg 123-456_A or 100-123_Z allowed

var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^\d{3}-\d{3}_./, tmp = nme.split("");

checkAndSave();

function checkAndSave() {

    if( reg.test( nme ) == false) {

        alert ("wrong file name"); return; }

    else { tmp.splice(3,1); tmp.splice(-2); tmp[2] += "-";

        for( i = 1; i < 6; i++ ) { tmp = tmp[i-1] + tmp; };

        var saveFol = Folder ("~/Desktop");

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

            saveFol = Folder( saveFol + "/" + tmp );

            if (!saveFol.exists) { saveFol.create() };

        };

    var saveName = "/" + nme + ".tif";

    var saveFile = new File( saveFol + saveName );

    if( saveFile.exists ) { alert( saveFile + "\nFile already exists - not saved now" ); return; };

    saveAsTIFF( saveFile ); alert( saveFol + saveName + " saved" )

    //aDoc.close( SaveOptions.DONOTSAVECHANGES );

    }; return;

};

function saveAsTIFF( saveFile ) {

    tiffSaveOptions = new TiffSaveOptions();

    tiffSaveOptions.layers = true;

    tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;

    activeDocument.saveAs( saveFile, tiffSaveOptions, false );

}

This snippet should create the wished folder hierarchy (on your desktop) based on the given example file name.

Have fun

3 replies

kareyh56356757
Participating Frequently
June 9, 2017

I realized my mistake and fixed it with the following...

var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split(""); 

checkAndSave();

I had been trying to read the letters as digits. 

pixxxelschubser
Community Expert
Community Expert
June 9, 2017

Regex is not easier way off. You should understand the syntax before.

Try this snippet (standalone):

var a = "1234567_A";

var b = "ZZ12345_A";

var  nme = a.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split("");

checkAndSave();

nme = b.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split("");

checkAndSave();

nme = a.replace(/\.[^\.]+$/,""), reg = /^[A-Z]{2}\d{5}_./, tmp = nme.split("");

checkAndSave();

nme = b.replace(/\.[^\.]+$/,""), reg = /^[A-Z]{2}\d{5}_./, tmp = nme.split("");

checkAndSave();

function checkAndSave() { 

    if( reg.test( nme ) == false) { 

        alert (reg+" better regex do not find: "+ nme); return; } 

    else { alert (reg+" found: "+nme) }

    }

The reason is: \w will find all "word signs" eg digits AND letters

Have fun

kareyh56356757
Participating Frequently
June 9, 2017

This script has been working perfect for me!  Is there a way to alter it for files that have a naming structure of ZZ12345_A ?  Basically I want it to read any 7 digit filename and put it in the coordinating folder. 

pixxxelschubser
Community Expert
Community Expert
June 9, 2017

Hi kareyh56356757​,

hmmh?

ZZ12345_A is not a 7 digit filename.

Furthermore:

which letters and/or digits in the filename are fix and which ones are variable?

c.pfaffenbichler
Community Expert
Community Expert
January 25, 2017

Please post screenshots illustrating the task and Folder structure better.

And please explain what exactly you mean by

save a .tif file with LZW compression

Save a copy of the active File? Save the active File as a new file?

With Layers or flattened?

kareyh56356757
Participating Frequently
January 25, 2017


Would be saving the active file as a new file with the following TIF options.

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
January 26, 2017

kareyh56356757​,

a folder hierarchy like yours doesn't make any sense for me, sorry. But anyway, try this:

// folder_saveTif_inFolderBasedOnFilename.jsx

// https://forums.adobe.com/thread/2268221

// regards pixxxel schubser

// only file names eg 123-456_A or 100-123_Z allowed

var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^\d{3}-\d{3}_./, tmp = nme.split("");

checkAndSave();

function checkAndSave() {

    if( reg.test( nme ) == false) {

        alert ("wrong file name"); return; }

    else { tmp.splice(3,1); tmp.splice(-2); tmp[2] += "-";

        for( i = 1; i < 6; i++ ) { tmp = tmp[i-1] + tmp; };

        var saveFol = Folder ("~/Desktop");

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

            saveFol = Folder( saveFol + "/" + tmp );

            if (!saveFol.exists) { saveFol.create() };

        };

    var saveName = "/" + nme + ".tif";

    var saveFile = new File( saveFol + saveName );

    if( saveFile.exists ) { alert( saveFile + "\nFile already exists - not saved now" ); return; };

    saveAsTIFF( saveFile ); alert( saveFol + saveName + " saved" )

    //aDoc.close( SaveOptions.DONOTSAVECHANGES );

    }; return;

};

function saveAsTIFF( saveFile ) {

    tiffSaveOptions = new TiffSaveOptions();

    tiffSaveOptions.layers = true;

    tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;

    activeDocument.saveAs( saveFile, tiffSaveOptions, false );

}

This snippet should create the wished folder hierarchy (on your desktop) based on the given example file name.

Have fun