Link to home
Start Free TrialLog in
Avatar of Vicki05
Vicki05Flag for United States of America

asked on

CreateShortcut on Desktop from a variable drive

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
Avatar of Rob Miners
Rob Miners
Flag of Australia image

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.
Avatar of Vicki05

ASKER

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.
Avatar of RobSampson
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.
Avatar of Vicki05

ASKER

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?
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.
Avatar of Vicki05

ASKER

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?
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
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

Avatar of Vicki05

ASKER

Hi Rob,

It works great but it stops on DOS window and does not exit.
OK, just change
    strCommand = "%comspec% /k cscript  """ & strPath & """"

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

Regards,

Rob.
Avatar of Vicki05

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vicki05

ASKER

Thanks Rob for all your help. The solution you provided has resolved my issue.



Vicki
No problem.  Thanks for the grade.

Rob.
Avatar of Vicki05

ASKER

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?
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

Avatar of Vicki05

ASKER

Thanks Rob,

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

Vicki