質問

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);
    }
役に立ちましたか?

解決

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);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top