Solved

CreateShortcut on Desktop from a variable drive

Posted on 2013-01-27
17
441 Views
Last Modified: 2013-02-09
Hi All,

Question 1:
I need to copy 2 executables from a External Hard Drive to a small 8 gig drive through a batch file. The drive letter on the 8 gigabyte drive can vary. Is there a way to do that where it auto searches for the Drive, then copies the 2 files?

Question 2:

Once they are copied over to the 8 gig USB flash drive, I need to create shortcuts for both on the desktop. Now since the drive lettter can vary, I have no idea on how that can be accomplished.

This is what I have used if I know the drive letter. If it is something other then F, I do not know how I can account for that?

allusersdesktop = "C:\Documents and Settings\All Users\Desktop"
set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut(allusersdesktop & "\Transfer.lnk")
oShellLink.TargetPath = "F:\File Transfer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
oShellLink.Description = "Transfer"
oShellLink.WorkingDirectory = "f:\test"
oShellLink.Save




Please help
Vicki
0
Comment
Question by:Vicki05
  • 8
  • 7
  • 2
17 Comments
 
LVL 14

Expert Comment

by:Rob Miners
ID: 38825390
Try this it may help.

Open explorer and create a new folder. Something like USB. Insert the Memory stick. Open Computer Management, Disk Management and right click on the removable disk. Select Change Drive Letter and Paths. Here you can select a Drive Letter of choice by clicking Change, Select Add. Navigate to the folder and Select it and press OK. From now on when you insert the Memory stick you will have a copy of the contents in the new folder. You can set the batch file to the new drive letter without it ever changing.
0
 

Author Comment

by:Vicki05
ID: 38825425
I know how to do this manually but I would prefer to have a vbscript that can determine its location before creating the shortcuts. on the desktop.




allusersdesktop = "C:\Documents and Settings\All Users\Desktop"
set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut(allusersdesktop & "\Transfer.lnk")
oShellLink.TargetPath = "F:\File Transfer.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
oShellLink.Description = "Transfer"
oShellLink.WorkingDirectory = "f:\test"
oShellLink.Save




Please advice.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829108
Hi, is your script in the same folder as the target path?  If so, you can use:

strCurrentDrive = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, "\"))
oShellLink.TargetPath = strCurrentDrive & "File Transfer.exe"


Regards,

Rob.
0
 

Author Comment

by:Vicki05
ID: 38829489
Hi Rob,

Yes it is in the same path. I will try what you are suggesting. But is there a way that I can have my batch file find my removable drive and copy these files over as well?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38829506
Probably.  As long as it is picked up as a "removable drive", and as long as there is only one connected (unless we check for a specific folder or file that is unique to the drive), we should be able to do it.

As a first step, run this code
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
    Wscript.Echo "DeviceID: "& vbTab _
        &  objDisk.DeviceID       
    Select Case objDisk.DriveType
        Case 1
            Wscript.Echo "No root directory. " _
                & "Drive type could not be " _
                & "determined."
        Case 2
            Wscript.Echo "DriveType: "& vbTab _
                &  "Removable drive."
        Case 3
            Wscript.Echo "DriveType: "& vbTab _
                &  "Local hard disk."
        Case 4
            Wscript.Echo "DriveType: "& vbTab _
                &  "Network disk."      
        Case 5
            Wscript.Echo "DriveType: "& vbTab _
                &  "Compact disk."      
        Case 6
            Wscript.Echo "DriveType: "& vbTab _
                &  "RAM disk."   
        Case Else
            Wscript.Echo "Drive type could not be" _
                & " determined."
    End Select
Next

Open in new window


and you should see the drives listed as "Removable Drive".  That will show you that we can get the drive letter associated with each, so if that looks good, we can incorporate that.

Regards,

Rob.
0
 

Author Comment

by:Vicki05
ID: 38838057
The script stops at every step till it detects the removable drive, is there a way to bypass that and when it detects the drive, it copies the file?

Also can this be done in a Dos enviroment?
0
 
LVL 14

Expert Comment

by:Rob Miners
ID: 38838128
If you know the location of the files this batch could be modified to copy them to the USB stick, and then in turn run Rob's vbscript.

eXample: CPB.cmd

@echo off
:: variables
set path=%~dp0;%path%
set drive=%~dp0
set backupcmd=xcopy /y

%backupcmd% "d:\1 u64.txt" "%drive%\"
%backupcmd% "d:\u64.txt" "%drive%\"

%drive%\rob.vbs
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38838963
Hi, I have put the scripts together and this will now do the following:
1. Identify the drive the current script is running from (so put it in the same folder where you have the two files you want to copy *from*)
2. Determine the drive letter of the currently attached "removable drive"
3. Copy the two files from the script folder to the removable drive
4. Create two desktop shortcuts

For the file names, modify lines 18, 19, 27, and 36.

Regards,

Rob,

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")
For Each objDisk In colDisks

	WScript.Echo "Copying files from " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & " to " & objDisk.DeviceID & "\"
	'WScript.Echo "Copying " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe to " & objDisk.DeviceID & "\File Transfer.exe"
	objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe", objDisk.DeviceID & "\File Transfer.exe"
	objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Other File.exe", objDisk.DeviceID & "\Other File.exe"

	strAllUsersDesktop = "C:\Documents and Settings\All Users\Desktop"

	WScript.Echo "Creating desktop shortcuts"
	
	' Create first link file
	Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
	objLink.TargetPath = objDisk.DeviceID & "\File Transfer.exe"
	objLink.WindowStyle = 1
	objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
	objLink.Description = "Transfer"
	objLink.WorkingDirectory = objDisk.DeviceID & "\test"
	objLink.Save

	' Create second link file
	Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
	objLink.TargetPath = objDisk.DeviceID & "\Other File.exe"
	objLink.WindowStyle = 1
	objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
	objLink.Description = "Other"
	objLink.WorkingDirectory = objDisk.DeviceID & "\test"
	objLink.Save

Next

WScript.Echo "Done"

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Vicki05
ID: 38841709
Hi Rob,

It works great but it stops on DOS window and does not exit.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38841746
OK, just change
    strCommand = "%comspec% /k cscript  """ & strPath & """"

to
    strCommand = "%comspec% /c cscript  """ & strPath & """"

Regards,

Rob.
0
 

Author Comment

by:Vicki05
ID: 38843972
Hi Rob,

That did resolve the windows from closing, however when I tried it on windows 2000 computer, I keep getting the following error.

there is no disk in the drive. Please insert a disk into Drive A:

Please advice.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 38845934
Windows 2000?  Wow...that's not supported anymore, and Windows XP doesn't have much longer either....hopefully it's a relatively isolated system.

Anyway, the Drive A error will occur on any machine that has a floppy drive, so I have excluded the A: DeviceID from being processed.

Regards.

Rob.

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")
For Each objDisk In colDisks
	If objDisk.DeviceID <> "A:" Then
		WScript.Echo "Copying files from " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & " to " & objDisk.DeviceID & "\"
		'WScript.Echo "Copying " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe to " & objDisk.DeviceID & "\File Transfer.exe"
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe", objDisk.DeviceID & "\File Transfer.exe"
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Other File.exe", objDisk.DeviceID & "\Other File.exe"
	
		strAllUsersDesktop = "C:\Documents and Settings\All Users\Desktop"
	
		WScript.Echo "Creating desktop shortcuts"
		
		' Create first link file
		Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
		objLink.TargetPath = objDisk.DeviceID & "\File Transfer.exe"
		objLink.WindowStyle = 1
		objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
		objLink.Description = "Transfer"
		objLink.WorkingDirectory = objDisk.DeviceID & "\test"
		objLink.Save
	
		' Create second link file
		Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
		objLink.TargetPath = objDisk.DeviceID & "\Other File.exe"
		objLink.WindowStyle = 1
		objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
		objLink.Description = "Other"
		objLink.WorkingDirectory = objDisk.DeviceID & "\test"
		objLink.Save
	End If
Next

WScript.Echo "Done"

Open in new window

0
 

Author Closing Comment

by:Vicki05
ID: 38855507
Thanks Rob for all your help. The solution you provided has resolved my issue.



Vicki
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38857205
No problem.  Thanks for the grade.

Rob.
0
 

Author Comment

by:Vicki05
ID: 38861273
Hi Rob,

Is there a way for the script to avoid putting the files if it is a external drive as well? I ran into an issue where the files got copied to the drive itself?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38861862
If you can replicate the issue, when you run the code from comment ID 38829506 (by running cscript C:\Scripts\ShowDrives.vbs), what does it show the drives as?  How many does it show?

I guess what we could do is say if the detected source and destination drives are the same, don't do anything.

This will hopefully do that.

Regards,

Rob,

If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

strComputer = "."
strDriveScriptIsOn = UCase(Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, "\") - 1))
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=2")
For Each objDisk In colDisks
	If objDisk.DeviceID <> "A:" And objDisk.DeviceID <> strDriveScriptIsOn Then
		WScript.Echo "Copying files from " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & " to " & objDisk.DeviceID & "\"
		'WScript.Echo "Copying " & Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe to " & objDisk.DeviceID & "\File Transfer.exe"
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "File Transfer.exe", objDisk.DeviceID & "\File Transfer.exe"
		objFSO.CopyFile Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Other File.exe", objDisk.DeviceID & "\Other File.exe"
	
		strAllUsersDesktop = "C:\Documents and Settings\All Users\Desktop"
	
		WScript.Echo "Creating desktop shortcuts"
		
		' Create first link file
		Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
		objLink.TargetPath = objDisk.DeviceID & "\File Transfer.exe"
		objLink.WindowStyle = 1
		objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
		objLink.Description = "Transfer"
		objLink.WorkingDirectory = objDisk.DeviceID & "\test"
		objLink.Save
	
		' Create second link file
		Set objLink = objShell.CreateShortcut(strAllUsersDesktop & "\Transfer.lnk")
		objLink.TargetPath = objDisk.DeviceID & "\Other File.exe"
		objLink.WindowStyle = 1
		objLink.IconLocation = "%SystemRoot%\system32\SHELL32.dll,100"
		objLink.Description = "Other"
		objLink.WorkingDirectory = objDisk.DeviceID & "\test"
		objLink.Save
	End If
Next

WScript.Echo "Done"

Open in new window

0
 

Author Comment

by:Vicki05
ID: 38872369
Thanks Rob,

This is what I needeed. I appreciate all the help.

Vicki
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now