Bobo Posted April 15, 2008 Posted April 15, 2008 I need a script that can delete either file extensions or a wildcard? Basically due to R2 not being able to set a default printer we still use a vbs in the all users startup folder to set the default after login. Problem is these vbs files and the startup folder is populating the recent documents folder and I was hoping to add to the vbs default printer script at the end to delete *.vbs files and the startup link. Is this possible? Can you delete file extensions in a VBScript? Thanks guys, have done an extensive search but with no avail.
srochford Posted April 16, 2008 Posted April 16, 2008 I'd use a logon script but if you want to clear particular links then it's possible but not quite as easy as you want - all the "recent" files have a .lnk extension so this code won't work: set ofso=createobject("scripting.filesystemobject") set oShell=createobject("wscript.shell") sRecent=oshell.specialfolders("recent") set oFolder=ofso.getfolder(sRecent) for each oFile in oFolder.files sExtension=lcase(ofso.getextensionname(oFile)) wscript.echo sExtension, oFile.name if sExtension="vbs" then oFile.delete next All is not lost, however, because you can just use a bit more code: set ofso=createobject("scripting.filesystemobject") set oShell=createobject("wscript.shell") sRecent=oshell.specialfolders("recent") set oFolder=ofso.getfolder(sRecent) for each oFile in oFolder.files sName=lcase(ofso.getbasename(oFile)) if right(sName,3)="vbs" then oFile.delete next Instead of just directly getting the file extension (the bit after the last full stop) you take the bit before the last full stop (basename) and check the last 3 characters. This is relatively inefficient; if there are 1000 files in a folder it will look at all of them but because it runs on the local PC and only runs once per session it's probably OK. If you really do want to do wildcard searches then you have to use WMI and the CIM_DataFile: sComputer = "." set oShell=createobject("wscript.shell") sRecent=oshell.specialfolders("recent") if mid(sRecent,2,1)<>":" then wscript.echo "Can't find recent; giving up" wscript.quit end if sDrive=left(sRecent,2) sRecent=replace(mid(sRecent,3),"\","\\") & "\\" Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2") sSQL="Select * from CIM_DataFile Where drive='" & sDrive & "' and (name like '%.vbs.%') and (path='" & sRecent &"')" Set colFiles = oWMIService.ExecQuery(sSQL) For Each oFile in colFiles oFile.delete Next This is a bit more complicated (!) but is much more flexible. Key things is that it needs to know which computer you are looking at ("." means local computer); you have to know the drive letter (so it gives up if the recent path doesn't start x: - I could strip off the PC name from the start of the share but life's too short!), you have to escape \ with an extra \ and finally, the wildcard is the % sign. Other than that, it's really easy :-) You can run this against something like an entire fileserver to search for files but it does seem to be rather memory hungry - I'd guess it builds the file collection in memory. 1
Bobo Posted April 16, 2008 Author Posted April 16, 2008 Why dont you just assign it as a log on script? Because the startup folder comes into action after login so the printer will end up right back in the recent documents folder. Thanks srochford, although I was worried about the .lnk extensions bit, but thought there may be some way as you suggested. I have avoided learning vbs like the plague and rely on "thieving" others scripts so I thank you. We are cutting the folder down to a size of around 30 so the searching should not take too long. Again much thanks, and I will no doubtedly ask for more advice before this problem is solved.
whunter Posted August 25, 2011 Posted August 25, 2011 I need a script that can delete either file extensions or a wildcard? Basically due to R2 not being able to set a default printer we still use a vbs in the all users startup folder to set the default after login. Problem is these vbs files and the startup folder is populating the recent documents folder and I was hoping to add to the vbs default printer script at the end to delete *.vbs files and the startup link. Is this possible? Can you delete file extensions in a VBScript? Thanks guys, have done an extensive search but with no avail. This will delete all files in the last nested folder with extension txt. Plain and simple. Const DeleteReadOnly = TRUE 'or FALSE, if you like Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.DeleteFile("C:\folder\folder\...\folder\*.txt"), DeleteReadOnly
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now