Calling Windows Command

rumbu rumbu at rumbu.ro
Thu Mar 22 06:00:50 UTC 2018


On Wednesday, 21 March 2018 at 18:50:38 UTC, Vino wrote:
> Hi All,
>
>  Request your help in calling the windows command to delete all 
> file and folders recursively as the D function rmdirRecurse 
> does not delete file in the permission of the file is readonly 
> in windows 2008 R2
>
> import std.process: execute;
> import std.array: empty;
> auto RemoveDir () (
> auto dFiles = "C:\\Temp\Test1";
> auto Step = "run";
> if (Step == "run" && !dFiles.empty) {
>     version(Windows)
>      {
>       foreach(d; dFiles) {
>       execute(["rmdir.exe","-command", "/S /Q", `~d~`]); } } } 
> return dFiles;
> )
>
> void main () {
> writeln(RemoveDir);
> }
>
> From,
> Vino.N

It is faster if you use SHFileOperationW directly on Windows (and 
also it will ignore readonly attributes):

https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx


//not tested
void winrmdir(string path)
{
     import core.sys.windows.shellapi;
     import std.exception : enforce;
     import std.file: FileException;
     import std.utf: toUTF16;

     auto shpath = (path ~ "\0\0").toUTF16();
     SHFILEOPSTRUCTW s;
     s.wFunc = FO_DELETE;
     s.pFrom = shpath.ptr;
     s.fFlags = FOF_NOCONFIRMATION | FOF_SILENT | FOF_NOERRORUI;
     enforce!FileException(SHFileOperationW(&s) == 0 && 
!s.fAnyOperationsAborted);
}




More information about the Digitalmars-d-learn mailing list