无法在C驱动器下创建新目录并复制到该目录

时间:2017-07-10 14:05:18

标签: batch-file cmd mkdir xcopy

我正在编写批处理文件来为项目设置环境,我需要直接在C:驱动器下创建一个新目录。这是我写的。首先,我检查文件是否作为Admin执行,然后检查文件是否已经存在,如果它没有,则创建它并转到它。然后我做其他事情。


  :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"

2 个答案:

答案 0 :(得分:0)

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

另外,尝试查看是否可以在C:\ first下创建一个test子目录。如果不允许,可能是C:\

的目录所有权问题

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

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

答案 1 :(得分:0)

借助BoogieMan2718Mofi所说的内容,我修复了我的批处理文件。这是固定的版本:

: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. 
)

感谢您的帮助。