Delete files in Folder using X++ Ax 2012

Hi Friends,

How to delete all the files in particular folder?

I can use WinApi::Deletefile(…) but here i need to give the exact file name. In my scenario, I may have more than one files and in different name. I want to empty the folder.

Please help me to resolve the issue.

Thanks,

Maria

You have two options:

  • either read file names in the folder and delete each one
  • or delete the whole folder and recreate it again

Thanks for the idea. Can you pls give some sample code do that.

static void clearDirectory(Args _args)

{

System.String[] files;

int filecount;

int i;

if (WinAPI::folderExists(‘C:\Myfolder’))

{

files = System.IO.Directory::GetFiles(‘C:\Myfolder’);

filecount = files.get_Length();

for(i=0; i < filecount; i++)

{

System.IO.File::Delete(files.get_Item(i));

}

}

}