in reply to delete folder and its content with plain perl

Windows has a help command:
C:\>help rmdir Removes (deletes) a directory. RMDIR [/S] [/Q] [drive:]path RD [/S] [/Q] [drive:]path /S Removes all directories and files in the specified directo +ry in addition to the directory itself. Used to remove a dir +ectory tree. /Q Quiet mode, do not ask if ok to remove a directory tree wi +th /S

Replies are listed 'Best First'.
Re^2: delete folder and its content with plain perl
by pryrt (Abbot) on Nov 07, 2017 at 14:54 UTC

    I had thought roboticus was probably right about requiring cmd /c. So I thought salva's answer wasn't enough; but it turns out to be right (sorry for doubting you, ++salva): you don't need the cmd /c, and all you need is to provide the proper switches to rmdir:

    C:\Temp>mkdir delme.dir\subdir C:\Temp>dir /S delme.dir Volume in drive C is System Volume Serial Number is ... Directory of C:\Temp\delme.dir 11/07/2017 06:44 AM <DIR> . 11/07/2017 06:44 AM <DIR> .. 11/07/2017 06:44 AM <DIR> subdir 0 File(s) 0 bytes Directory of C:\Temp\delme.dir\subdir 11/07/2017 06:44 AM <DIR> . 11/07/2017 06:44 AM <DIR> .. 0 File(s) 0 bytes Total Files Listed: 0 File(s) 0 bytes 5 Dir(s) 45,598,023,680 bytes free C:\Temp>perl -le "print system(qq(rmdir delme.dir)) && $! || 'deleted' +" The directory is not empty. No such file or directory C:\Temp>perl -le "print system(qq(rmdir /S delme.dir)) && $! || 'delet +ed'" delme.dir, Are you sure (Y/N)? n No such file or directory C:\Temp>perl -le "print system(qq(rmdir /S /Q delme.dir)) && $! || 'de +leted'" deleted C:\Temp>dir /S delme.dir Volume in drive C is System Volume Serial Number is ... File Not Found

    But ytjPerl should also study/learn from the other responses, because TIMTOWTDI. And if you are sticking with system, use the multi-argument version that roboticus showed, and beware of quoting and backslashes. My oneliners above were for ease of typing, and to prove salva's point, not for robust and good coding style.

      pryrt:

      Yeah, I probably should have elaborated on that a bit. As you probably know CMD.EXE shell has some commands built into it, so there are no EXE versions of some commands you'd expect to find. I should've mentioned to the OP that adding the "cmd", "/c" arguments are to let you tell the CMD shell to execute a built-in command. For commands with external EXE files (such as RMDIR.EXE) it wouldn't be helpful.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re^2: delete folder and its content with plain perl
by ytjPerl (Scribe) on Nov 08, 2017 at 15:42 UTC
    I am trying to use  my $result =  system ( 'rmdir /S /Q "$foldername"'); to delete the folder I created earlier in my script. But I got the error message that system cannot find the path. Do you know the problem here? I used both with quote and without. Thanks

      ytiPerl:

      Inside single quotes, a variable is not expanded, so you're running "rmdir /S /Q $foldername".

      Try changing it to:

      my $result = system("rmdir /S /Q $foldername");

      Of course, if you have backslashes or spaces in the $foldername variable, then you'll get other problems. That's why you might want to consider using the list version of the system command, like:

      my $result = system('rmdir', '/S', '/Q', $foldername);

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        my $result = system('rmdir', '/S', '/Q', $foldername);?

        I used this, but as my $foldername path is d:\log_script\Archive I got error invalid switch - "log_script" , is that because "_" in my path?