Member_2_4371340
asked on
VB Script that will scan user profile for .lnk files, and delete
I have a mandatory profile that all Citrix users load on logon. The profile is locked down, but Windows 2003 is regenerating shortcuts in the Start menu that I do not want:
%USERPROFILE%>>programs>>I nternet Explorer.lnk
%USERPROFILE%>>programs>>O utlook Express.lnk
%USERPROFILE%>>programs>>A ccessories >>Address Book.lnk
%USERPROFILE%>>programs>>A ccessories >>Entertai nment>>Win dows Media Player.lnk
I want to run a profile startup script that will delete all these shortcuts.
I'm looking for a Script that will:
- Recursively search the %Userprofile% folders for .lnk files, delete the files, the create a log of the deleted files.
OR
- If we can't get that working, a simple manual deletion of the above files using static paths
I've attached my code below modified from a script I found on computerperformance.co.uk (Editor: Guy Thomas)
%USERPROFILE%>>programs>>I
%USERPROFILE%>>programs>>O
%USERPROFILE%>>programs>>A
%USERPROFILE%>>programs>>A
I want to run a profile startup script that will delete all these shortcuts.
I'm looking for a Script that will:
- Recursively search the %Userprofile% folders for .lnk files, delete the files, the create a log of the deleted files.
OR
- If we can't get that working, a simple manual deletion of the above files using static paths
I've attached my code below modified from a script I found on computerperformance.co.uk (Editor: Guy Thomas)
' This code is a modified version of Guy Thomas' script on
' http://www.computerperformance.co.uk/ezine/ezine139.htm
'------------------------------------------------------------'
Option Explicit
Dim objFSO, objFiles, objShell, intCount
Dim strFile, strName, strLongName, strDirectory, strEnv, strExt
Set objShell = CreateObject("Wscript.Shell")
strEnv = objShell.ExpandEnvironmentStrings("%userprofile%")
intcount = 0
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSO = objFSO.GetFolder(strEnv)
Set objFiles = objFSO.Files
For each strFile in objFiles
strExt = strFile.Type
If lcase(strExt) = "LNK FILE" Then
strName = strFile.Name
strLongName = strLongName & strName & VBTab & VBTab
intCount = intCount +1
End if
next
WScript.Echo "The number of LNK files is: " & intCount
WScript.Echo strLongName & VbCr & intCount
set objFSO = nothing
set strFile = nothing
set objFiles = nothing
WScript.Quit
Why not use something simple like the following:
del %USERPROFILE%\programs\Int ernet Explorer.lnk
del %USERPROFILE%\programs\Out look Express.lnk
del %USERPROFILE%\programs\Acc essories\A ddress Book.lnk
del %USERPROFILE%\programs\Acc essories\E ntertainme nt\Windows Media Player.lnk
del %USERPROFILE%\programs\Int
del %USERPROFILE%\programs\Out
del %USERPROFILE%\programs\Acc
del %USERPROFILE%\programs\Acc
Why not use Robocopy, it solves all the world's issues! XD
Robocopy "%UserProfile%\programs" "" *.lnk /MOV
Robocopy "%UserProfile%\programs" "" *.lnk /MOV
ASKER
Graye, I already have a VB script on the server that maps network drives depending on Windows security groups. I want to integrate the 2 scripts.
QCubed, I'm a big fan of Robocopy, but can you explain exactly how that switch deletes .lnk files, and will that line recursively scan all folders in %Userprofile% for .lnk files, deleting shortcuts as it locates them?
QCubed, I'm a big fan of Robocopy, but can you explain exactly how that switch deletes .lnk files, and will that line recursively scan all folders in %Userprofile% for .lnk files, deleting shortcuts as it locates them?
ASKER
Graye, I just tried a batch script with the above pasted lines - The script runs, but the shortcuts are still there...
Perhaps the script runs before the shortcust are generated?
Is there a ' wscript.sleep <seconds> ' equivalent for batch files?
Perhaps the script runs before the shortcust are generated?
Is there a ' wscript.sleep <seconds> ' equivalent for batch files?
Yeah, the path is not correct... for modern versions of Windows it'd be something like this:
%USERPROFILE%\AppData\Roam ing\Micros oft\Window s\Start Menu\\programs\Internet Explorer.lnk
%USERPROFILE%\AppData\Roam
ASKER
This is a Windows 2003 Std server.
Why not simply use the following batch file?
@echo off
for /f "delims=?" %%f in ('dir %userprofile%\*.lnk /s/b') do del "%%f"
ASKER
x66_x72_x65_x65, that script is very good, but I'm after finding out the root of my problem. I added "Sleep 10" after your 2 script lines above.
On logging on to the Citrix server, all users load a mandatory profile from C:\profile.mand. I have saved the startup batch file to C:\profiles.mand\stduser\S tart Menu\Programs\Startup. The batch file executes on log on, but the following error is displayed:
"The system cannot find the path specified"
I'm guessing that for some reason, %USERPROFILE" isn't being recognised...
On logging on to the Citrix server, all users load a mandatory profile from C:\profile.mand. I have saved the startup batch file to C:\profiles.mand\stduser\S
"The system cannot find the path specified"
I'm guessing that for some reason, %USERPROFILE" isn't being recognised...
@echo off
for /f "delims=?" %%f in ('dir %userprofile%\*.lnk /s/b') do del "%%f"
Sleep 10
ASKER
If I goto Start\Run\%USERPROFILE%, my profile opens up OK.
When I open up a command prompt, and change directory to the C:\,and enter "CD %USERPROFILE%, the command executes fine, but nothing happens.
If I type "DIR %USERPROFILE%", DOS lists the directories and files in my profile...
When I open up a command prompt, and change directory to the C:\,and enter "CD %USERPROFILE%, the command executes fine, but nothing happens.
If I type "DIR %USERPROFILE%", DOS lists the directories and files in my profile...
If your profiles are being stored in folder structure which contains a space you'll need to enclose the %userprofile% variable in quotes.
Windows 2003 profiles are stored in a folder which contains spaces (i.e. C:\Documents and Settings (by default) vs. C:\Users for Windows 2008)
You may want to experiment with the sleep command as it's likely not necessary in this case.
Windows 2003 profiles are stored in a folder which contains spaces (i.e. C:\Documents and Settings (by default) vs. C:\Users for Windows 2008)
You may want to experiment with the sleep command as it's likely not necessary in this case.
@echo off
for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" /s/b') do del "%%f"
sleep 10
Relating to the possible root problem, have you check the following folders for the problem links?
C:\Documents and Settings\All Users\Start Menu\Programs
C:\Documents and Settings\All Users\Start Menu\Programs\Accessories
C:\Documents and Settings\Default User\Start Menu\Programs
C:\Documents and Settings\Default User\Start Menu\Programs\Accessories
Also, here are the changes to the script (bolded)
@for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" /s/b') do del "%%f"
C:\Documents and Settings\All Users\Start Menu\Programs
C:\Documents and Settings\All Users\Start Menu\Programs\Accessories
C:\Documents and Settings\Default User\Start Menu\Programs
C:\Documents and Settings\Default User\Start Menu\Programs\Accessories
Also, here are the changes to the script (bolded)
@for /f "delims=?" %%f in ('dir "%userprofile%\*.lnk" /s/b') do del "%%f"
ASKER
I'll try to explain what fixed the problem:
One of the issues was that DOS would not read %USERPROFILE% correctly unless it placed inside inverted commas. The spaces in "Documents and Settings" being the most likely reason for that.
There's a mandatory profile stored in "C:\Profile.Mand" on the Citrix server. All remote users have their terminal services profiles configured to use this profile. When I set the script up to delete the shortcuts in "%USERPROFILE%\Start Menu\Programs\..., the script looks to run OK, but the shortcuts are still available in the folders. I edited the startup login script to point at the shortcuts in the mandatory profile, and hey presto, I can see the shortcuts being deleted.
I assume this has something to do with the mandatory profile being cached every time somebody logs on?
One of the issues was that DOS would not read %USERPROFILE% correctly unless it placed inside inverted commas. The spaces in "Documents and Settings" being the most likely reason for that.
There's a mandatory profile stored in "C:\Profile.Mand" on the Citrix server. All remote users have their terminal services profiles configured to use this profile. When I set the script up to delete the shortcuts in "%USERPROFILE%\Start Menu\Programs\..., the script looks to run OK, but the shortcuts are still available in the folders. I edited the startup login script to point at the shortcuts in the mandatory profile, and hey presto, I can see the shortcuts being deleted.
I assume this has something to do with the mandatory profile being cached every time somebody logs on?
ASKER
Another funny thing I've noticed, is that when you log on to the server via RDP, the Outlook Express shortcut is always regenerated by Windows, and can only be deleted, by:
del %USERPROFILE%\Start Menu\Programs\Outlook Express.lnk
RDP connection also load the mandatory profile, so it shouldn't be any different from logging on to the server via Citrix...
del %USERPROFILE%\Start Menu\Programs\Outlook Express.lnk
RDP connection also load the mandatory profile, so it shouldn't be any different from logging on to the server via Citrix...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, if you want to simply clean-up all problem links from the entire drive (all users, etc.) run the following command. This should only have to be ran once. Obviously you may substitute "Outlook Express.lnk" for any other link name you want to recursively delete.
del /f /s /q "%SystemDrive%\Outlook Express.lnk"
ASKER
Excellent script x66_x72_x65_x65!
Thank you,
Fin
Thank you,
Fin
ASKER
'If lcase(strExt) = ".lnk" Then' but no go either...