Delete files in a dirusing Windows Script

Posted on 2006-05-01
Last Modified: 2013-12-25
In the scriptcenter of MS I found a little script to remove all files from a dir. It works perfect except when there are files in that dir that are in use. in that case the script crashes with a RT-error: Access Denied - code 800A0046.
The dir where I need the script for, must be cleaned up every day but there are always files in use. Could soneone help me to modify the script so that ik will cleanup all files except those who are in use.

This is the script I've used:
Const DeleteReadOnly = TRUE
Set objFSO = CreateObject("Scripting.FilesSystemObject")

Question by:fvg
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    if files are in use, you cannot deleted them.
    the only think you can do is mark them for deletion in the registry so they get deleted on the next reboot.

    If the application only releases the lock on the file when restarting, you have to restart the application
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    to workaround:

    Const DeleteReadOnly = TRUE
    Set objFSO = CreateObject("Scripting.FilesSystemObject")

    set folder = objFSO.Getfolder("F:\WebMethods\IntegrationServer4\logs\")
    on error resume next
    for each file in folder.files

    Author Comment

    To angelIII
    Maybe I have explained myself in a wrong way but what I need is a script that deleted the files that are NOT in use. The files in use may stay in the dir.
    The script I used will not delete all the files NOT in use because it crashes as soon as it detects a file that is in use.
    LVL 142

    Accepted Solution

    well, with the error handler (on error resume next) the script should not crash but continue...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now