# How to Create Batch File to Mount Network Drive



## capella07 (Oct 31, 2011)

I am using this command:

```
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 (Apr 4, 2003)

MOUNT is a Unix/Linux command.

You need to use the NET USE command.


----------



## capella07 (Oct 31, 2011)

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 (Apr 4, 2003)

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 (Oct 31, 2011)

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 (Apr 4, 2003)

When you double click on the bat file what happens?


----------



## capella07 (Oct 31, 2011)

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 (Apr 4, 2003)

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 (Oct 31, 2011)

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 (Apr 4, 2003)

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 (Oct 31, 2011)

Great!

I'll use PAUSE, then and see how that works.


----------



## capella07 (Oct 31, 2011)

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!


----------



## Squashman (Apr 4, 2003)

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 (Apr 4, 2003)

Also in Notepad++ chose the View Menu option > Show Symbol > Show all Characters


----------



## capella07 (Oct 31, 2011)

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.


----------



## Squashman (Apr 4, 2003)

I just told you the encoding needs to be ANSI.


----------



## capella07 (Oct 31, 2011)

I wasn't aware that ANSI was the same as ASCII.

I changed it to ANSI and it works fine now.

Thank you for your time.


----------



## Squashman (Apr 4, 2003)

I guess I have to ask why you had the encoding set to UTF-8?
You are the 2nd person I have run into this year that has made that same mistake. The other guy had his set as UCS-2 which is double byte. Batch will never understand that. UTF-8 is technically ASCII compatible but does have an extra header byte which is what is throwing off the batch file.


----------



## Squashman (Apr 4, 2003)

This should explain what is going on if you wanted to know.


> Byte order mark
> 
> Many Windows programs (including Windows Notepad) add the bytes 0xEF, 0xBB, 0xBF at the start of any document saved as UTF-8. This is the UTF-8 encoding of the Unicode byte order mark (BOM), and is commonly referred to as a UTF-8 BOM, even though it is not relevant to byte order. The BOM can also appear if another encoding with a BOM is translated to UTF-8 without stripping it.
> The presence of the UTF-8 BOM may cause interoperability problems with existing software that could otherwise handle UTF-8; for example:
> ...


----------



## capella07 (Oct 31, 2011)

Squashman said:


> I guess I have to ask why you had the encoding set to UTF-8?


That's what Notepad++ defaulted to. As I stated, I wasn't aware that ASCII and ANSI were different and until you mentioned it, it never occurred to me that encoding wasn't the issue. That's why I came here for help. If it *would *have occurred to me, I suppose I wouldn't have needed the help in the first place, would I?


----------



## Squashman (Apr 4, 2003)

I have never seen N++ default to UTF-8 and I have been using it for years. But it is one of your preference settings. ANSI is set by default when you install it. I have never changed this setting. You may want to check this setting.


----------

