-2

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 コマンドを使用することをお勧めします。

4

1 に答える 1

0
@ECHO OFF
SETLOCAL

FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"') DO ECHO NET use %%a: %%b
ECHO(======================
FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"'') DO FOR /f %%c IN ("%%b") DO ECHO NET use %%a: %%c
GOTO :eof

This should do what you appear to want.

You should replace type q20294868.txt with net use for your situation. q20294868.txt is simply a file I used to save your test data.

There are two separate methods here. The text is filtered first for lines containing \ and then for those /b beginning "OK"

I'm unsure whether the network name may potentially be included on a data line rather than on a line by itself, consequently I've devised the second method. The first is simpler, the second more robust.

Note that your original findstr would have eliminated any lines containing ANY of the individual words contained in the quotes - see findstr /? from the prompt for more information.

And of course, the resultant NET USE command is merely ECHOed to the screen, not executed.

于 2013-11-30T06:48:36.230 に答える