How to read net use generated text file and use the net use command to remap the network drive

3.3k views Asked by At

How to read net use generated text file lets called it network_drive.txt that consist of the current machine mapped network drive as image below:

New connections will be remembered.

Status ---      Local ---       Remote ------------------     Network

-------------------------------------------------------------------------------
OK ----------           H:   ----     \\server\users\john -----
                                                Microsoft Windows Network

OK ----------          Y:    ----     \\server\e$\ --------------
                                                Microsoft Windows Network

The command completed successfully.

How to read the file above to map the network drive again with the same Letter path and path only if the status is ok and ignore the unavailable one?

update!

@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.**

however.. in some cases, sometimes the "Microsoft Windows Network" is on the same line as the letter and Drive path and hence deleting the record/line.

can someone help pls?

Update.

I removed Microsoft Windows Network from the findstr line because the tokens in the for loop would only pick up the second and third strings for the net use command.

I have tested it and it works.

Also, it would be a good idea to use if exist command on the second line just to see if the file is exist before running the other commands.

1

There are 1 answers

0
Magoo On
@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.