2015-12-20 86 views
0

我想要做一个小批处理脚本读取.txt文件中的用户名列表,并且如果它与变量%username%匹配,它将驱动器。映射网络驱动器检查.txt文件中的用户列表

这里是我的脚本:

for /F "tokens=*" %%a in (c:\Users\johndoe\Desktop\myfile.txt) do if /i "%%a"=="%username%" goto Label1 

:Label1 
net use J: \\johndoe-pc\Users\johndoe\Desktop\Test 

的问题是,我可以写在文本文件中的一切,将执行:Label1

只有当%username%变量与列表匹配时,我才要它执行Label1

+4

当'为/ F'循环结束,在那里你觉得你的脚本将继续...? – aschipfl

回答

3

Cmd逐行处理批处理文件。在for /f ...行之后,没有什么可以阻止这个处理(如goto :eof),所以它继续下一行(这只是一个标签,因此它被忽略)。下一行要处理的是net use...

还有一个更简单的方法做你想要什么:

find "%username%" "c:\Users\johndoe\Desktop\myfile.txt" >nul 
if %errorlevel%==0 net use J: \\johndoe-pc\Users\johndoe\Desktop\Test 
+0

谢谢你这种方式正常工作。 – Khromfire

相关问题