0

I need to create a script in VBS to remove all files from the Desktop in Windows, except files that contain .rdp and .exe extensions.

This is the script that has been created so far (it works, but it deletes everything):

Dim objShell, objFolder, objFolderItem, objFS, colSubFolders, Folder

Const USER_PROFILE = &H28&

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(USER_PROFILE)
Set objFolderItem = objFolder.Self
vPastaDeletar = objFolderItem.Path & "\Desktop"
Const DeleteReadOnly = TRUE

Dim Folder

Set objFS = CreateObject("Scripting.FileSystemObject")

ObjFS.DeleteFile(vPastaDeletar & "\*.*"),DeleteReadOnly

Set objFolder = objFS.GetFolder(vPastaDeletar)

Set colSubFolders = objFolder.Subfolders

On Error Resume Next

For Each Folder in objFolder.Subfolders

if Folder.name<> "Desktop" then

Folder.Delete

end if


Next

How can I edit the script to only preserve/skip files with .rdp and .exe extension?

Thanks in advance!

felipe
  • 1
  • How do you expect to skip files with a specific extension when you're not [enumerating files](https://devblogs.microsoft.com/scripting/how-can-i-get-a-list-of-all-the-files-in-a-folder-and-its-subfolders/)? – LesFerch Jul 12 '22 at 14:43
  • Okay, I understood in parts. How could I do this, or, where am I going wrong exactly? Could you give me more help? Thanks – felipe Jul 12 '22 at 14:50
  • Spend some time testing the code from the provided link. Use `WScript.echo` to display variables. Run in a `Cmd` console window using `CScript` so you can easily see results. – LesFerch Jul 12 '22 at 15:12
  • I'm testing and rewriting the code here! As for the variables, apparently everything is fine. I'll be testing some way to make the script filter the .rdp and just "ignore" them. Thank you so much for heading again! If you have any other suggestions for this, I'd really appreciate it! – felipe Jul 12 '22 at 15:23
  • See [this answer](https://stackoverflow.com/a/18921133/15764378) for code to get the file extension. – LesFerch Jul 12 '22 at 16:00

0 Answers0