Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
prevent app from creating windows system folders using regex?

I'm building a browser-based app which allows users to create folders with some .json files (not big deal). The thing is, the framework that I'm using (NW.js) doesn't appears to care about allowing users to create folders named "CON" or "NUL"; These are not supposed to be created, the files inside just vanish and it's somewhat difficult to delete the folder themselves.

I can no problem make something like this to prevent to happen:

var newFolder = "nul";
function checkFolderName(text) {
    switch(newFolder) {
    case "con":
        console.log("folder can't be created")
        break;

    case "nul":
        console.log("folder can't be created")
        break;

    // and so on... there's about 23 windows-reserved names that I could find

    default:
        console.log("folder can be created")
}}
checkFolderName(newFolder);

But I wonder if there is some easier way to check this through Regex/Javascript, or maybe some different approach to this idea.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here's a little cleaner way to write what you want.

Function renamed to imply that it takes a string and returns a boolean.

If the input string is invalid, returns false.

All banned names are grouped together in an easily updated list.

Validity is checked by if the list contains the name.

function folderNameIsValid (name) {
  let valid = false;
  if (!name || typeof(name) !== 'string') {
    return valid;
  }

  const bannedNames = [
    'con',
    'nul'
  ];

  if (!bannedNames.includes(name.toLowerCase())) {
    valid = true;
  }
  return valid;
}

if (folderNameIsValid('NUL')) {
  console.log('folder can be created');
} else {
  console.log('folder cannot be created');
}
about 4 years ago · Juan Pablo Isaza Report

0

If you want to use regex you can do it like this:

if (/^con|nul|abc|xyz$/.test(newFolder))
    console.log("folder can't be created");

The | character means 'or'. so the test function will return true if newFolder is "con" or "nul" or "abc" or "xyz"

The ^ and the $ sign are the beginning and the ending of the string.

If you want to know exactly what was the illegal part, you can use this:

var matching = newFolder.match(/^con|nul|abc|xyz$/);
if (matching) {
    console.log("folder can't be created, found " + matching[0]);
}

You can also use this notation of switch case:

switch (newFolder)
{
   case "con":
   case "nul":
   case "abc":
   case "xyz":
       console.log("folder can't be created");
       break;

   default: 
       console.log("folder can be created");
}
about 4 years ago · Juan Pablo Isaza Report

0

My friend could help me to do this with regex:

let valid = /^(con|prn|aux|nul|com|lpt)\d*$/gi.test(newFolder)
console.log(valid) // true or false

However, it's starts to get pretty clanky if you want to add more "banned names", so jaredcheeda's suggestion it's pretty handy in this case

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!