Copy link to clipboard
Copied
Hi folks,
I am looking for a way to determine if and where an alpha character may be found in a string.
I am trying to put a script together that would read the strings, which happen to be SKU numbers, from an Excel file. It would then determine what folder on a server to search in to retrieve a file. We have complex folder structure that is broken down into subfolders. Being able to know where the Alpha falls in the SKU# (file name) and report what the Alpha character would help direct me to the right place. Searching the terrabytes of data from the root directory is too slow.
My end goal is something along the lines of :
1) Read the first string in column A in csv file.
2) Check if string has an alpha character and in what position it is. Currently SKUs are 5 characters long.
3) If an alpha is found, report the position and what letter it is to be used to determine the correct directory path to check for the file.
4) Download/copy file to a folder location.
Theses are examples of strings and their associated folder structure.
12345 no alpha
F1234 position 1 has folders: [alpha]2000-[alpha]2999 etc. D2000-D2999
1R234 position 2 has folders: 1C000-1C999, 1D000-1D999 etc.
12Y34 position 3 has folders: 10A-19Y, 20A-29Y etc.
123X5. position 4. has folders: 000A0-099A9, 100A0-199Z9 etc.
Any help would be appreciated, thanks!
I would use regex - .test()
var skuCode = "12345";
if (/\b\d{5}\b/i.test(skuCode) === true) {
alert("There is no alpha character, there are only 5 digits!");
} else if (/\b[a-z]\d{4}\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 1!");
} else if (/\b\d[a-z]\d{3}\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 2!");
} else if (/\b\d{2}[a-z]\d{2}\b/i.test(skuCode) === true) {
alert("There is an alpha character at posi
...
Stephen got a version that works. Here's my way with the loop:
var sku = '24Y23'
var pos = 'none'
for (var i =0;i<5;i++){
var digit = sku.substr (i, 1);
if(isNaN(Number(digit))){
pos = i+1
break;
}
}
alert(pos)
Copy link to clipboard
Copied
My way might be slower. But I would create a loop for each sku number and go though each digit by using something like substr(). Then test to see if it is a number by trying to convert it to a number Number(), and then use isNaN() to see if it's a number or alpha character. Then by the counter in the loop, you can determine it's position.
Copy link to clipboard
Copied
Hi Chuck, Thank you for your feed back. I'm afraid I'm not highly skilled in the art of scripting and if you have some code examples that I can work with it would help greatly.
Copy link to clipboard
Copied
I would use regex - .test()
var skuCode = "12345";
if (/\b\d{5}\b/i.test(skuCode) === true) {
alert("There is no alpha character, there are only 5 digits!");
} else if (/\b[a-z]\d{4}\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 1!");
} else if (/\b\d[a-z]\d{3}\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 2!");
} else if (/\b\d{2}[a-z]\d{2}\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 3!");
} else if (/\b\d{3}[a-z]\d\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 4!");
} else if (/\b\d{4}[a-z]\b/i.test(skuCode) === true) {
alert("There is an alpha character at position 5!");
} else {
alert("If you see this message, something unexpected happened!");
}
You can test this by changing "12345" to "A2345" to "1B345" etc.
Copy link to clipboard
Copied
Thanks Stephen, I'm checking it out now. Cheers!
Copy link to clipboard
Copied
Regex makes my head spin!
Copy link to clipboard
Copied
@Chuck Uebele – Haha, I get it... Like JS, RegEx is just another language. I'm terrible at learning "proper" languages (French, German, Japanese etc)... RegEx and JS are also hard too, but just a bit easier!
I dedicated about a year of "free time" study to learning RegEx before scripting. I was then able to modify some existing scripts, without really knowing how to script... Problem is, I always look for a RegEx solution, when a more "standard" JS-based method may be more appropriate!
Copy link to clipboard
Copied
I'm the other way. regex as the last resort.
Copy link to clipboard
Copied
Stephen got a version that works. Here's my way with the loop:
var sku = '24Y23'
var pos = 'none'
for (var i =0;i<5;i++){
var digit = sku.substr (i, 1);
if(isNaN(Number(digit))){
pos = i+1
break;
}
}
alert(pos)
Copy link to clipboard
Copied
Cool!
Copy link to clipboard
Copied
Thank you Chuck! This worked very well too but it didn't handle a situation where there was no alpha character. I was able to pull the letter when one was present with your version,
var sku = '24t23'
var pos = 'none'
for (var i =0;i<5;i++){
var digit = sku.substr (i, 1);
if(isNaN(Number(digit))){
pos = i+1
break;
}
}
alert( "The letter " + (myFunction(sku,pos)) + " is at position " + pos)
function myFunction(a, n) {
return a.charAt(n - 1);
}
Copy link to clipboard
Copied
If there is no alpha character, the alert will say "none", but you can change the initial value of pos to 0, if that's what you want.
Copy link to clipboard
Copied
Ye, thanks Chuck. That returns a zero that can denote "no alpha".
Copy link to clipboard
Copied
So something like this:
var sku = '24423'
var pos = 0
for (var i =0;i<5;i++){
var digit = sku.substr (i, 1);
$.writeln (digit)
if(isNaN(Number(digit))){
pos = i+1
break;
}
}
alert('The position of the alpha character is at: ' + pos)
Copy link to clipboard
Copied
That works 🙂
Thanks Chuck!
Copy link to clipboard
Copied
@Chuck Uebele – would you do the same with the if/else block as I did, or is there a better way to action/process based on the result of the test?
var sku = '24423'
var pos = 0
for (var i = 0; i < 5; i++) {
var digit = sku.substr(i, 1);
$.writeln(digit)
if (isNaN(Number(digit))) {
pos = i + 1
break;
}
}
if (pos === 0) {
alert("There is no alpha character, there are only 5 digits!");
} else if (pos === 1) {
alert("There is an alpha character at position 1!");
} else if (pos === 2) {
alert("There is an alpha character at position 2!");
} else if (pos === 3) {
alert("There is an alpha character at position 3!");
} else if (pos === 4) {
alert("There is an alpha character at position 4!");
} else {
alert("There is an alpha character at position 5!");
}
P.S. Although an edge case that may not be a practical concern, how would you detect a wrong pattern, such as only 4 characters or 6 characters rather than the expected 5? A conditional check for sku.length === 5 or similar?
Copy link to clipboard
Copied
I would use a switch statement.
Copy link to clipboard
Copied
Ah, I think that I have only made one script that required a switch statement, but I can't find it to see why.
Copy link to clipboard
Copied
Switch statements tend to be streamlined. else if statements are good if what you're looking for requires multiple statements.
Copy link to clipboard
Copied
This is very helpful. Knowing what to use to get the desired results is important. I'm still very much learning.
Copy link to clipboard
Copied
It takes a lot of time. It's also goog tonlook at other 's code to see how they handle things.
Copy link to clipboard
Copied
Searching the terrabytes of data from the root directory is too slow.
My end goal is something along the lines of :
4) Download/copy file to a folder location
By @PS-Guru
What methods have you used for recursive searching under the root level?
So, working backwards and looking at this differently... You essentially just want a fast way to find a file, yes? You don't know where the file actually is, except that it is under the top-level root directory.
Find "ABC123.psd"... Then do something with the found file, such as copying it from the storage location to the desktop (as a simplified example)?
Copy link to clipboard
Copied
Yes, that's about it Stephen. If you look at my original post, I included a spreadsheet that had a folder structure that showed basicly how things are layed out. By using what you have provided, I was hoping to be able to read the file name from Excel and use it to hone in on the correct folder. Then I would pull the file from that folder to, as you said, a desktop folder for example. I would have to write something into the code to bypass a file that was not found/unavailable.
I already have a script I wrote about 12 years ago with the help of Mike Hale, R.I.P., that asks the user for CSV location, Source and destination folders. It then grabs the files, renames them to Column B and saves them.
I was hoping to somehow use it as the base to build upon. Only thing is, I would need to read the CSV and THEN determine each individual source folder based upon the SKU number read. The whole looping thing does my head in.
Here is the Find, Rename and Save script.
function main() {
var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
datafile = new File(csvFile);
var csvString = [];
if (datafile.exists){
datafile.open('r') ;
while(!datafile.eof){// read one line at a time until end of file
csvString.push( datafile.readln().replace(/^\s+|\s+$/g, '') );
}
datafile.close();
}
var searchFolder = Folder.selectDialog ("Select folder to search in")
var saveFolder = Folder.selectDialog ("Select folder to save files")
getListOfFiles(searchFolder);
for(var l =0;l<csvString.length;l++){
var toFind=csvString[l].split(',');
for(var i=0;i<searchFiles.length;i++){
var m = toFind[0].toLowerCase();
if(decodeURI(searchFiles[i].name).toLowerCase() == decodeURI(m)){
var ext =searchFiles[i].name.substring (searchFiles[i].name.lastIndexOf ('.'));
var newFilepath = saveFolder.fullName+"/"+toFind[1]+ext;
if( saveFolder.exists && searchFiles[i] instanceof File){
var newFile = new File(newFilepath);
searchFiles[i].copy(newFile);
}
}
}
}
};
searchFiles=[];
main();
function getListOfFiles(folder) {
var fileList = folder.getFiles();
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof File) searchFiles.push(file);
if (file instanceof Folder) getListOfFiles(file);
}
}
Copy link to clipboard
Copied
It might take a bit of work setting up, but you could use a nested switch fo search. So basically, you would use the same type of code you would then extract not only the position, but the letter. Then you would use that as the top level switch. Then for the next leveal, you would extract the number sequence to match up with the folder.
Copy link to clipboard
Copied
Yes, that's what I was thinking. It's a process of narrowing down from the root to the folder where the file should be located. Thank you for all your help. You and Stephen are invaluable and much appreciated!