1
1
12rbah2021-01-12 16:37:24
go
12rbah, 2021-01-12 16:37:24

How to properly delete a folder after unzipping?

The program unzips the archive into a directory called "arch_Path" in the same directory, then a series of actions with files takes place, after which you need to delete the directory, but sometimes it happens that the directory cannot be deleted because it is "in use" or the file is used though the program no longer works with it (it), how can I make sure that this directory is always deleted?

sample code if needed

spoiler

"github.com/mholt/archiver"
...
archPath := filepath.Join(filepath.Dir(pathToArch), "arch_Path")
archiver.Unarchive(pathToArch, arch_Path)
...
  defer func() {
    err := os.RemoveAll(tempPath)
    if err != nil {
      log.Println(err)
    }
  }()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Glebov, 2021-01-21
@12rbah

1) Find out what program is blocking your files (unlocker, for example)
2.1) If it is a program you wrote, then find places where read / write operations are not closed
2.2) If this is a foreign program, then try to close it through taskkill (or tell the user so that it closes itself)
2.3) If this is your program and you are trying to delete it, then this is done like this:
https://www.codeproject.com/Articles/31454/How-To-...
3) You can unpack to the Temp folder and delete it :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question