Delete directorys and their content

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
pspwill
Posts: 51
Joined: Thu Nov 17, 2005 8:07 am

Delete directorys and their content

Post by pspwill »

Is there a function on the PSP to delete directorys and all the content in them? sceIoRmdir wont delete directories if they have files in them.
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

You need to delete the files within the directory first.

I have this code snippet that will delete all files and sub directories as well as the given directory, but it might not be perfect ;)

Code: Select all

void recursiveDelete(char *dir)
{
 DIR *dip;
 struct dirent *dit;
 dip = opendir(dir);
 char fullname[512];

 while ((dit = readdir(dip)) != NULL)
 {
  sprintf(fullname, "%s/%s", dir, dit->d_name);

   if ((FIO_S_IFREG & (dit->d_stat.st_mode & FIO_S_IFMT)) == 0)
      {
             recursiveDelete(fullname);
             printf("Deleting directory: %s\n", fullname);
             rmdir(fullname);
         }
  else
      {
      printf("Deleting file: %s %i\n", fullname, FIO_S_IFREG & (dit->d_stat.st_mode & FIO_S_IFMT));
      remove(fullname);
      }
  }
 closedir(dip);
 rmdir(dir);
}
pspwill
Posts: 51
Joined: Thu Nov 17, 2005 8:07 am

Post by pspwill »

Thanks! :D and if anyone wants it, the psp version:

Code: Select all

void recursiveDelete(char *dir)
{
 int fd;
 SceIoDirent dirent;
 fd = sceIoDopen(dir);
 char fullname[512];

 while (sceIoDread(fd, &dirent) > 0)
 {
  sprintf(fullname, "%s/%s", dir, dirent.d_name);

   if ((FIO_S_IFREG & (dirent.d_stat.st_mode & FIO_S_IFMT)) == 0)
      {
             recursiveDelete(fullname);
             printf("Deleting directory: %s\n", fullname);
             sceIoRmdir(fullname);
         }
  else
      {
      printf("Deleting file: %s %i\n", fullname, FIO_S_IFREG & (dirent.d_stat.st_mode & FIO_S_IFMT));
      sceIoRemove(fullname);
      }
  }
 sceIoDclose(fd);
 sceIoRmdir(dir);
}
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

You'll almost certainly want

Code: Select all

memset(&dirent, 0, sizeof dirent);
in there. Often the random stuff in dirent means sceIoDread() will fail.

Jim
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Or just use Insert_witty_name's code, it uses newlib which takes care of that sort of stuff.
Post Reply