This website is run by the community, for the community... and it needs advertisements in order to keep running. Blocking our ads means your killing our stats!
Please disable your ad-block, or become a premium member to hide all advertisements and this notice.

This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with script to delete files

This website is run by the community, for the community... and it needs advertisements in order to keep running. Blocking our ads means your killing our stats!
Please disable your ad-block, or become a premium member to hide all advertisements and this notice.

Just wondering if the scripting gurus here can help me out a little.
I need a script to check a folder for pdf files and if there are any to delete them.

I've written this script to do this and set a scheduled task to run it every hour. It works fine but when there's no files it pops up an error dialog box.

I then did this one to try checking for the files first. When I run it (and there are files in the folder) it goes straight to the else statement.

Code:

Const DeleteReadOnly = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("[URL="file://\\server\folder\*.pdf"]\\server\folder\*.pdf[/URL]") Then
Set objFolder = objFSO.DeleteFile("[URL="file://\\server\folder\*.pdf"]\\server\folder\*.pdf[/URL]"), DeleteReadOnly
Else
Wscript.Echo "File does not exist."
End If

This website is run by the community, for the community... and it needs advertisements in order to keep running. Blocking our ads means your killing our stats!
Please disable your ad-block, or become a premium member to hide all advertisements and this notice.

I'll take a look at the second, but im fairly sure that the file.exists wont accept wildcards. you would need to loop through each of the files checking for the pdfs

Use this, it will loop through all the files in the folder, and delete any ending in .pdf. Its generally a bad idea to do a wildcard delete, as if you screw it up, you could wipe lots more than you intended. This script will also output an error log of any files that error whilst deleting.

However, the log will be overwritten every time the script executes, it can be modified to create a new file with the current datetime stamp if needed though.

CertForums.com is not sponsored by, endorsed by or affiliated with Cisco Systems, Inc. Cisco®, Cisco Systems®, CCDA™, CCNA™, CCDP™, CCNP™, CCIE™, CCSI™; the Cisco Systems logo and the CCIE logo are trademarks or registered trademarks of Cisco Systems, Inc. All other trademarks, including those of Microsoft, CompTIA, VMware, Juniper ISC(2), and CWNP are trademarks of their respective owners.