File path validation

William08
Tera Contributor

Hi all,

 

Need a help on file path validation for below patterns. Could anyone please help on this

 

x:\File

\File\File\ or
\File\File or
\File\\File etc

 

Thanks in advance

1 ACCEPTED SOLUTION

Hi @William08 , 

 

I have validated the below code in background script can you please check once

var patterns = [
  /^E:[\\\/].+[\\\/]?$/,
  /^[a-zA-Z0-9_]+([\\\/][a-zA-Z0-9_]+)+[\\\/]?$/,
];
var filePaths = 'E:\\Test/data1/data2/data2/data2/';
gs.print(filePaths);
var pattern = patterns[0]; // Select the first pattern
if (pattern.test(filePaths)) {
  gs.print("matched");
} else {
  gs.print("mismatched");
}

 Output :

*** Script: E:\Test/data1/data2/data2/data2/
*** Script: matched

 

SaiShravan_0-1686234349016.png

 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

View solution in original post

6 REPLIES 6

Sai Shravan
Mega Sage

Hi @William08 ,

you can use regular expressions (regex) to define the validation rules.

// Define the file path patterns
var patterns = [
  /^x:\\[a-zA-Z0-9_]+$/,
  /^\\[a-zA-Z0-9_]+(\\[a-zA-Z0-9_]+)+$/,
  /^\\[a-zA-Z0-9_]+(\\[a-zA-Z0-9_]+)*$/,
  /^\\[a-zA-Z0-9_]+\\\\[a-zA-Z0-9_]+$/
];

// Example file paths to validate
var filePaths = [
  'x:\\File',
  '\\File\\File\\',
  '\\File\\File',
  '\\File\\\\File'
];

// Validate file paths against the patterns
filePaths.forEach(function(filePath) {
  var isValid = patterns.some(function(pattern) {
    return pattern.test(filePath);
  });

  gs.info('File path: ' + filePath + ' - Valid: ' + isValid);
});

 

Tested Output:

SaiShravan_0-1686219758584.png

 

The patterns array contains the regular expressions that define the file path patterns you provided. The filePaths array includes example file paths that you want to validate.

 

The code uses the test() method of each pattern to check if a file path matches any of the defined patterns. If a match is found, the file path is considered valid. The result is logged using gs.info().

 

Regards,

Shravan.
Please mark this as helpful and correct answer, if this helps you

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi Shravan,

 

I modified and tried but it is not working could you please help!

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
var patterns = [
  /^E:\[a-zA-Z0-9_]+$/,
  /^\[a-zA-Z0-9_]+(\[a-zA-Z0-9_]+)+$/,
  
];

var filePaths =  g_form.getValue('folder_path');
alert(filePaths);
for (var i = 0; i < patterns.length; i++) {
  var pattern = patterns[i];
  if (pattern.test(filePaths)) {
    alert("matched");
  } else {
   alert("missmatched");
  }
}

}

 

It should accept the below example patterns

E:\Test

/data1/data2/data2/data2/ 

Hi @William08 ,

 

can you please update as below

 

 var patterns = [
        /^E:\\[a-zA-Z0-9_]+$/,
        /^\/[a-zA-Z0-9_]+(\/[a-zA-Z0-9_]+)+\/$/,
    ];

 

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Hi @Sai Shravan 

 

It is throwing alert as missmatched.