net use で生成されたテキスト ファイルを読み取る方法は、以下の画像のように、現在のマシンにマップされたネットワーク ドライブで構成される network_drive.txt と呼ばれます。
新しい接続が記憶されます。
Status --- Local --- Remote ------------------ Network
-------------------------------------------------------------------------------
OK ---------- H: ---- \\server\users\john -----
Microsoft Windows Network
OK ---------- Y: ---- \\server\e$\ --------------
Microsoft Windows Network
コマンドは正常に完了しました。
上記のファイルを読み取って、ステータスが OK の場合にのみ同じレター パスとパスでネットワーク ドライブを再度マップし、使用できないドライブを無視する方法は?
アップデート!
@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 Network」がレターおよびドライブパスと同じ行にあるため、レコード/行が削除されることがあります。
誰かがplsを助けることができますか?
アップデート。
for ループ内のトークンは net use コマンドの 2 番目と 3 番目の文字列のみを取得するため、findstr 行から Microsoft Windows Network を削除しました。
私はそれをテストしましたが、動作します。
また、他のコマンドを実行する前に、ファイルが存在するかどうかを確認するためだけに、2 行目に if exist コマンドを使用することをお勧めします。