Question

I am trying to create folder when it does not exist. If folder is exist it will skip and continue to create the next folder.

Which part is wrong in this following code,

Error

Micrsoft Jscript runtime error: File already exists

Code

   function CreateFolder(fldr)
    {
       if (fso.FolderExists(fldr)){
          return;
          }
       else 
        fso.CreateFolder("C:\\"+ fldr);
    }
Was it helpful?

Solution

If fldr doesn't include the drive letter, FolderExists looks for this folder in the current wording directory. But your code then creates this folder in C:\. Most likely, the error occurs because there's no folder with this name in the current working directory, but it exists in C:\.

Your code should probably be either

function CreateFolder(fldr)
{
  if (! fso.FolderExists(fldr))
    fso.CreateFolder(fldr);
}

or

function CreateFolder(fldr)
{
  var path = fso.BuildPath("C:", fldr);

  if (! fso.FolderExists(path))
    fso.CreateFolder(path);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top