1. 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

Discussion in 'Scripting & Programming' started by nugget, Feb 28, 2008.

  1. nugget
    Honorary Member

    nugget Junior toady

    7,796
    71
    224
    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.

    Code:
     
    Const DeleteReadOnly = TRUE
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile("[URL="file://\\server\folder\*.pdf"]\\server\folder\*.pdf[/URL]"), DeleteReadOnly
    
    
    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
    

    Any ideas?
     
    Certifications: A+ | Network+ | Security+ | MCP (270,271,272,290,620) | MCDST | MCTS:Vista
    WIP: MCSA, 70-622,680,685
  2. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    171
    211
    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
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  3. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    171
    211
    Code:
    Option Explicit
    Const FORWRITING = 2
    Const DELETEREADONLY = TRUE
    
    Dim myFolder, objFSO, objFolder, objOutput, myFile
    myFolder = "C:\Test"
    
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objOutput = objFSO.OpenTextFile("Error Log.csv", FORWRITING, True)
    
    
    
    Set objFolder = objFSO.GetFolder(myFolder)
    
    For each myFile in objFolder.Files
    	If LCASE(Right(myFile.Name, 4)) = ".pdf" Then
    		On Error Resume Next
    		myFile.Delete DELETEREADONLY
    
    		If err.number <> 0 then
    			objOutput.WriteLine myfile.name & "," & err.description
    			err.clear
    		end if
    		On Error Goto 0
    	End If
    Next
    
    objOutput.Close
    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.
     
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  4. nugget
    Honorary Member

    nugget Junior toady

    7,796
    71
    224
    Thanks for that Fergal. Works as advertised. :thumbleft
     
    Certifications: A+ | Network+ | Security+ | MCP (270,271,272,290,620) | MCDST | MCTS:Vista
    WIP: MCSA, 70-622,680,685

Share This Page

Loading...