How to Create Batch File to Mount Network Drive

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
I am using this command:
Code:
mount -u:root -p:[I][password][/I] \\216.17.1.125\srv\www\htdocs g:
at the command line to mount an NFS-enabled network drive from my Windows 7 machine.

I need to know what I need to put in a batch file (which will be put in my Startup folder), so the drive is automatically mounted at login/startup.

I tried just putting the line above in a batch file, but that didn't work.

Thanks for anyone's help.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
MOUNT is a Unix/Linux command.

You need to use the NET USE command.
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
Thanks for replying, Squashman.

I forgot to mention that I'm connecting to a Linux server. I did, however, try "net use" in the batch file, but that didn't work.

To be clear, from my Win7 computer, at the command prompt I type "mount -u:root -p:[password] \\216.1.17.125\srv\www\htdocs g:" to connect to a Linux network server that is using/running NFS.

From a batch file in my Startup folder I've tried both "mount" and "net use", but neither connects the drive - so far I've only been able to connect to it from the command prompt.

I can't map the network drive from Explorer, because that would use SAMBA and we're trying to use NFS for performance reasons. That part is beyond my understanding, but what I know is I can't map the drive from Explorer, and I need to connect using the above command.

Thanks again!
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Oh yeah. I remember now. Windows 7 now includes the ability to mount an NFS share. So if you have the Client Service for NFS installed you should be able to mount the NFS share.

You said in your previous post that you are able to do this from the CMD prompt?
If so it would be the same code in the batch file.
Create a new file that has the extension .BAT. Make sure it is .BAT and not .BAT.TXT. People have been confused on how to do this since Vista and Wndows 7 came out. A batch file is basically a text file. Once you change the extension to .BAT, Windows knows to execute it instead of opening it with Notepad. So just put the code you had into your first post into your batch file.
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
Yeah - unfortunately, I did just what you described:

Copy/pasted the command I enter from the cmd prompt into a text file,
Saved it as "mount h drive.bat" (not .txt), and
Put it in my Startup folder

The script doesn't work when I reboot or logoff/on :(
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
When you double click on the bat file what happens?
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
The dos box pops up and then disappears very quickly, but no connection is made.

When I connect from the cmd prompt there's about a 5 second or so delay before I get the message that it's connected - would it maybe help if I added a delay to the batch file?

Just a thought...
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
So are you getting a drive letter mapping when you manually launch the bat file to mount the NFS share?

You can always add the PAUSE command after the MOUNT command so that you can see the output of your MOUNT command before the dos box closes.
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
I'll give that a shot.

I did some searching on batch file how-tos and found SLEEP [timeInSeconds], so I'll try that. Or would PAUSE, requiring my input, be better? Would one or the other disrupt actually mounting the drive for 10 seconds or upon a key press (respectively).
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Batch files work sequentially. It has to finish processing a command before it proceeds to the next one.

Sleep is not a native Windows 7 command but TIMEOUT is. Sleep is part of the Server Admin Kit.

Pause requires user input for the batch file to proceeed.
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
Well, just adding the PAUSE command didn't work. I attached an image of the file and what I see when the script is paused. (drivemount.png) This happens whether I run the script by clicking on the bat file, or upon startup/login.

I wasn't sure what to make of the odd characters, but one thought that crossed my mind was, "Does it need to run from a different directory?"

So, I added CD .. before the MOUNT line and then ran it. That time it complained that the CD .. line had the weird characters in front of it (see the other attached image: drivemountWithDirChange.png). But the rest of the script ran.

So it works - kinda... As long as I have some other command before the MOUNT line, it'll misread that line and then correctly execute the MOUNT line.

Any idea what's causing that and how to overcome it?

Thanks again for your help!
 

Attachments

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Weird. At least it worked with the mount command being on the 2nd line. It certainly doesn't look like there is any characters before the mount command in your initial batch file.

Look at the Encoding in Notepad ++. Does it say Encode in ANSI? Batch files have to be plain ascii text to execute correctly.
Do you use any different language settings within Windows?
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Also in Notepad++ chose the View Menu option > Show Symbol > Show all Characters
 

capella07

Thread Starter
Joined
Oct 31, 2011
Messages
10
Both great thoughts, but alas, View Chars showed there is nothing before the MOUNT command (just a CRLF at the end of that line), and the Encoding is set to UTF-8.

Interestingly (and not surprisingly, I guess), I just added a CRLF before the MOUNT line and still get the Character not recognized message, but the MOUNT line executes anyway, as it did with the CD command before it.

So, it seems as if something is getting "injected" at the very beginning of the file regardless of what I do.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top