You are not logged in.

#1 18 Nov 2015 08:44

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Filter text file and set variables

I have a code that outputs the contents of net use like so:

net use >> _%computername%_maps.txt

In this case the file is _CD0-PL0001_maps.txt

The output of the command is:

New connections will be remembered.


Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           R:        \\nas-1\Marketing Production 
                                                Microsoft Windows Network
Unavailable  S:        \\192.168.1.5\Share1     Microsoft Windows Network
OK           T:        \\192.168.1.10\Downloads_CP
                                                Microsoft Windows Network
OK           U:        \\192.168.1.50\Share1_TEST
                                                Microsoft Windows Network
OK           V:        \\192.168.1.42\Share2
                                                Microsoft Windows Network
OK           W:        \\192.168.1.3\Share7 
                                                Microsoft Windows Network
OK           X:        \\192.168.1.11\Share8 Microsoft Windows Network
OK           Y:        \\192.168.1.14\Share9
                                                Microsoft Windows Network
OK           Z:        \\192.168.1.16\Share9 
                                                Microsoft Windows Network
The command completed successfully.

I have made the following script in order to interpret the data and mount every drive automatically. The idea here is to process the text file and automatically mount every folder with its original letter, while excluding all other useless data, especially those "Microsoft Windows Network" which are also listed in the output of the script.

@echo off
setlocal enabledelayedexpansion

for /f "skip=6 tokens=* delims= " %%a in ('type _CD0-PL0001_maps.txt') do (
  @for /f "tokens=2 delims= " %%b in ('echo %%a') do set letter=%%b
    @for /f "tokens=3 delims= " %%c in ('echo %%a') do set server=%%c
      call :mountdrive !letter! !server!
)

:mountdrive
net use %1 %2

Thank you

Offline

#2 30 Nov 2015 13:27

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Filter text file and set variables

No one? Come on guys. I just need a script to mount every drive share listed in the second snippet using the code from the third snippet.

Offline

#3 30 Nov 2015 13:34

Shadow Thief
Member
Registered: 12 Jul 2012
Posts: 205

Re: Filter text file and set variables

I'm not seeing a question here; how does the code you've provided not already do what you're asking?

Last edited by Shadow Thief (30 Nov 2015 13:35)

Offline

#4 30 Nov 2015 13:49

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Filter text file and set variables

The script loops through all lines of text. As you can see above, some lines contain only 'Microsoft Windows Network' and the script tries to mount this as well, and outputs an error. It should exclude those somehow.

Offline

#5 30 Nov 2015 14:51

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Filter text file and set variables

Ah see from your wording, I thought you were just presenting your work, rather than soliciting advice.

There are a number of ways to skin this cat.  Here's a slightly hacky one to try:

for /f "skip=6 tokens=* delims= " %%a in ('type _CD0-PL0001_maps.txt ^| find "\\"') do (

FYI - You may also experience issues if you have any mappings which aren't associated with drive letter.  You could try this (in addition to the above):

:mountdrive
if /i "%~d1" NEQ "%~1" net use %1 & goto :EOF
net use %1 %2

Last edited by bluesxman (30 Nov 2015 14:52)


cmd | *sh | ruby | chef

Offline

#6 30 Nov 2015 15:38

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Filter text file and set variables

It works but for some reason it only lists drives X: Y: and Z:.

I used the following to test:

:mountdrive
if /i "%~d1" NEQ "%~1" net use %1 & goto :eof
echo net use %1 %2
goto :eof

Offline

#7 01 Dec 2015 09:14

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Filter text file and set variables

There were two parts to my post.  The one you've mentioned was a side issue.  Can you post the code you're running and the file you are processing?


cmd | *sh | ruby | chef

Offline

#8 02 Dec 2015 07:35

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Filter text file and set variables

I used this with the example file from the first post:

pushd %~dp0
setlocal enabledelayedexpansion

for /f "skip=6 tokens=* delims= " %%a in ('type _CD0-PL0001_maps.txt ^| find "\\"') do (
  @for /f "tokens=2 delims= " %%b in ('echo %%a') do set letter=%%b
    @for /f "tokens=3 delims= " %%c in ('echo %%a') do set server=%%c
      call :mountdrive !letter! !server!
)

:mountdrive
if /i "%~d1" NEQ "%~1" net use %1 & goto :eof
echo net use %1 %2
goto :eof

Last edited by Eehixohw (02 Dec 2015 07:36)

Offline

#9 03 Dec 2015 11:51

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Filter text file and set variables

Sorry that was an error in my code, you need to remove the "skip=6".  You're pre-filtering the net use list for UNC paths (find "\\") so the header messages are no longer in the output.  Your "skip=6" will be jumping over mappings instead of the header.

Last edited by bluesxman (03 Dec 2015 11:53)


cmd | *sh | ruby | chef

Offline

#10 04 Dec 2015 07:55

Eehixohw
Member
Registered: 25 Sep 2011
Posts: 40

Re: Filter text file and set variables

Thank you.

Offline

Board footer

Powered by