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

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
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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 Commented:
Hi Could you please explain me the script as I need to know do we need to give an input file. Thanks
0
Commented:
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 Commented:
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 Commented:
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
Commented:
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
Commented:
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 Commented:
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
Commented:
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 Commented:
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
Commented:
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 Commented:
Hi Thanks. But I am getting the following error while executing:

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

0
Commented:
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 Commented:
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
Commented:
Can you rename it to a TXT file and upload it as an attachment here please.

~bp
0
Author Commented:
Hi I have enclosed the file:

Map.txt
0
Commented:
It looks like you are missing a double quote off the end of the last line.

)>>"%LogFile%"

~bp
0
Author Commented:
Still getting the same error.
0
Commented:
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 Commented:

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 Commented:
Hi I am checking this in Windows7
0
Author Commented:
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
Commented:
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
Commented:
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 Commented:
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
Commented:
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
Commented:
==> Yes I need to disconnect the shared drives and has to remap them again

What purpose does this serve?

~bp
0
Author Commented:
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 Commented:
Hi In the new script it is disconnecting the drives but not remapping
0
Commented:
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
Commented:
At a command line, does the following work for you without error?

wmic netuse get localname,remotepath /format:list

~bp
0
Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Good one
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.