Automatically Delete Files/Folders

前端 未结 5 582
自闭症患者
自闭症患者 2020-11-27 04:09

Is there any way to automatically delete all files or folders with few R command lines? I am aware of the unlink() or file.remove() functions, but

5条回答
  •  暖寄归人
    2020-11-27 04:59

    Using a combination of dir and grep this isn't too bad. This could probably be turned into a function that also tells you which files are to be deleted and gives you a chance to abort if it's not what you expected.

    # Which directory?
    mydir <- "C:/Test"
    # What phrase do you want contained in
    # the files to be deleted?
    deletephrase <- "deleteme"
    
    # Look at directory
    dir(mydir)
    # Figure out which files should be deleted
    id <- grep(deletephrase, dir(mydir))
    # Get the full path of the files to be deleted
    todelete <- dir(mydir, full.names = TRUE)[id]
    # BALEETED
    unlink(todelete)
    

提交回复
热议问题