Skip to main content
Inspiring
September 8, 2021
Answered

How to open a File that is in 1 of 4 location

  • September 8, 2021
  • 3 replies
  • 766 views

Hello,

How would I structure an IF ELSE statement to open a file that could be in one of a few different locations?  Or is there a better way to do this?

 

Thank you 🙂

 

I have a prompt that allows me to enter a 7 digit file name.

What I would like to do is this:

IF G:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002.pdf Exists

Then Open File

Else IF W:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002.pdf Exists

Then Open File

Else IF C:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002. pdf Exists

Then Open File

Else Alert "File was not located"

This topic has been closed for replies.
Correct answer m1b

There are lots of ways to approach this. I reckon something like this:

// you must first have set each of these
var path1 = "your path to directory 1",
    path2 = "your path to directory 2",
    path3 = "your path to directory 3",
    fileName = "5860002.pdf";

// make an array of paths
var paths = [path1, path2, path3];

// will return first valid file in paths, starting with first path
var myFile = getFileInPaths(fileName, paths);

if (myFile != undefined) {
    // now open myFile
}



// this is a function that gets called by code above
function getFileInPaths(fileName, paths) {
$.writeln(fileName);
    // declare this vars, but don't assign value (it will be undefined)
    var myPath;

    // keep trying a path until no paths left, or file exists
    while (myPath = paths.shift()) {
        $.writeln('myPath+fileName = ' + myPath + fileName);
        var myFile = File(myPath + fileName)
        if (myFile.exists) return myFile;
    }

    // no valid file found
    alert('No valid file found in paths')
    return;
}

- Mark

3 replies

pixxxelschubser
Community Expert
Community Expert
September 9, 2021

@BryanPagenkopf 

Sorry, I don't understand your different requirements:

 

What I would like to do is this:

IF G:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002.pdf Exists

Then Open File

Else IF W:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002.pdf Exists

Then Open File

Else IF C:\Layout\Plaques_Layout\PDF\5800000-5899999\5860000-5869999\5860002. pdf Exists



By @BryanPagenkopf

 

 


var path1 = sc + sc + "ad" + sc + "fs1" + sc + "gemini" + sc + "layout" + sc + "plaques_layout" + sc + "PDF" + sc;
var path2 = sc + sc + "wa1-fs-01" + sc + "public" + sc + "WA-Plaque Art Layouts" + sc + "WA-PDF's" + sc;
var path3 = sc + sc + "can-fs-vm01" + sc + "Public" + sc + "plaque_layouts" + sc + "PDF" + sc;
var path4 = sc + sc + "IDP-FS-02" + sc + "DATA" + sc + "Graphics" + sc + "Plate_Layouts" + sc + "Proofs" + sc;

 

By @BryanPagenkopf

 

 

What path specifications do you need/want to work with?

These?

/g/Layout/Plaques_Layout/PDF/5800000-5899999/5860000-5869999/5860002.pdf
/w/Layout/Plaques_Layout/PDF/5800000-5899999/5860000-5869999/5860002.pdf
/c/Layout/Plaques_Layout/PDF/5800000-5899999/5860000-5869999/5860002.pdf

Or such?

//ad/fs1/gemini/layout/plaques_layout/PDF/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//wa1-fs-01/public/WA-Plaque Art Layouts/WA-PDF's/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//can-fs-vm01/Public/plaque_layouts/PDF/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//IDP-FS-02/DATA/Graphics/Plate_Layouts/Proofs/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf



Inspiring
September 10, 2021

@pixxxelschubser   Sorry what is needed is this:

//ad/fs1/gemini/layout/plaques_layout/PDF/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//wa1-fs-01/public/WA-Plaque Art Layouts/WA-PDF's/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//can-fs-vm01/Public/plaque_layouts/PDF/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf
//IDP-FS-02/DATA/Graphics/Plate_Layouts/Proofs/5000000-5999999/5800000-5899999/5860000-5869999/5860002.pdf

The other one I listed were meant to be a simplified example only.

 

I would love to know how to combine what you created with what @m1b  suggested at the start of this post.

m1b
Community Expert
Community Expert
September 9, 2021

The part where you are trying to get the path of folders based on the entered 7-digit number is the tricky bit. @pixxxelschubser has put forward an excellent approach to deriving this. Here is my approach which is quite different, but will be interesting for learning purposes I hope.

var n = Number(prompt('Enter 7-digit filename:', 5860002)),
    fileName = String(n) + '.pdf',
    delim = "/",
    folderPath = getFolderPathForNumber(n) + '';

// the number 2 in the next line is how many levels of folders you want
$.writeln(getFolderPathForNumber(n, delim, 2, fileName));


function getFolderPathForNumber(n, delim, numberOfPathElements, fileName) {
    n = Number(n);
    delim = delim || "/";
    numberOfPathElements = numberOfPathElements || 2;

    var magnitude = getMagnitude(n),
        folderNames = [];

    for (var m = magnitude - 1; m >= magnitude - numberOfPathElements; m--) {
        folderNames.push(getNumberRangeString(n, m));
    }

    // add the fileName
    folderNames.push(fileName);

    return folderNames.join(delim);
}

function getNumberRangeString(n, magnitude) {
    n = Number(n);
    // magnitude here is basically how many 0s and 9s you want to see
    magnitude = magnitude || getMagnitude(n);
    // this is to fix a rounding issue if n is equal to the floor value
    n += Number.MIN_VALUE;
    // these lines scale n down by magnitude, round them down, then scale them back up
    var n1 = Math.floor(n * Math.pow(10, -magnitude)) * Math.pow(10, magnitude),
        n2 = Math.floor((n * Math.pow(10, -magnitude)) + 1) * Math.pow(10, magnitude) - 1;
    return String(n1) + '-' + String(n2);
}

function getMagnitude(n) {
    var m = -1;
    while (n + 1 >= 2) {
        m++;
        n /= 10;
    }
    return m;
}

 

- Mark 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
September 9, 2021

There are lots of ways to approach this. I reckon something like this:

// you must first have set each of these
var path1 = "your path to directory 1",
    path2 = "your path to directory 2",
    path3 = "your path to directory 3",
    fileName = "5860002.pdf";

// make an array of paths
var paths = [path1, path2, path3];

// will return first valid file in paths, starting with first path
var myFile = getFileInPaths(fileName, paths);

if (myFile != undefined) {
    // now open myFile
}



// this is a function that gets called by code above
function getFileInPaths(fileName, paths) {
$.writeln(fileName);
    // declare this vars, but don't assign value (it will be undefined)
    var myPath;

    // keep trying a path until no paths left, or file exists
    while (myPath = paths.shift()) {
        $.writeln('myPath+fileName = ' + myPath + fileName);
        var myFile = File(myPath + fileName)
        if (myFile.exists) return myFile;
    }

    // no valid file found
    alert('No valid file found in paths')
    return;
}

- Mark

Inspiring
September 9, 2021

Hey Mark,

Thank you for the assist, I'm pretty Green with Coding when it comes to Order of Operations. As an extra level of complexity I have to create the filepath for the entered file, and I am having difficulty with getting the script to work.
Here is what I have for a script attempting to use your solution:

//var doc = app.activeDocument;
var strEnter=prompt("Open file #");

//Declaring variables
var folderString;	//String that holds the current folder structure
var sc = "/";	

var path1 = sc + sc + "ad" + sc + "fs1" + sc + "gemini" + sc + "layout" + sc + "plaques_layout" + sc + "PDF" + sc;
var path2 = sc + sc + "wa1-fs-01" + sc + "public" + sc + "WA-Plaque Art Layouts" + sc + "WA-PDF's" + sc;
var path3 = sc + sc + "can-fs-vm01" + sc + "Public" + sc + "plaque_layouts" + sc + "PDF" + sc;
var path4 = sc + sc + "IDP-FS-02" + sc + "DATA" + sc + "Graphics" + sc + "Plate_Layouts" + sc + "Proofs" + sc;
//var path5 = sc + sc + "cfserver" + sc + "PDF Archive" + sc;

//make array of paths
var folderPrefix = [path1, path2, path3, path4];

//will return first valid file path
var locFile = getFileInPaths(strEnter, folderPrefix);

if (locFile != undefined) {
// now open locFile
}

//var folderPrefix = sc + sc + "ad" + sc + "fs1" + sc + "gemini" + sc + "layout" + sc + "plaques_layout" + sc + "PDF" + sc;

var driveFolder = new Folder(folderPrefix);
var soNum = strEnter.slice(0,7);
var myNum = 1;
var arrayCount;
var arrayLength;
var arr;
var newFile;


    
            //Set String to Uppercase
            soNum = soNum.toUpperCase();

        //Creating folder structure
        
                //Stripping off first 7 digits and entering them into an array 
                arrayLength = (strEnter.length );
                arrayCount = 0;
                arr = new Array(arrayLength);
                
                while (arrayCount < arrayLength)
                {
                    arr[arrayCount] = strEnter.charAt(arrayCount)
                    if (arr[arrayCount] === '/' || arr[arrayCount] === '?' || arr[arrayCount] === '<' || arr[arrayCount] === '>' || arr[arrayCount] === '\\' || arr[arrayCount] === ':' || arr[arrayCount] === '*' || arr[arrayCount] === '|' || arr[arrayCount] === '"')
                    {	
                        alert("Invalid character entered.  Do not use /,?,<,>,*,|,:,or " +'"' )
                        enterError = false;	
                    }
                    arrayCount ++;
                }
                
    

        var folder1 = arr[0] + "000000-" + arr[0] + "999999" + sc;
        var folder2 = arr[0] + arr[1] + "00000-" + arr[0] +arr[1] + "99999" + sc;
        var folder3 = arr[0] + arr[1] + arr[2] +"0000-" + arr[0] + arr[1] + arr[2] +"9999" + sc;
        var folderString = (folderPrefix  + folder2  + folder3);
	
        activeFolder = new Folder (folderString);

        var myFile= new File(folderString + sc + strEnter + ".pdf");

// this is a function that gets called by code above
function getFileInPaths(strEnter, folderString) {
    $.writeln(strEnter);
        // declare this vars, but don't assign value (it will be undefined)
        var myPath;
    
        // keep trying a path until no paths left, or file exists
        while (myPath = folderString.shift()) {
            $.writeln('myPath+strEnter = ' + myPath + strEnter);
            var myFile = File(myPath + strEnter)
            if (myFile.exists) return myFile;
//            if (myFile.exists) app.open(myFile);
        }
    
        // no valid file found
        alert('No valid file found in paths')
        return;
    }