Monday, 30 April 2012

Deleting file using file extension

To delete files or directory there is the delete command:

>> delete %test.txt


If you want to delete only some files on file extension basis, you can use the following script:
REBOL [
    Title: "Delete Files by Suffix"
    Date: 7-Jul-2000
    File: %delete-suffix.r
    Author: "Reburu"
    Purpose: {
        Delete files based on their suffixes.   Can also delete
        deeply through all subdirectories.
    }
    Note: "Press ESCAPE to break out at the prompt."
]
delete-suffix: func [
    "Delete files deeply by suffix."
    dir-name "Starting directory"
    suffixes "Block of suffixes or none"
    /deep "Delete into subdirectories"
    /sure "Do not verify the deletion"
][
    if dir? dir-name [
        dir-name: dirize dir-name
        ;print ["Inspecting:" dir-name]
        foreach file read dir-name [
            either dir? dir-name/:file [
                if deep [
                    either sure [
                        delete-suffix/deep/sure dir-name/:file suffixes
                    ][delete-suffix/deep dir-name/:file suffixes ]
                ]
            ][
                if any [not suffixes find suffixes find/last file "."] [
                    if any [
                        sure
                        confirm   rejoin ["Delete " dir-name   file " ?"]
                    ][
                        print ["Deleting:" dir-name/:file]
                        delete dir-name/:file
]   ]   ]   ]   ]   ]


It creates the delete-suffix function, you have to choose the directory and the file extension(s):

>> write %test.temp "example"
>> delete-suffix %. %.temp
Deleting: ./test.temp


The option /deep continue to delete in subdirectories, the option /sure doesn't ask you to confirm before deleting:

>> delete-suffix %. none ; delete all files
>> delete-suffix/deep %. [%.jpg %.gif %.bmp] ; delete image files
>> delete-suffix/deep %msvc [%.sbr %.obj %.pdb %.ilk %.pch %.bsc %.idb]
>> delete-suffix/deep/sure %. [%.err ] ; delete all error files for sure

No comments:

Post a Comment