2hotty
(Getting the hang of it)
2002-08-06 01:11 PM
Desktop Shortcuts

I am building a script that will delete a mapped network drive and the desktop shortcut which relates to this network drive. My pdc and bdc platforms are NT and my workstation platforms are XP, 2000, 98, NT. I have no problem with deleting the mapped network drive was just wondering how to delete the desktop shortcut.

Aany help much appreciated guys.


LonkeroAdministrator
(KiX Master Guru)
2002-08-06 01:30 PM
Re: Desktop Shortcuts

if it's named properly that would not be problem. if it's named whatever, you can't do anything about it without some nice cracks.

anyway, you will locate users desktop with:
$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")

and to delete known named link:
$file="mydrive.lnk"
del $desktop+"\$file"

if you then have no idea of the name, you would need to dir the desktop and check all files if they are lnk and if yes, then check if they point to that location...
can write that for you too if you need it...

just say [Big Grin]


2hotty
(Getting the hang of it)
2002-08-06 02:46 PM
Re: Desktop Shortcuts

Sorry about this i just tested my script on an NT workstation and nothing happened. What scipt would you write which would delete a network connection t: "fileserver\net_share" and a desktop shortcut named - shortcut to net_share on 'fileserver' (t).

LonkeroAdministrator
(KiX Master Guru)
2002-08-06 03:16 PM
Re: Desktop Shortcuts

I would add some file content scanning in there...
like:
code:
$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
$file=dir($desktop+"\*.lnk")
while @error=0
shell '%comspec% /c type $desktop'+'\$file |find "\\server\share" >nul'
if not @error
? "this file: $file" ? "points to the place where it shouldn't!"
del $desktop+"\$file"
endif
$file=dir()
loop

not tested as did it just from head...


Sealeopard
(KiX Master)
2002-08-06 03:20 PM
Re: Desktop Shortcuts

It really depends on where the shortcut is stored, either in the 'All Users' profile, the 'Default User' profile, or the current user's profile.

If it's in 'All Users' or 'Default Users' then you'll need administrative permissions to delete that file.


Radimus
(KiX Supporter)
2002-08-06 03:44 PM
Re: Desktop Shortcuts

try this

code:
Break on


deldrive("h")

function DelDrive($letter)
dim $d[6], $dp[6], $loop, $desktop

$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
$desktop=expandenvironmentvars($desktop)
$d=DriveProp("$letter")
if $d[1]="network"
use "$letter:" /d
$dp=split($d[2],"\")
$icon=dir("$desktop\shortcut to*.lnk")
while not @error
if instr($icon,$dp[2]) and instr($icon,$dp[3])
? "got it" $icon
del "$desktop\$icon"
endif
$icon=dir()
loop
endif

endfunction

function DriveProp($Drive)
dim $fso, $Drives, $list, $param, $property
$DriveType="unknown","removable","fixed","network","cd-rom","ram"
$DrivePara="driveletter","drivetype","sharename","isready","filesystem","volumename","serialnumber"
dim $temparray[6]
$fso = createobject("scripting.filesystemobject")
if $fso
$D=$fso.getdrive("$Drive")
for $param = 0 to ubound($DrivePara)
$property=$DrivePara[$param]
$ret=execute('$$temparray[$param]=$$d.$property')
if $param=1 $temparray[$param]=$DriveType[$temparray[$param]] endif
next
$DriveProp=$temparray
$fso=""
endif
exit @error
endfunction



LonkeroAdministrator
(KiX Master Guru)
2002-08-06 04:05 PM
Re: Desktop Shortcuts

mmm...
so rad you have spent time with this...

anyway the shortcut gets deleted if it has in it's name the drive...


2hotty
(Getting the hang of it)
2002-08-06 04:06 PM
Re: Desktop Shortcuts

Radimus you have really confused me. i am only new to this. Right. Every user has their t drive mapped to the same network path. I wish to remove this and the desktop shortcut which is named the same on every user's desktop.

LonkeroAdministrator
(KiX Master Guru)
2002-08-06 04:14 PM
Re: Desktop Shortcuts

but is it on users desktop?
if it is in all users\desktop
you can't probably delete it in logonscript as most likely normal user has no rights to delete it.


2hotty
(Getting the hang of it)
2002-08-06 04:15 PM
Re: Desktop Shortcuts

you can because when i configure a new machine i manually mapp the network drive in the user profile. Therefore you dont need administrative rights to delete it.

LonkeroAdministrator
(KiX Master Guru)
2002-08-06 04:19 PM
Re: Desktop Shortcuts

so, I can't see why my first sample didn't work for you.

2hotty
(Getting the hang of it)
2002-08-06 04:24 PM
Re: Desktop Shortcuts

im sorry i haven't tested your script yet, was just confused by radimus. Could you please explain what happens at each stage of the script, so it can understand the process.

LonkeroAdministrator
(KiX Master Guru)
2002-08-06 04:28 PM
Re: Desktop Shortcuts

well, it scans all link files in desktop folder and if it links to the pointed place it deletes the link.
anyway, if you know the name of the link-file you just need to specify it and use my first sample.
it reads from registry the place of users desktop folder and deletes the file from there:
code:
;your filename here:
$file="mydrive.lnk"
;the place in reg for folder info:
$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
del $desktop+"\$file"



Radimus
(KiX Supporter)
2002-08-06 04:40 PM
Re: Desktop Shortcuts

it only deletes the shortcut if the servername and sharename are in the 'shortcut to'

Driveprop() is a UDF described in the UDF forum, and it's purpose is to return and array of values of the types described in the $DrivePara array

The Deldrive() function only will operate if it is passed a drive letter that it a network drive type (not a CD drive letter) it will only function if you give it a network drive to delete.

beyond that, just read the code line by line... it is just little steps


2hotty
(Getting the hang of it)
2002-08-06 05:21 PM
Re: Desktop Shortcuts

Lets start from the beginning in simple terms to a beginner(me).

what would be the command to delete a mapped network drive. I have mapped T to \\fileserver\net_share.

deldrive t: "\\fileserver\net_share"

This does not work can someone please explain in simple terms explaining what each stage of the script does.


Sealeopard
(KiX Master)
2002-08-06 05:25 PM
Re: Desktop Shortcuts

To for example delete a mapped drive T: you just need to do a
code:
use T: /delete /persistent

However, the question was more related to how to delete an shortcut on the user's desktop.


2hotty
(Getting the hang of it)
2002-08-06 05:32 PM
Re: Desktop Shortcuts

Thanks sealeopard.

Now could you show me how to delete the shortcut on the desktop which is called - shortcut to net_share on 'fileserver' (T).

I woiuld really appreciate it if you could explain what is happening at each stage.


Radimus
(KiX Supporter)
2002-08-06 05:38 PM
Re: Desktop Shortcuts

$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
del "$desktop\shortcut to net_share on 'fileserver' (T).lnk"

Click here for more detail

[ 06. August 2002, 17:42: Message edited by: Radimus ]


2hotty
(Getting the hang of it)
2002-08-06 05:53 PM
Re: Desktop Shortcuts

It didnt work. sorry.

Radimus
(KiX Supporter)
2002-08-06 06:05 PM
Re: Desktop Shortcuts

more detail....

2hotty
(Getting the hang of it)
2002-08-06 06:15 PM
Re: Desktop Shortcuts

I checked the help file, but i can't quite grasp the idea of com automation and variables yet.

Below is what i have created for a user called - smithb. Everything is fine except it doesn't seem to delete the desktop shortcut. I am off home now so any ideas overnight are much appreciated.

If @userid = 'smithb'
use t: /delete /persistent
$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurentVersion\Explorer\User Shell Folders","Desktop")
$file="shortcut to net_share on 'fileserver'(T).lnk"
del $desktop+"\$file"
Endif


LonkeroAdministrator
(KiX Master Guru)
2002-08-06 06:18 PM
Re: Desktop Shortcuts

the shortcut can be with another extension.
anyway, if you change the del line to:
del "$desktop\shortcut to net_share on 'fileserver' (T)*"

there is no way it can stay there.


Radimus
(KiX Supporter)
2002-08-06 06:35 PM
Re: Desktop Shortcuts

you may need to do:
$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
$desktop=expandenvironmentvars($desktop)


Sealeopard
(KiX Master)
2002-08-06 07:24 PM
Re: Desktop Shortcuts

code:
use T: /delete /persistent
? 'Deleting mapping to drive letter T: '+@ERROR+' - '+@SERROR


$desktop=readvalue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders","Desktop")
? 'Retrieving location of user's DESKTOP folder in his profile: '+@ERROR+' - '+@SERROR


? 'Users DESKTOP folder is located in '+$desktop
$desktop=expandenvironmentvars($desktop)


? Expanding environment variables (if present) and complete path to user's DESKTOP is '+$desktop

del "$desktop\shortcut to net_share on 'fileserver' (T).lnk"
? "Deleting file """+$desktop+"\shortcut to net_share on 'fileserver' (T).lnk"" :"+@ERROR+" - "+@SERROR

I hope this is extensive enough, if you need an even more detailed explanation, then I would suggest you play around with the examples in the KiXtart manual to understand what each command/function/macro is doing.

[ 06. August 2002, 19:26: Message edited by: sealeopard ]


Radimus
(KiX Supporter)
2002-08-06 07:52 PM
Re: Desktop Shortcuts

btw... if you use my code, you only need to do:

deldrive("h")
or
deldrive("t")

as long as you have the 2 functions located somewhere in the script... commonly in the end. refer to the usages of functions.

requires kix 4+

If you need it, I can repost it with the test code still in.


2hotty
(Getting the hang of it)
2002-08-07 11:52 AM
Re: Desktop Shortcuts

Finally completed this stage, thanks to all you guys, the help is much appreciated. [Smile]