批处理脚本以自动映射网络驱动器,但无法检测到网络驱动器

时间:2019-01-18 06:54:43

标签: batch-file

我正在编写一个脚本来自动映射2个网络驱动器,使用相同的代码,但是相应地更改了IP地址,因此只能成功映射1个网络驱动器。

我尝试手动映射网络驱动器,并且它可以正常工作。

if EXIST \\172.168.101.187\smb (
set shareExistsTX=1
echo Sender Share exist. 
) else (
set shareExistsTX=0
echo Sender Share Dont exist. 

)

if EXIST Y:\ (
set driveExistsTX=1
echo Drive Y:/ Unavailable
) else (
set driveExistsTX=0
echo Drive Y:/ available
)


if %shareExistsTX%==1 if %driveExistsTX%==1 (
echo deleting network drive
net use /delete Y: /y
set driveExistsTX=0
)


if %shareExistsTX%==1 if not %driveExistsTX%==1 (echo mounting smbserver Sender
net use Y: \\172.168.101.187\smb /persistent:Yes /user:user password
)

set driveExistsTX=
set shareExistsTX=

if EXIST \\172.168.101.188\smb (
set shareExistsRX=1
echo Receiver Share Exist
) else (
set shareExistsRX=0
echo Receiver Share Dont exist. 

)

if EXIST Z:\ (
set driveExistsRX=1
echo Drive Z:/ Unavailable
) else (
set driveExistsRX=0
echo Drive Z:/ available
)


if %shareExistsRX%==1 if %driveExistsRX%==1 (
echo deleting network drive
net use /delete Z: /y
set driveExistsRX=0
)


if %shareExistsRX%==1 if not %driveExistsRX%==1 (
echo mounting smbserver Receiver
net use Z: \\172.168.101.188\smb /persistent:Yes /user:user password

)

set driveExistsRX=
set shareExistsRX=
pause

我应该同时映射两个网络驱动器,但是仅映射了172.168.101.187,而出现错误消息“不存在接收者共享”。这意味着该脚本无法检测到172.168.101.188的网络驱动器

1 个答案:

答案 0 :(得分:0)

要直接解决您的问题,请测试共享中项目的存在,而不是共享本身:

if EXIST \\172.168.101.188\smb\*.
if EXIST \\172.168.101.187\smb\*.

修改 但是,根据您的评论,文件夹中没有任何内容,因此让我们创建一些内容,对其进行测试然后再次将其删除。像这样:

type nul>\\172.168.101.188\smb\_tmp
if EXIST \\172.168.101.188\smb\*.
type nul>\\172.168.101.187\smb\_tmp
if EXIST \\172.168.101.187\smb\*.

然后在脚本中完成映射后,添加:

del /Q Y:\_tmp
del /Q Z:\_tmp

del /Q \\172.168.101.187\smb\_tmp >nul
del /Q \\172.168.101.188\smb\_tmp >nul

因此,要获取您实际的脚本并对其进行所有更改,我们将获得此脚本,它应该可以工作:

@echo off
type nul>\\172.168.101.187\smb\_tmp
type nul>\\172.168.101.188\smb\_tmp
if EXIST \\172.168.101.187\smb (
set shareExistsTX=1
echo Sender Share exist. 
) else (
set shareExistsTX=0
echo Sender Share Don't exist. 

)

if EXIST Y:\ (
set driveExistsTX=1
echo Drive Y:/ Unavailable
) else (
set driveExistsTX=0
echo Drive Y:/ available
)


if %shareExistsTX%==1 if %driveExistsTX%==1 (
echo deleting network drive
net use /delete Y: /y
set driveExistsTX=0
)


if %shareExistsTX%==1 if not %driveExistsTX%==1 (echo mounting smbserver Sender
net use Y: \\172.168.101.187\smb /persistent:Yes /user:user password
)

set driveExistsTX=
set shareExistsTX=

if EXIST \\172.168.101.188\smb (
set shareExistsRX=1
echo Receiver Share Exist
) else (
set shareExistsRX=0
echo Receiver Share Dont exist. 

)

if EXIST Z:\ (
set driveExistsRX=1
echo Drive Z:/ Unavailable
) else (
set driveExistsRX=0
echo Drive Z:/ available
)


if %shareExistsRX%==1 if %driveExistsRX%==1 (
echo deleting network drive
net use /delete Z: /y
set driveExistsRX=0
)


if %shareExistsRX%==1 if not %driveExistsRX%==1 (
echo mounting smbserver Receiver
net use Z: \\172.168.101.188\smb /persistent:Yes /user:user password

)
del /Q Y:\_tmp>nul
del /Q Z:\_tmp>nul
set driveExistsRX=
set shareExistsRX=
pause

那么也许这个版本略有不同?

我不需要检查驱动器是否存在,无论如何我都尝试将其映射:

@echo off
setlocal enabledelayedexpansion
set "shares=\\172.168.101.187\smb \\172.168.101.188\smb"

for %%i in (%shares%) do (
    type nul>%%i\_tmp
    if "%%i"=="\\ppsm202zatcwi\Mac" set "dr=Y:"
    if "%%i"=="\\172.168.101.188\smb" set "dr=Z:"
    if exist %%i\*. (
      net use !dr! %%i >nul 2>&1 && echo Mapped !dr! to %%i
      del /Q !dr!\_tmp>nul
      ) else (
        echo Sharename %%i not found unable to map !dr!
   )
 )
 pause