Create shorcut to network drives

Posted on 2007-11-20
Last Modified: 2009-11-12
At present users logon to their machines and access the files they require through various mapped drives. Each user has a script when they logon which is assigned to their user profile in Active Directory, the script will be their surname followed by the first initial of their first name, for example smitha.bat

It is quite common that a user will call Technical Support to say that their drives have not loaded, sometimes a restart will fix this but sometimes it doesn't and therefore we manually put their .bat file onto the desktop which they can double click and it will then load the drives.

What I would like to do is create a script either in VB or as a batch file so when the user logs onto any machine the script automatically puts their .bat file on the desktop. This will save time for the users and tech support.

Does anyone know how I could do this?
Question by:ZEDA_ISG
  • 7
  • 4

Expert Comment

ID: 20319001

Would something like this solve your problem ??

1st get the name of the users login script with a dsquery query:

dsquery user -samid %username% | dsget user -loscr

2nd copy the file to C:\documents and settings\%username%\desktop

Hope this helps

Author Comment

ID: 20319090
Thank you for the fast reply.

The first line works as it states the users batch file. Great, thanks.

What we need to do is create a shortcut to the .bat from the server to the desktop upon logon, for example:



C:\Documents and Settings\smitha\Desktop

Then they double click the smitha.bat which then loads the network drives in My Computer.


LVL 27

Expert Comment

ID: 20321206
Here's an example of how to create a shortcut on the desktop, just pass in the path to the bat file:

Private Sub CreateShortCut(ByVal path As String)

Set myWSH = CreateObject("WScript.Shell")
Set WSHShortcut = _
    myWSH.CreateShortCut("C:\Documents and Settings\All Users\Desktop\MapDrives.lnk")
    With WSHShortcut
        .TargetPath = path
        .Description = "MapDrives"
        .RelativePath = path
        .WorkingDirectory = "C:\"
        .WindowStyle = 3   ' 1 = normal; 3 = maximize window; 7 = minimize
    End With
    Set myWSH = Nothing
    Set WSHShortcut = Nothing
End Sub

'And here's an example:
Dim databasePath As String
databasePath = "C:\TEMP\db4.mdb"
CreateShortCut (databasePath)

MsgBox "Done"

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.


Expert Comment

ID: 20326217

I still believe it's better to copy the batchfile, since it is a t iny bit of data. If you use script VBrocks mentioned above you must change the path from all user profile to target userprofile. Else will the next person to logon be able to click the batchfile lnk too.

Me and my collegue spend some time on this :p

The following is tested and should work:

dsquery user -samid %username% | dsget user -loscr >>logonS.txt

for /F "skip=1" %i in (logonS.txt) do @if not %i==dsget copy %i C:\documents and settings\%username%\desktop
1 file(s) copied.

Check it out! Just 2 lines!

Author Comment

ID: 20326524
Thanks for the replys. I tried this:

dsquery user -samid %username% | dsget user -loscr >>logonS.txt

for /F "skip=1" %i in (logonS.txt) do @if not %i==dsget copy %i C:\documents and settings\%username%\desktop
1 file(s) copied.

It creates the logonS.txt file in the root of C:/ which is great but it doesn't put the individuals .bat file on the desktop, i.e. smitha.bat.

Any ideas?

Expert Comment

ID: 20327814

You have to test a little with that.

Change the for loop into:

for /F "skip=1" %i in (logonS.txt) do @if not %i==dsget echo %i
This echo's in your dosboxx what logonscript is supposed to be.(We have no logon script in AD so had to test that For loop with something else)
If you see nothing, you can add >>log.txt to the command and show me here what is in log.txt
Next you can try
for /F "skip=1" %i in (logonS.txt) do @if not %i==dsget copy %i C:\
To see whether he can copy the script to c:\ (Maybe %username% does not work.

Good luck!

Author Comment

ID: 20328133

Tried the new suggestions again, still it runs the first bit:
dsquery user -samid %username% | dsget user -loscr >>logonS.txt

But after this, it is still not doing anything at all.  I have found that if I drag the script into a command prompt and run it, it does not run with the % signs as in the script in notepad
for /F "skip=1" i in (logonS.txt) do @if not i==dsget echo i
instead of
for /F "skip=1" %i in (logonS.txt) do @if not %i==dsget echo %i

I also tried putting the >>log.txt but the text file didn't create as an error came back in the command window saying "echo was unexpected at this time" or "copy was unexpected at this time" depending on whether using Echo or Copy.

Thanks for the help so far...

Expert Comment

ID: 20333050

I am running out of options to help you with this, the command works fine here.

Be sure that when you run in a command prompt you are in the same path as logonS.txt. I might be a problem of there being a slight delay between logonS.txt being created and you opening it for reading.

In picture below you clearly see the echo of D:\test.vbs

I hope you figure it out!

Author Comment

ID: 20356872
Thanks for the help but I cannot get this to work.

It finds the batch file no problem but it simply will not copy the .bat file to the desktop for the user to click to load the drives.

Any other ideas?

Author Comment

ID: 20372046
Can anyone else offer any advice?

It would save so much time if we got this to work!

Author Comment

ID: 20394875
Anyone ? :)

Accepted Solution

ZEDA_ISG earned 0 total points
ID: 20412307
I have got this to work by doinf the following:

Create a .bat file which contains a copy command:

copy "\\servername\sysvol\\scripts\network_drives\network_drives.lnk" "%systemdrive%\Documents and Settings\%username%\Desktop"

This then create a network_drives.lnk file which copies the .bat file to the desktop.

It is then applied through group policy, logon/logoff script. Working just as I wanted to. Thanks for the help anyway.

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question