• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 820
  • Last Modified:

Mapping a drive from a variable

Mapping a drive from a variable

I am using the following to set variables in a BAT.


@echo off
setlocal
set Location=%ComputerName:~0,3%


I am trying to map a drive using the variable above


Net use q: \\%location%MGMT\share

With %location% for the earlier set variable, and then adding MGMT on to complete the server name

For example

Workstation name NYCACT001

%location% is NYC

Server name is NYCMGMT

Share name share

Hence \\%location%MGMT\share


Any Thought on what I am doing wrong would be of great help.

Thank you.
0
ChicagoMikeW
Asked:
ChicagoMikeW
  • 6
  • 4
  • 2
  • +1
1 Solution
 
SteveGTRCommented:
Looks good. What is happening that makes you think something is wrong?

If you wanted to verify that the net use command was properly setup you might insert this command:

echo Net use q: \\%location%MGMT\share
0
 
Christopher McKayCommented:
I'm somewhat confused as to why you would use this method to arrive at the server name, however, it looks correct, have you tried something like this:

set location=%computername:~0,3%MGMT

net use q: \\%location%\share


Hope this helps!

:o)

Bartender_1
0
 
ChicagoMikeWAuthor Commented:
The error I get is network path not found but if I type the path instead of the variable it works.

So

Net use q: \\%location%MGMT\share

      Network pat not found


Net use q: \\NYCMGMT\share

      Works fine



Thanks again
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
ChicagoMikeWAuthor Commented:
I tried


set loc=%computername:~0,3%MGMT

net use q: \\%loc%\share

It does not like that either.
0
 
Christopher McKayCommented:
can you do as SteveGTR has suggested, and use the echo command to see what is being sent to the command line?

echo net use q: \\%loc%\share
0
 
ChicagoMikeWAuthor Commented:
Brilliant

Thank you

I am getting the whole computer name

Net use q: \\nycact001:~0,3%\share

I am trying to use the

set Location=%ComputerName:~0,3%

so that I can just use the first 3 letters of the computer name,

any thought.

Thank you
0
 
oBdACommented:
Check your original script; there is likely a space at the end of the line
set Location=%ComputerName:~0,3%
Make sure the line ends directly after the % sign.
0
 
SteveGTRCommented:
Some more things to check:

1) What version of windows is this running on?

2) What does this display:

echo CMDEXTVERSION=%CMDEXTVERSION%
0
 
ChicagoMikeWAuthor Commented:
It is Windows XP SP2

It says
CMDEXTVERSION=2
0
 
ChicagoMikeWAuthor Commented:
I checked no space at the end.
0
 
SteveGTRCommented:
That's all good. The only way I can see getting what you are getting is with this code:

@echo off

setlocal

set location=%computername%:~0,3%MGMT

echo location=%location%

It has an extra % after the e in computername.

Can you cut and paste the exact line of code?
0
 
ChicagoMikeWAuthor Commented:
This is what I have now


@echo off
setlocal
set Location=%ComputerName:~0,3%
set Department=%ComputerName:~3,3%



echo net use q: \\%location%MGMT\c$


I previously had

@echo off
setlocal
set Location=%ComputerName%:~0,3%
set Department=%ComputerName:~3,3%



echo net use q: \\%location%MGMT\c$

The extra % was the problem you are correct.
0
 
SteveGTRCommented:
Easy to do and hard to see. I've done that myself.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now