We help IT Professionals succeed at work.

Using DOS commands in a bat file

297 Views
Last Modified: 2010-08-05
Hi,

I have written a batch file and need to do the following...

1) Start a DOS window this I can do using "cmd /k"

2) In the DOS window I want to navigate to a network folder using a UNC path \\networkfolder\test

How can I do this...?

Comment
Watch Question

Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
Command prompts will not give you a unc prompt - you need to be on a drive letter.  You can reference UNCs for copying files/running programs, but if you need a UNC prompt, then you need to map the path to a drive letter.
Top Expert 2007

Commented:
You can dynamically map a drive in your batch processing like so:

@echo off

setlocal

set unc=\\networkfolder\test

set drive=
for /F "tokens=1,2" %%a in ('net use * "%unc%" 2^>NUL') do if "%%a"=="Drive" set drive=%%b

if "%drive%"=="" echo Could not map to %unc%&goto :EOF

echo Drive=%drive%
pause

net use "%drive%" /del

Commented:
You can also use the "net use" command to get there, you can do this by either mapping the UNC to a drive letter doing your work and then deleting the drive letter.  You can also just connect to the UNC and just keep on calling its name.  As examples I'll use the UNC of \\networkfolder\test and I'll start a file called program.bat and then another file fun.exe then I'll delete the connect as part of my house keeping.

example 1)
net use z: \\networkfolder\test
cd z:
call program.bat
fun
net use z: /d

example 2)
net use  \\networkfolder\test
call \\networkfolder\test\program.bat
\\networkfolder\test\fun
net use \\networkfolder\test /d
ISC

Author

Commented:
The following command does part of what I want, maps the next available drive...

I just now need to know which drive letter has been used..?

cmd /c net use * \\inag0010\vendor_apps\pls /persistent:no  
pause

Thanks Ian Carrick
Top Expert 2007

Commented:
If you use my script the drive will be placed in the %drive% environment variable.
Top Expert 2007

Commented:
@echo off

setlocal

set unc=\\inag0010\vendor_apps\pls

set drive=
for /F "tokens=1,2" %%a in ('net use * "%unc%" 2^>NUL') do if "%%a"=="Drive" set drive=%%b

if "%drive%"=="" echo Could not map to %unc%&goto :EOF

echo Drive=%drive%
pause

net use "%drive%" /del
ISC

Author

Commented:
Hi SteveGTR:

I have still one issue I do not know what drive letters will be available...

Is it possible to do something like...

set driveletterused = cmd /c net use * \\inag0010\vendor_apps\pls /persistent:no  




ISC

Author

Commented:
Once I have the mapped drive I want to do...
cd /c :driveletter
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.