Any way to delete all files in a specific directory?

Heffer

Civil Gamers Expert
Feb 5, 2021
10
2
91
So I wanna delete all my files in my directory because I cant be asked to delete them manually. I wrote this script which I think should of worked but it said "Malformed file name" when I executed my script. Here it is:

string[] files = File.FindFiles("r/documents");
number numFiles = Array.Length(files);
number fileIndex = 1;

while(fileIndex <= numFiles){
StringResult deletefile = File.Delete("r/documents", Util.ToString(files);
fileIndex = fileIndex + 1;

if(deletefile.GetResult()){
Console.WriteLine("deleting files");

}else{
Console.WriteLine("Error:"..deletefile.GetString());
}

}



If anyone can find the issue with this script then I'll highly appreciate it. Idk if its even possible so can someone clarify if it's even possible to do this. Thanks xx
 

Attachments

  • files.JPG
    files.JPG
    38.3 KB · Views: 7

Heffer

Civil Gamers Expert
Feb 5, 2021
10
2
91
Thanks for that however the script still saids "Malformed file name"
 

Heffer

Civil Gamers Expert
Feb 5, 2021
10
2
91
turns out this is impossible to do as you have to specify the filename.
 

Janus

CC Executive VIP
Donator
Dec 25, 2020
52
18
91
It's possible, you just need to delete an individual file each iteration. You also need to request admin privilege in order to delete files.
Example:

Code:
using File;
using Console;
using Array;
using Util;
using Application;
using System;

StringResult reqadmin = Application.RequestAdminPrivilege();
string dir = "r/documents";

string[] files = File.FindFiles("r/documents");
number numFiles = Array.Length(files);
number fileIndex = 1;

Console.WriteLine("Found "..Util.ToString(numFiles).." files!");

while(fileIndex <= numFiles){
    string filename = files[fileIndex]);
    File.Delete(dir, filename);
    Console.WriteLine(fileIndex..". "..filename.." deleted");
    fileIndex = fileIndex + 1;
}