Solved delete multiple folders with diffrent paths using vbscript

October 31, 2012 at 07:13:31
Specs: Windows XP
Following are the steps:

Check if exist and delete a folder at c:\test1 if found and continue. If not found continue.
Check if exist and delete a folder at c:\programfiles\test2 if found and continue. If not found continue.
Check if a desktop shortcut and start menu shortcut exist and delete if found. If not exit.


See More: delete multiple folders with diffrent paths using vbscript

Report •


#1
October 31, 2012 at 20:28:43
rd /s /q c:\test1 2>nul
rd /s /q "c:\program files\test2" 2>nul
del /q "%userprofile%\desktop\xxx.lnk" 2>nul
del /q "%userprofile%\start menu\xxx.lnk" 2>nul

Report •

#2
November 1, 2012 at 06:08:29
thank you, but i need it in vbscript..

Report •

#3
November 1, 2012 at 09:51:07
✔ Best Answer
Set fso = CreateObject("Scripting.FileSystemObject")
'Get paths to the shell's folders
With CreateObject("WScript.Shell") 
  startMenu = .SpecialFolders("StartMenu")
  allStartMenu = .SpecialFolders("AllUsersStartMenu")
  desktop = .SpecialFolders("Desktop")
  allDesktop = .SpecialFolders("AllUsersDesktop")
End With
'WScript.Shell doesn't provide the Program Files directory; asking Explorer
With CreateObject("Shell.Application") 'Reference: ShlObj.h
  progFiles = .Namespace(&H26).Self.Path 'CSIDL_PROGRAM_FILES
  On Error Resume Next 'Mapping might not appear before Vista
    progFilesx86 = .Namespace(&H2A).Self.Path 'CSIDL_PROGRAM_FILESX86
  On Error Goto 0
End With

Del "c:\test1"
Del fso.BuildPath(progFiles, "test2")
If Len(progFilesx86) > 0 Then _
  Del fso.BuildPath(progFilesx86, "test2")
Del fso.BuildPath(startMenu, "a start menu shortcut")
Del fso.BuildPath(allStartMenu, "a start menu shortcut")
Del fso.BuildPath(desktop, "a desktop shortcut")
Del fso.BuildPath(allDesktop, "a desktop shortcut")
WScript.Quit

Sub Del(sPath)
  If fso.FolderExists(sPath) Then _
    fso.DeleteFolder sPath, True
  If fso.FileExists(sPath) Then _
    fso.DeleteFile sPath, True
End Sub

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
November 2, 2012 at 07:15:16
Wow Perfect solution. I have figured how to delete folders and desktop shortcut, but i couldn't figure out how to delete start menu untill now. Also thanks for the specification for program files, i might not have thought about it.

Report •

#5
November 13, 2012 at 08:00:23
I have an issue regarding the same script above given by Razor2.3. I use SCCM to push this script to all 1000 machines. It is able to delete everything except the desktop icon. As per the person using SCCM to push this script, it is not deleting the desktop icon because it is looking for the SCCM profile desktop and not the user profile. Is there a way i can enumerate folders in the documents and settings? Its surprising that the start menu folder is deleted in the user profile and not the SCCM profile.I don't get this. Any kind of advise is appreciated.

Report •

#6
November 15, 2012 at 10:35:28
It seems strange that the program in question would add something to the global Start Menu and the local user's Desktop. Did you include the extension of the shortcut (probably .lnk or .url)? Have you confirmed the shortcut's location within the file system?

How To Ask Questions The Smart Way


Report •

#7
November 15, 2012 at 10:48:04
It works fine when i run it manually , but when SCCM runs it all test machines, it doesn't delete the icons. I figured, it is unable to find the user profile. So, from this link http://blogs.msdn.com/b/alejacma/ar... i could get the correct user profile.

I used the subkey and enumerated through Profile Image Path for the system as follows:
oReg.EnumKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList", arrSubKeys
For Each SubKey In arrSubKeys
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & SubKey, "ProfileImagePath", ValueData
If InStr(ValueData, "Documents and Settings") Then
'Clean up. Remove possible spaces at the beginning and end of string
ValueData = Trim(ValueData)

and deleted the files as follows:
fso.DeleteFile ValueData & "\Desktop\shortcut.lnk"

It worked.
Thanks


Report •

#8
November 15, 2012 at 11:12:35
You might also want to look into checking the Default User's desktop as well.

How To Ask Questions The Smart Way


Report •


Ask Question