Solved

# Need a script to provide an output of command to input of a command

Posted on 2011-10-04
365 Views
I am looking for a script or a batch file:
The real requirement is that I will run a command ,net use * /d > out.txt. This will result me prompting to say Y /N along with list of mapped drives and their respective paths. I need to use the content of the out.txt(which will contain the mapped drives and their respective paths) to map the same shares again. Any help or ideas are highly appreciated. Thanks
0
Question by:jmohan0302

LVL 51

Expert Comment

Give this a try, it will iterate through each share, disconnect it, and then reconnect it.  It also displays the shares as it goes, you may want to delete that once it works well.

@echo off
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv') do (
net use %%A /DELETE /Y
net use %%A "%%~B" /PERSISTENT:YES
echo Remapped drive [%%A] to share [%%~B]
)

~bp
0

Author Comment

Hi Could you please explain me the script as I need to know do we need to give an input file. Thanks
0

LVL 51

Expert Comment

No input file.  The FOR loop processes the output of the WMIC command, which will list all current mapped drives (same basic info as NET USE but in a format that easier to work with in the script).  Inside the FOR loop, the loop variable %%A will be assigned to the drive letter, and %%B will be assigned to the sharename that drive was connected to.  We then delete the current share, and remap it.

~bp
0

Author Comment

Hi ,
I saved it in a .bat file and when I tried to run I am getting the following error:

C:\Users\jayabalm\Desktop>share.bat
Invalid XSL format (or) file name.

C:\Users\jayabalm\Desktop>

The share.bat contains your code. Kindly help. Thanks
0

Author Comment

Hi I found that the code in the notepad is enabled for Wordwrap, that's y I am getting the error. Now I removed that and its working fine. But I could not really see what's happening. Is there any way to create a log file of what it is doing? or what are all map drives that is disconnecting and re-mapping? Thanks
0

LVL 51

Expert Comment

Okay, this should fix that.

@echo off
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv.xsl') do (
net use %%A /DELETE /Y
net use %%A "%%~B" /PERSISTENT:YES
echo Remapped drive [%%A] to share [%%~B]
)

~bp
0

LVL 51

Expert Comment

Okay, in that case stick with the original working file.

It does log all the reconnects done, try adding a PAUSE line to the end if you aren't seeing those before the window closes.

If you want to log them then you could adjust the ECHO line to:

echo Remapped drive [%%A] to share [%%~B] >>"c:\dir\logfile.txt"

~bp
0

Author Comment

Hi I tried, I am not getting the log file created. Could you adjust the code and give it to me so that I can understand wer I am  going wrong. Thanks
0

LVL 51

Expert Comment

Actually, it looks like you named the BAT file SHARE.BAT.  That's a bad choice since there is a windows built in program SHARE.EXE that will execute instead.  Rename the BAT to something that doesn't already exist, like maybe REMAP.BAT.

~bp
0

Author Comment

Ok. Its working but I could not get the log files. So I am not knowing what is happening? Help me to get the log file. THanks
0

LVL 51

Expert Comment

Okay, here's a version that should log to a file, set the name of the file for logging near the top as you need.  I also added a few comments to help further explain the script, hope this helps.

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
REM Inside loop %%A will be the drive letter, %%B will be the sharename it is mapped to
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv') do (

REM Log a message about remapping this share
echo ***** Remapping drive [%%A] to share [%%~B] *****

REM First, delete the existing mapping for this drive
net use %%A /DELETE /Y

REM Then remap using the same drive and sharename
net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%"

~bp
0

Author Comment

Hi Thanks. But I am getting the following error while executing:

C:\Users\jayabalm\Desktop>map1.bat
Invalid XSL format (or) file name.

0

LVL 51

Expert Comment

Could that be the same problem you mentioned earlier:

"Hi I found that the code in the notepad is enabled for Wordwrap, that's y I am getting the error. Now I removed that and its working fine."

If not then try changing the

/format:csv

to

/format:csv.xsl

~bp
0

Author Comment

Hi I checked for the Wordwrap and that is not enabled and I tried changing the /format:csv to /format:csv.xsl but still getting the same error:

The code in the map1.bat is :

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
for /F "skip=2 tokens=2,3 delims=," %%A in ('wmic netuse get remotepath^,localname^,status /format:csv.xsl') do (
REM Log a message about remapping this share
echo ***** Remapping drive [%%A] to share [%%~B] *****

REM First, delete the existing mapping for this drive
net use %%A /DELETE /Y

REM Then remap using the same drive and sharename
net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%

Please check did I have missed some thing while pasting. I really appreciate your help and inputs. Thanks
0

LVL 51

Expert Comment

Can you rename it to a TXT file and upload it as an attachment here please.

~bp
0

Author Comment

Hi I have enclosed the file:

Map.txt
0

LVL 51

Expert Comment

It looks like you are missing a double quote off the end of the last line.

)>>"%LogFile%"

~bp
0

Author Comment

Still getting the same error.
0

LVL 51

Expert Comment

From a DOS window command prompt, can you try each of these commands and let me know what they say:

wmic netuse get remotepath,localname,status /format:csv

wmic netuse get remotepath,localname,status /format:csv.xsl

~bp

0

Author Comment

C:\> wmic netuse get remotepath,localname,status /format:csv
Invalid XSL format (or) file name.

C:\> wmic netuse get remotepath,localname,status /format:csv.xsl
Invalid XSL format (or) file name.
0

Author Comment

Hi I am checking this in Windows7
0

Author Comment

Hi Bill,

I tried it on a WIN2k3 the command is working fine. I think the switch /Format.CSV is not supporting in WINdows 7. Is there any way to get rid of this error in Windows7
0

LVL 51

Expert Comment

Yes, there were some issues with the format:csv in Win7, but typically the additon of the .XSL extension resolves that.  Oddly in your case it doesn't.

Okay, we'll need to try and work around that.  If you just do the following can you paste up the output please.

wmic netuse get remotepath,localname

~bp
0

LVL 51

Expert Comment

At the risk of backing up a bit, what are you trying to accomplish by remapping the existing mapped drives to the same share?

~bp
0

Author Comment

Yes I need to disconnect the shared drives and has to remap them again

But in Windows7 If I delete the /Fomat switch it is working in command line.

But when I add the same line the batch file is executing but the log file is not created. However the same is working fine WINXP and WIN2k3. Its really strange.
0

LVL 51

Expert Comment

Okay, I think there may be some different issues with this approach, but let's give it a try.

@echo off

REM Define filename to log activity to
set LogFile=c:\ee\ee27379643.txt

REM Delete logfile if it exists
if exist "%LogFile%" del "%LogFile%"

REM Loop through all current defined mapped drives
REM Inside loop %%A will be the drive letter, %%B will be the sharename it is mapped to
for /F "skip=1 tokens=1*" %%A in ('wmic netuse get remotepath^,localname') do (

REM Log a message about remapping this share
echo ***** Remapping drive [%%A] to share [%%~B] *****

REM First, delete the existing mapping for this drive
net use %%A /DELETE /Y

REM Then remap using the same drive and sharename
net use %%A "%%~B" /PERSISTENT:YES

)>>"%LogFile%"

~bp
0

LVL 51

Expert Comment

==> Yes I need to disconnect the shared drives and has to remap them again

What purpose does this serve?

~bp
0

Author Comment

I am writting a script. The real problem is AD account locking out. We are getting frequent account lockout issues. We found that the accounts were getting locked out for a lot of reasons out of which is Mapping of shared drives and we found many reasons for that. So I am writing a script which will resolves the lockout issues for all the reasons.I got strucked up in the middle for remapping the same drives what user has mapped earlier. You really helped me in doing that.

-MJ
0

Author Comment

Hi In the new script it is disconnecting the drives but not remapping
0

LVL 51

Expert Comment

Yeah, the problem with WMIC without the CSV is that it has a few funny characters in it's output that confuse BAT scripts.

Looking for a work around...

~bp
0

LVL 51

Expert Comment

At a command line, does the following work for you without error?

wmic netuse get localname,remotepath /format:list

~bp
0

LVL 51

Accepted Solution

Also, as one other test, can you place this file in the folder that is the current directory when you start a DOS command line session, and then try both of these commands again:

wmic netuse get remotepath,localname,status /format:csv

wmic netuse get remotepath,localname,status /format:csv.xsl


csv.xsl
0

Author Closing Comment

Good one
0

## Join & Write a Comment Already a member? Login.

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

#### 732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!