如何读取网络使用生成的文本文件,我们将其称为 network_drive.txt,其中包含当前机器映射的网络驱动器,如下图所示:
将记住新的连接。
Status --- Local --- Remote ------------------ Network
-------------------------------------------------------------------------------
OK ---------- H: ---- \\server\users\john -----
Microsoft Windows Network
OK ---------- Y: ---- \\server\e$\ --------------
Microsoft Windows Network
命令成功完成。
仅当状态正常并忽略不可用的情况下,如何读取上述文件以使用相同的字母路径和路径再次映射网络驱动器?
更新!
@echo off
set drive=\\server\users\john\
net use > %drive%\%USERNAME%_temp_Networkdrive.txt <-- generating net use file
for /f "skip=6 delims=*" %%a in (%drive%\%USERNAME%_temp_Networkdrive.txt) do (
echo %%a >>%drive%\%USERNAME%_del_Networkdrive.txt ) <-- deleting the first 6 lines
xcopy %drive%\%USERNAME%_del_Networkdrive.txt %drive%\%USERNAME%_Networkdrive.txt /y <-- make a new copy of the network drive file after deleting the lines
findstr /v "The command completed successfully." %drive%\%USERNAME%_del_Networkdrive.txt > %drive%\%USERNAME%_Networkdrive.txt <-- find the string and delete them and make a new copy of file with just the network drives
del %drive%\%USERNAME%_del_Networkdrive.txt /f /q
del %drive%\%USERNAME%_temp_Networkdrive.txt /f /q
for /f "tokens=2,3,4" %%a in (%drive%\%USERNAME%_Networkdrive.txt) do ( net use %%a %%b ) **<-- find the letter path and the drive path and map accordingly.**
但是.. 在某些情况下,有时“Microsoft Windows 网络”与字母和驱动器路径位于同一行,因此会删除记录/行。
有人可以帮忙吗?
更新。
我从 findstr 行中删除了 Microsoft Windows Network,因为 for 循环中的标记只会获取 net use 命令的第二个和第三个字符串。
我已经对其进行了测试,并且可以正常工作。
此外,最好在第二行使用 if exists 命令,以便在运行其他命令之前查看文件是否存在。