- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 02:28 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 07:25 AM
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
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 03:24 AM
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:
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
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 05:54 AM
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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 06:04 AM - edited 06-08-2023 06:08 AM
Hi @William08 ,
can you please update as below
var patterns = [
/^E:\\[a-zA-Z0-9_]+$/,
/^\/[a-zA-Z0-9_]+(\/[a-zA-Z0-9_]+)+\/$/,
];
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 06:09 AM