• 0

[MS-DOS Batch] Automatically determine if drive is already mapped


Question

I didn't know if I should start this topic under "Programming" but it seems to be the only place to put it.

I want to run a batch file that will automatically map a network drive before it continues the script. The problem I have is that if for some reason the drive is already mapped, or the batch file has been run twice in one session, the "net use" command returns an error. The script still works in the end, but I would like to bypass the "net use" command altogether if the drive is already mapped.

I've been digging through the net trying to find anything that can help me out, but to no avail. The closest thing I can find is just typing "net use" can tell you if the drive is already mapped or not, but I can't take any one piece of that result and use it in my script.

Does anyone have any suggestions? My purpose here is to write an IF statement that determines:

IF drive X: is already mapped, then skip net use command and proceed with the remainder of the script.

ELSE, run net use command and proceed with the remainder of the script.

I figured ERRORLEVEL could come in handy, but that only works after I get the error that I'm trying to avoid in the first place.

Thanks!

19 answers to this question

Recommended Posts

  • 0

I would be careful with what you are checking. If the user mapped a different drive than the one that is supposed to be there, it may see a drive mapped and skip the mapping.

What we have done is unmap and then map the drive in the same script. This will ensure that the correct drives are mapped to the desired letter every time.

  • 0

You could always look for a specific file to that share, which should eliminate the chance of having the wrong share mapped.

Unmapping the share could cause issues if other processes are using the same share.

  • 0
  t_r_nelson said:
I would be careful with what you are checking. If the user mapped a different drive than the one that is supposed to be there, it may see a drive mapped and skip the mapping.

What we have done is unmap and then map the drive in the same script. This will ensure that the correct drives are mapped to the desired letter every time.

Very true. I thought about that but I realized that this is happening in a fairly controlled environment... but I suppose it would be good to do. The only downside is if I try to unmap it while another resource is using the same drive to do something, causing other errors in the process.

  Joe USer said:
How about using IF EXIST for a file on the mapped drive?

Asssuming W: is your mapped drive.

IF NOT EXIST W:\. netuse..etc..

I might give that a try, thanks. I suppose maybe I can merge these two suggestions together? To unmap a drive you have to make sure the drive is mapped in the first place otherwise another error will appear. *sigh*

Also, thanks for your speedy replies.

EDIT:

  Joe USer said:
You could always look for a specific file to that share, which should eliminate the chance of having the wrong share mapped.

Unmapping the share could cause issues if other processes are using the same share.

Yes exactly. If I look for a specific file on that share, I wouldn't need to unmap at all.

I'll give that a try now and report back.

  • 0

I made an assumption that mappings are needed, can you run the process using UNC without having to map to a specific drive letter?

  t_r_nelson said:
Good point, Joe. I was just considering the case of the script running at login.

I learned that the hard way, with WFW 3.11.

Multithreaded batch processing, completely crazy, but it worked.

  • 0

Alright I just gave this a try and it seems to work perfectly fine so far. I'll keep testing to make sure.

@echo off

echo --------------------------------------------------

echo Now running database maintenance. Please wait...

echo IMPORTANT: DO NOT TOUCH THE COMPUTER DURING THIS PROCESS

echo --------------------------------------------------

IF EXIST \\domain and filename here.exe (

echo The drive is already mapped.

GOTO SKIPPED

) ELSE (

echo The drive has not yet been mapped.

net use x: \\domain and share here Password /user:domain\user

)

:SKIPPED

[command that I need to run regardless]

echo --------------------------------------------------

echo Maintenance complete.

echo --------------------------------------------------

pause

Thanks for the help guys! I'll post here again if something comes up. The only thing I'm uncertain about is if it's okay that I don't specify X: in the EXISTS statement.

  • 0
  jake1eye said:
Here is a link to a HTA from the Hey Scripting Guys that uses a HTA to list available drive letters and how to map that drive.

Hey Scripting Guys

Thanks, but that seems to be for VBScript. I'm just working with command line batch.

  • 0
  Xtreme $niper said:
Alright I just gave this a try and it seems to work perfectly fine so far. I'll keep testing to make sure.

@echo off

echo --------------------------------------------------

echo Now running database maintenance. Please wait...

echo IMPORTANT: DO NOT TOUCH THE COMPUTER DURING THIS PROCESS

echo --------------------------------------------------

IF EXIST \\domain and filename here.exe (

echo The drive is already mapped.

GOTO SKIPPED

) ELSE (

echo The drive has not yet been mapped.

net use x: \\domain and share here Password /user:domain\user

)

:SKIPPED

[command that I need to run regardless]

echo --------------------------------------------------

echo Maintenance complete.

echo --------------------------------------------------

pause

Thanks for the help guys! I'll post here again if something comes up. The only thing I'm uncertain about is if it's okay that I don't specify X: in the EXISTS statement.

IF EXIST \\domain and filename here.exe (

Once you use a UNC connection you're going to be logged into the server as the current user. If the users are different, then you're going to have a problem. You should specify the X: in the if exists.

  • 0

Hmm alright, so then should I do:

IF EXIST X:\\address\file.exe

or

IF EXIST X:\file.exe ?

For some reason I'm tempted to use the second one but the first one seems more technically correct to me...

  • 0
  Xtreme $niper said:
Hmm alright, so then should I do:

IF EXIST X:\\address\file.exe

or

IF EXIST X:\file.exe ?

For some reason I'm tempted to use the second one but the first one seems more technically correct to me...

X:\file.exe is what you want, (assuming it's in the root of the share)

  • 0

Alright I thought so (yes it's at the root). Thanks.

But now I have another problem. I want to schedule this using the "Schedule Task" app on WinXP. The only problem is the computers here are required to press CTRL+ALT+DEL before you can log in (secure logon). For some reason the batch script fails to do any mapping when it is run at the "press CTRL+ALT+DEL to log in" screen.

However, if I do press CTRL+ALT+DEL and go to the actual log in screen (still not logged in) the script works fine. Any ideas why, or how to get around this?

EDIT: I ran into this idea here, http://joedix.com/tech_help/2006/07/schedu...running-wo.html but I think that would be a pretty big security risk, no?

Edited by Xtreme $niper
  • 0
  Xtreme $niper said:
Alright I thought so (yes it's at the root). Thanks.

But now I have another problem. I want to schedule this using the "Schedule Task" app on WinXP. The only problem is the computers here are required to press CTRL+ALT+DEL before you can log in (secure logon). For some reason the batch script fails to do any mapping when it is run at the "press CTRL+ALT+DEL to log in" screen.

However, if I do press CTRL+ALT+DEL and go to the actual log in screen (still not logged in) the script works fine. Any ideas why, or how to get around this?

EDIT: I ran into this idea here, http://joedix.com/tech_help/2006/07/schedu...running-wo.html but I think that would be a pretty big security risk, no?

What account are you running the task under? Does it have a password?

  • 0

I'd be running this task under non-administrator accounts I believe. All accounts have to have a password, and every user has to press the CTRL+ALT+DEL combo before logging in.

I'm not sure why but it seems drive mapping doesn't work if you don't press CTRL+ALT+DEL.

EDIT: I just did another round of testing. The drive mapping actually doesn't work in ANY form if your account is locked. I previously thought as long as you hit the safe logon combo first you'd be okay, but apparently I was wrong.

As long as the system is at the login screen (even though the account itself is logged in) the application that needs to be run from the network share won't run, causing the script to fail.

Any ideas?

Edited by Xtreme $niper
  • 0

Ok, that's a little strange, I run batches via the task shceduler in Windows 2000 all the time.

Are you on a domain? What OS/SP are you using (XP sp2?)?

Have you tried using an admin account?

Are you using the task scheduler or the AT command?

Also, try this, get a copy of whoami.exe from the MS resource kit and pipe the output to a local file, see if it's using the correct account.

  • 0

Well I guess it's more complicated than just that... I forgot to mention I'm using an application called TestPlanner that automates some tasks for you by controlling the keyboard and mouse to accomplish "macro" like tasks.

The script itself actually opens and runs, but the problem is once the TestPlanner app runs, it's supposed to do a few things. One of which is to open the run window from the start menu and run another application... but for some reason the keys are never mapped to the text field within the run window and so the script fails.

So now I realize your ability to help me out can easily end here, because you probably have no idea what this TestPlanner application is...

The problem is that I am just an employee in a corporation, so there is no chance of me getting admin access. I don't think that's the problem though. I think that because the computer is in a locked state, the text field in the run command window can't be selected and so the script fails when it is trying to enter text into a field that cannot be selected. It looks like I just can't get a fix for that.

  • 0
  Xtreme $niper said:
Well I guess it's more complicated than just that... I forgot to mention I'm using an application called TestPlanner that automates some tasks for you by controlling the keyboard and mouse to accomplish "macro" like tasks.

The script itself actually opens and runs, but the problem is once the TestPlanner app runs, it's supposed to do a few things. One of which is to open the run window from the start menu and run another application... but for some reason the keys are never mapped to the text field within the run window and so the script fails.

So now I realize your ability to help me out can easily end here, because you probably have no idea what this TestPlanner application is...

The problem is that I am just an employee in a corporation, so there is no chance of me getting admin access. I don't think that's the problem though. I think that because the computer is in a locked state, the text field in the run command window can't be selected and so the script fails when it is trying to enter text into a field that cannot be selected. It looks like I just can't get a fix for that.

There's one last chance, you can try this, but I don't think it will work, see if the TestPlanner has the ability to 'allow service to interact with desktop'. In windows 2000, you could set this in the services control panel.

Honestly, I don't think that would solve your problem though. Since it's a macro program it most likely needs an unlocked console to work.

  • 0
  Joe USer said:
There's one last chance, you can try this, but I don't think it will work, see if the TestPlanner has the ability to 'allow service to interact with desktop'. In windows 2000, you could set this in the services control panel.

Honestly, I don't think that would solve your problem though. Since it's a macro program it most likely needs an unlocked console to work.

I just took a look at the program, and I unfortunately have found no such feature. Thanks for the suggestion though.

The funny thing is that although it can't select a text field to enter text into as part of the macro, it still successfully opens the Run command window via the "Windows key + R" shortcut.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • My home system is a 5800X (upgraded from 2700X) with a 7800 XT. I can't comment as to why you feel so strongly about the differences, but I have used both Windows 10 and 11 for literally thousands of hours each; I'd guess over 10,000 hours on Windows 10 and maybe half that on Windows 11. Earlier builds of Windows 11 had some pretty big UI lag issues, which did annoy me, but not enough to go back to Windows 10. 23H2 and forward have corrected all those issues for me and I have no complaints at all at this time. Let me clarify what I meant by "Windows 11 runs perfectly fine." I don't mean it is merely acceptable; I mean that I don't perceive a difference between it and Windows 10. I chose the word "fine" because I don't believe that either 10 or 11 are perfect in that area and both have the rare UI hiccup, but in my experience, their responsiveness is at the same level.
    • WinToUSB 9.9 by Razvan Serea WinToUSB allows you to install and run a fully-functional Windows on external hard drive, USB flash drive or Thunderbolt drive. It is so easy and efficient, with just 3 steps and a few minutes, you can create your first portable Windows 11/10/8/7 or Windows Server directly from an ISO, WIM, ESD, SWM, VHD, VHDX file or CD/DVD drive, or you can clone currently running Windows installation to USB or Thunderbolt drive as portable Windows. WinToUSB also supports creating Windows installation USB drive from Windows 11/10/8/7 and Windows Server installation ISO, with it you can install Windows from the USB drive easily. Note: The WinToUSB Free Edition is solely intended for non-commercial, private, and personal use on home computers. It should be noted that technical support is not available for the free edition. Use of WinToUSB Free Edition within any organization or for commercial purpose is strictly prohibited. WinToUSB key features include: Creation of Windows To Go from ISO, WIM, ESD, SWM, VHD(X) or DVD drive.Improved Clone Windows 11/10/8/7 to USB/Thunderbolt drive as portable Windows. Creation of Windows To Go on Non-Certified Windows To Go USB drive. Encrypt Windows To Go with BitLocker to keep your data safe. Creation of Windows installation and bootable WinPE USB drive with BIOS & UEFI support. Download Official Windows 11/10/8.1 ISO file from Microsoft. Use any edition of Windows 11/10/8/7 and Windows Server 2022/2019/2016/2012/2010 to create Windows To Go USB drive. Windows To Go (Portable Windows) Creator WinToUSB allows you to install & run fully-functional Windows on an external HDD/SSD, USB flash drive or Thunderbolt drive, which means you can carry the portable Windows drive to anywhere and use it on any computer. Faster installation and cloning speed compared to competing products Support any edition of Windows 11/10/8/7 and Windows Server Creation of Windows To Go from ISO, WIM, ESD, SWM, VHD(X) or CD/DVD drive Clone currently running Windows to USB/Thunderbolt drive Creation of Windows To Go on Non-Certified Windows To Go drive Create BitLocker encrypted Windows To Go Workspace Create portable Windows for Intel-based Mac computers Support for creating VHD(X)-based Windows To Go Windows Installation USB Creator WinToUSB releases a feature called "Windows Installation USB Creator" which allows you to create a Windows installation USB drive from a Windows 11/10/8/7/vista/2022/2019/2016/2012/2008 installation ISO file with a few simple steps, with this feature you can create a bootable Windows installation USB drive to install Windows on both Traditional BIOS and UEFI computers by using the same USB drive. Bypass Windows 11 system requirements (TPM 2.0, Secure Boot, Minimum hardware and Microsoft account) Install Windows on both BIOS and UEFI computers by using the same USB drive Windows PE Bootable USB Creator This feature allows you to create a bootable Windows PE USB drive, it can help you transfer the contents of a Windows PE ISO file to a USB drive and make the USB drive bootable, and this feature supports the creation of a bootable WinPE USB driver that supports both Traditional BIOS and UEFI computers. WinToUSB 9.9 changelog: Added option to disable BitLocker automatic drive encryption when creating Windows installation USBs Fixed bug: setup.exe cannot bypass the Windows 11 system requirements Fixed bug: Cloned Windows ARM64 cannot start properly Fix other minor bugs Download: WinToUSB 9.9.0 | 28.7 MB (Freeware) Links: Home Page | Free vs Pro Comparison | Screnshots Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Maybe you don't realize this...but everything you said agreed with me. Yes, many tech outlets reported on Ryzen 9000 issues prior to 24H2, which I already addressed, and as I already said, that issue only existed for a few short months. Ryzen 9000 was released the same quarter of 2024 as 24H2. So again...months, not years. I also already said 24H2 showed some minor improvements on older Ryzen CPU. The article you posted agrees with me, and even says the improvements were only 3-5%, which is even more petty an amount than I estimated. If you want to fuss on the 3-5% numbers, then yes, I will grant you that was an issue for an extended amount of time. In my opinion, that is such a small amount it isn't worth fussing over, but you are welcome to a different option. However, if that was your point, then you didn't make that point in good faith, because you highlighted Ryzen 9000 so much, which had a FAR bigger and FAR shorter issue, it's really a very different conversation.
    • The vast majority of users do not care which iOS version they're using. They don't even know or bother updating to the latest version, unless they see the prompt. The version numbers is more for the power users and I don't think Apple renamed their OS for them.
  • Recent Achievements

    • Week One Done
      abortretryfail earned a badge
      Week One Done
    • First Post
      Mr bot earned a badge
      First Post
    • First Post
      Bkl211 earned a badge
      First Post
    • One Year In
      Mido gaber earned a badge
      One Year In
    • One Year In
      Vladimir Migunov earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      495
    2. 2
      snowy owl
      251
    3. 3
      +FloatingFatMan
      251
    4. 4
      ATLien_0
      228
    5. 5
      +Edouard
      191
  • Tell a friend

    Love Neowin? Tell a friend!