无法在 C 盘下创建新目录并复制到该目录

Cannot Create New Directory Under C Drive and copy to it

我正在编写一个批处理文件来为项目设置环境,我需要直接在C: 盘下创建一个新目录。这是我写的。首先,我检查文件是否以管理员身份执行,然后我检查文件是否已经存在,如果不存在,则创建它并转到它。那我做点其他的。


  :check_Permissions
    echo Administrative permissons required. Detecting permissions...

    net session >nul 2>&1
    if %errorLevel% == 0 (
      echo Success : Administrative permissions confirmed.
    ) else (
      echo Failure : Current permissions inadequate.
      echo Please, run this file as administrator.
    )

    pause >nul

  if not exist "\C:\NewDir" (
    echo NewDir directory will be created under C: drive. 
    mkdir "\C:\NewDir"
    echo Created NewDir Folder under C: drive.
  ) else (
    echo Directory already exists.
    cd C:\NewDir
  )
  if not exist "\C:\NewDir" ( 
    echo Directory was not created. 
  )
  :next

我不知道为什么没有创建目录,因为文件是 运行 作为管理员,不可能是因为缺少权限...

我还需要将另一台服务器上的远程共享目录的内容复制到新建的文件夹中。 XCOPY 不工作。这是代码:


  XCOPY /s "\remoteserver\directory\sub-dir\directory-to-copy" "C:\NewDir"

确保批处理文件已保存并 运行 在 C:\ 目录中。

另外,先试试看能不能在C:\下建立一个test子目录。如果不允许,则可能是 C:\

的目录所有权有问题

关于XCOPY,请确保您有从远程共享目录复制的权限。

您是否尝试过先在 CMD 中逐步测试它?

BoogieMan2718 and Mofi 所说的帮助下,我修复了我的批处理文件。这是固定版本:

:check_Permissions
  echo Administrative permissons required. Detecting permissions...

  net session >nul 2>&1
  if %errorLevel% == 0 (
    echo Success : Administrative permissions confirmed.
  ) else (
    echo Failure : Current permissions inadequate.
    echo Please, run this file as administrator.
  )

  pause >nul

cd C:\
if %cd% == "C:\" ( echo In C:\ drive. )

if not exist "C:\NewDir" (
  echo NewDir directory will be created under C: drive. 
  mkdir "C:\NewDir"
  echo Created NewDir Folder under C: drive.
  echo NewDir will now be copied to your computer. This action may take a few minutes...
  ROBOCOPY /E /V /Z "\remoteServer\pathToDirectoryToCopy" "C:\NewDir"
) else (
  echo Directory already exists.
)
if not exist "C:\NewDir" ( 
  echo Directory was not created. 
)

感谢您的帮助。